PIC Microcontoller Math Method

BCD (packed) addition

From: Scott Dattalo

 ;******************************************
;bcd_add
;
; Computes  z = x + y
; where x,y,z are all 8-bit packed BCD numbers
; Exits with C=1 if x+y > 0x99 and with z=1 if
; x+y = 0x100.
; Note that z can be aliased to x or y so that
;it's possible to calculate x = x+y or y = x+y
;This routine forms the BCD two's complement of
;y and then uses the bcd_subtract routine to
;find the sum. e.g. z = x - (-y) = x + y
;
; 10 cycles
bcd_add
	mov	W, /y	;W = ~y
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;        ADDLW   0x9a+1  ;W = ~y + 0x9a +1
	mov	Hack, W
	mov	W, #$9a+1	;W = ~y + 0x9a +1
	add	W, Hack
                        ;W = (~y + 1) + 0x9a
                        ;W = 0x9a - y
	mov	W, x-w	;W = x - (0x9a - y)
                        ;W = x + y - 0x9a
                        ;W = x + y + 0x66
	rl	z	;Get the carry
	sb	DC	;if lsn of x + lsn of y < 10 (dec)
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;         ADDLW  -0x06   ; then remove the extra 6 added above
	mov	Hack, W
	mov	W, #-$06	; then remove the extra 6 added above
	add	W, Hack
	sb	z.0	;Similarly for the msn
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;         ADDLW  -0x60
	mov	Hack, W
	mov	W, #-$60
	add	W, Hack
	rr	z	;restore the carry
	mov	z, W

	ret