;***************************************************************************
;*
;* "bcdAdd" - 2-digit packed BCD addition
;* Fixed by Wagner Lipnharski - wagner@ustr.net - Mar/2002
;* Modified by Professor Barnekow - barnekow@msoe.edu - APR/2007
;* This subroutine adds the two unsigned 2-digit BCD numbers
;* "BCD1"(r24) and "BCD2"(r22). The result is returned in "BCD1"(r24), and the overflow
;* carry in "BCD2".
;*
;* Calling convention:
;*	Pass BCD1 and BCD2 via r24 and r22 repectively
;*	Return BCD sum in r24
;*
;* Example:
;*	lds r24, count
;*	ldi r22, 1
;*	rcall bcdAdd
;*
;* Low registers used :None
;* High registers used  :2 (r24,r22) 
;* T Flag used as a temporary internal Carry Flag
;* On Return, Carry Flag represents the real valid status of the Add.
;***************************************************************************

;***** Subroutine Register Variables

.def BCD1 =r24  	;BCD input value #1
.def BCD2 =r22  	;BCD input value #2

;***** Code

bcdAdd:     
    clt             ;FIX BUG - Clear Temp Carry
    add  BCD1,BCD2  ;add the numbers binary
    ldi  BCD2,6     ;value to be added later
    brcc add_0      ;if carry not clear
    set             ;Save Intermediary Carry
add_0:      
    brhs add_1      ;if half carry not set
    add  BCD1,BCD2  ;add 6 to LSD
    brcc add_5      ;FIX BUG    is carry clear?
    set             ;FIX BUG    If not Save it (Fix Bug)
add_5:      
    brhs add_2      ;if half carry not set (LSD <= 9)
    subi BCD1,6     ;restore value
    rjmp add_2      ;else
add_1:      
    add  BCD1,BCD2  ;add 6 to LSD
add_2:      
    swap BCD2
    add  BCD1,BCD2  ;add 6 to MSD
    brcs add_4      ;if carry not set (MSD <= 9)
    brts add_4      ;FIX BUG
    subi BCD1,0x60  ;restore value
    clr  BCD2       ;clear secondary BCD carry 
add_3:      	
    ret             ;else
add_4:      
    ldi  BCD2,1     ;set secondary BCD carry
    sec             ;set Carry Flag for multi-byte bcd add
    ret 