PIC Microcontroller Math Method

BCD (packed) subtraction

From: Tony Nixon


;       ------------------------------------------
; SUBROUTINE - 24 BIT DIVISION
; numerator:        nratorH  nratorM  nratorL
; denominator:      denomH   denomM   denomL
;
; result:           nratorH  nratorM  nratorL
; remainder:        remainH  remainM  remainL
;
divizn  movlw .24
        movwf BCount
        movf nratorH,w
        movwf shiftH
        movf nratorM,w
        movwf shiftM
        movf nratorL,w
        movwf shiftL
        clrf nratorH
        clrf nratorM
        clrf nratorL
        ;
        clrf remainH
        clrf remainM
        clrf remainL
dloop   bcf status,carry
        rlf shiftL
        rlf shiftM
        rlf shiftH
        rlf remainL
        rlf remainM
        rlf remainH
        movf denomH,w
        subwf remainH,w
        btfss status,z
        goto nochk
        ;
        movf denomM,w
        subwf remainM,w
        btfss status,z
        goto nochk
        ;
        movf denomL,w
        subwf remainL,w
nochk   btfss status,carry
        goto nogo
        ;
        movf denomL,w
        subwf remainL
        btfss status,carry
        decf remainM
        movf remainM,w
        xorlw 0xff
        btfsc status,z
        decf remainH
        movf denomM,w
        subwf remainM
        btfss status,carry
        decf remainH
        movf denomH,w
        subwf remainH
        bsf status,carry
nogo    rlf nratorL
        rlf nratorM
        rlf nratorH
        decfsz BCount
        goto dloop
        ;
        return