PIC Microcontroller Math Method

Divide 24 bit int by 16 bit int to 24 bit int

by Nikolai Golovchenko

;Inputs:
;   Dividend - AARGB0:AARGB1:AARGB2 (0 - most significant!)
;   Divisor  - BARGB0:BARGB1
;Temporary:
;   Counter  - LOOPCOUNT
;   Remainder- REMB0:REMB1
;Output:
;   Quotient - AARGB0:AARGB1:AARGB2
;   Remainder- REMB0:REMB1
;
;       Size: 28
; Max timing: 4+24*(6+6+4+3+6)-1+3+2=608 cycles (with return)
; Min timing: 4+24*(6+6+5+6)-1+3+2=560 cycles (with return)
;          

FXD2416U:
        CLRF REMB0
        CLRF REMB1
        MOVLW 24
        MOVWF LOOPCOUNT
LOOPU2416
        RLF AARGB2, F		;shift left divider to pass next bit to remainder
        RLF AARGB1, F		;and shift in next bit of result
        RLF AARGB0, F

        RLF REMB1, F		;shift carry into remainder
        RLF REMB0, F

        RLF LOOPCOUNT, F		;save carry in counter
         
        MOVF BARGB1, W  		;substract divisor from remainder
        SUBWF REMB1, F
        MOVF BARGB0, W
        BTFSS _C
        INCFSZ BARGB0, W
        SUBWF REMB0, W  		;keep that byte in W untill we make sure about borrow

	SKPNC			;if no borrow
         BSF LOOPCOUNT, 0		;set bit 0 of counter (saved carry)

	BTFSC LOOPCOUNT, 0 	;if no borrow
	 GOTO UOK46LL	   	;jump

        MOVF BARGB1, W		;restore remainder if borrow
        ADDWF REMB1, F
        MOVF REMB0, W		;read high byte of remainder to W
        				;to not change it by next instruction
UOK46LL
	MOVWF REMB0		;store high byte of remainder
	CLRC			;copy bit 0 to carry
	RRF LOOPCOUNT, F		;and restore counter
	DECFSZ LOOPCOUNT, f	;decrement counter
	 GOTO LOOPU2416		;and repeat loop if not zero
	 
        RLF AARGB2, F		;shift in last bit of result
        RLF AARGB1, F	
        RLF AARGB0, F
        RETURN


Interested: