BCD Subtraction Code Help

Status
Not open for further replies.

Suraj143

Active Member
I need 8bit BCD packed subtraction routine.Can somebody help on this.
The seconds register I get from DS3231 in BCD format.Trim_Seconds also in BCD format.
Code:
            movf        Trim_Seconds,W
            subwf       Seconds,W
            movwf      W_Seconds
 
I have code for doing an unpacked BCD subtraction which won't be difficult to adapt to packed BCD. But maybe someone else already has a packed BCD version. If not, I can post my unpacked version.
 
I found this one for packed-BCD... Probably came from PICLIST code repository...

Code:
;******************************************************************
;
;  packed bcd subtract -> x - y = z  (C = 0 = 'borrow')
;
bcdsubtract
        MOVF    y, W            ; W = y
        SUBWF   x, W            ; W = x-y
        RLF     z, F            ; lsb of z has the carry
        SKPDC                   ; if lsn of x < lsn of y
         ADDLW  -0x06           ;  then convert lsn of the
                                ;  result to BCD.
        BTFSS   z, 0            ; Similarly for the msn's
         ADDLW  -0x60
        RRF     z, F            ; Get the carry
        MOVWF   z               ; and save the result
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…