# SXMicrocontrollerMathMethod

From: Scott Dattalo

Translated and optimized for the Scenix SX by Nikolai Golovchenko

```;******************************************
;
; 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

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

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

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

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

```;******************************************
;
; 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

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

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:

```;******************************************
;
; 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

mov	W, #\$66	;x = x + y + 0x66
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

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

```

 file: /Techref/scenix/lib/math/add/bcdp2_sx.htm, 2KB, , updated: 2009/12/7 11:16, local time: 2022/1/26 21:40, TOP NEW HELP FIND:  54.173.214.227:LOG IN

 ©2022 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! PIC Microcontroller Math Method BCD (packed) addition

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.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded."

### Welcome to sxlist.com!

Site supported by
& kind contributors
just like you!

(here's why

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

.