• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

BCD Subtraction Code Help

Not open for further replies.


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.
            movf        Trim_Seconds,W
            subwf       Seconds,W
            movwf      W_Seconds


Active Member
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.

Mike - K8LH

Well-Known Member
I found this one for packed-BCD... Probably came from PICLIST code repository...

;  packed bcd subtract -> x - y = z  (C = 0 = 'borrow')
        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
Not open for further replies.

Latest threads

EE World Online Articles