please dont rip this site

SX Microcontroller Math Method

BCD (packed) addition

From: Scott Dattalo

Translated and optimized for the Scenix SX by Nikolai Golovchenko

;******************************************
;bcd_add
;
; Computes  x = x + y
; where x and y are all 8-bit packed BCD numbers
; Exits with C=1 if x+y > 0x99 and with z=1 if
; x+y = 0x100 or x+y = 0.
;
; Size: 15 instructions
; Execution time including return: 14/15 cycles

bcd_add
	mov	W, #$66	;x = x + y + 0x66
	add	W, y
	add	x, W

	clr	W
	sb	DC		;if lsn of x + lsn of y < 10 (dec)
	 or	W, #$06	;then remove the extra 6 added above
	snb	C		;Similarly for the msn
	 jmp	bcd_add2

	or	W, #$60
	sub	x, W		;correct result
	clrb	C		;restore carry
	ret
bcd_add2
	sub	x, W		;correct result
	setb	C		;restore carry
	ret

If you don't need to check the carry the routine can be simplified:

;******************************************
;bcd_add_nc
;
; where x and y are all 8-bit packed BCD numbers
; Exits with z=1 if x+y = 0x100 or x+y = 0.
;
; Size: 10 instructions
; Execution time including return: 12 cycles

bcd_add_nc
	mov	W, #$66	;x = x + y + 0x66
	add	W, y
	add	x, W

	clr	W
	 sb	DC		;if lsn of x + lsn of y < 10 (dec)
	or	W, #$06	;then remove the extra 6 added above
	 sb	C		;Similarly for the msn
	or	W, #$60

	sub	x, W		;correct result
	ret

If you don't want to influence input registers, the following routine computes s = x + y:

 

;******************************************
;bcd_add_to
;
; Computes  s = x + y
; where x and y are all 8-bit packed BCD numbers
; Exits with C=1 if x+y > 0x99 and with z=1 if
; x+y = 0x100 or x+y = 0.
;
; Size: 16 instructions
; Execution time including return: 15/16 cycles

bcd_add_to
	mov	W, #$66	;x = x + y + 0x66
	add	W, y
	add	W, x
	mov	s, W

	clr	W
	sb	DC		;if lsn of x + lsn of y < 10 (dec)
	 or	W, #$06	;then remove the extra 6 added above
	snb	C		;Similarly for the msn
	 jmp	bcd_add_to2

	or	W, #$60
	sub	s, W            ;correct result
	clrb	C		;restore carry
	ret
bcd_add_to2
	sub	s, W		;correct result
	setb	C		;restore carry
	ret

Comments:


file: /Techref/scenix/lib/math/add/bcdp2_sx.htm, 2KB, , updated: 2009/12/7 12:16, local time: 2024/10/7 00:40,
TOP NEW HELP FIND: 
3.237.15.145:LOG IN
©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://www.sxlist.com/techref/scenix/lib/math/add/bcdp2_sx.htm"> PIC Microcontroller Math Method BCD (packed) addition</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to www.sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .