;-------------------------
;Divide:
; 15900/1600 = 9.9375
; so round up to 10
;-------------------------
;15900
MOVLW d'28'
MOVWF nratorL
MOVLW d'62'
MOVWF nratorM
CLRF nratorH
;1600
MOVLW d'6'
MOVWF denomL
MOVLW d'64'
MOVWF denomM
CLRF denomH
CALL Divide_24x24
;Result: nratorH nratorM nratorL
;Remainder: remainH remainM remainL
;-------------------------
;-------------------------
;Divide:
; 15000/1600 = 9.375
; so round down to 9
;-------------------------
;15900
MOVLW d'152'
MOVWF nratorL
MOVLW d'58'
MOVWF nratorM
CLRF nratorH
;1600
MOVLW d'6'
MOVWF denomL
MOVLW d'64'
MOVWF denomM
CLRF denomH
CALL Divide_24x24
;Result: nratorH nratorM nratorL
;Remainder: remainH remainM remainL
;-------------------------
;--------------------------------------------------------------------
; SUBROUTINE - 24 BIT DIVISION from TONY NIXON
; numerator: nratorH nratorM nratorL
; denominator: denomH denomM denomL
;
; result: nratorH nratorM nratorL
; remainder: remainH remainM remainL
;--------------------------------------------------------------------
Divide_24x24:
;--------------------------------------------------------------------
movlw d'24' ; set decimal 24 loop count
movwf BCount
movf nratorH,w ; copy Numerator into Shift Holding ram registers (w-reg)
movwf shiftH
movf nratorM,w
movwf shiftM
movf nratorL,w
movwf shiftL
clrf nratorH ; clear final Answer Numerator Ram locations
clrf nratorM
clrf nratorL
;
clrf remainH ; clear final Answer Remainder Ram locations
clrf remainM
clrf remainL
dloop:
bcf STATUS, C ; bit clear Carry Flag in STATUS register
rlf shiftL, f ; Shift numerator(dividend) Left to move
rlf shiftM, f ; next bit to remainder
rlf shiftH, f ; and shift in next bit of result
rlf remainL, f ; shift carry (next Dividend bit) into remainder
rlf remainM, f
rlf remainH, f
movf denomH,w
subwf remainH,w ; subtract divsor(denomH) from(newly shifted left) Remainder HIGH byte.
btfss STATUS, Z
goto nochk ; skip if result was ZERO from good subtraction result
;
movf denomM,w
subwf remainM,w ; subtract divsor(denomM) from(newly shifted left) Remainder MIDDLE byte.
btfss STATUS, Z
goto nochk ; skip if result was ZERO from good subtraction result
;
movf denomL,w
subwf remainL,w ; subtract divsor(denomL) from(newly shifted left) Remainder LOW byte.
nochk:
btfss STATUS, C ; Carry SET? then denom is larger than reemainder
goto nogo
;
movf denomL,w
subwf remainL, f ; Subtract denominator from remainder value in Low Byte
btfsc STATUS, C ; Carry Set? Then execute fixup code for when a borrow is generated
goto nodec_remainM ; when no borrow bit is needed from the higher byte positions.
decf remainM, f ; Decrement to Borrow from Middle Byte, because carry was SET.
movf remainM,w
xorlw 0xff ; Check if rollover from Borrow occurred. remainM value went from 0 to 0xFF
btfsc STATUS, Z
decf remainH, f ; ZERO bit set, yes rollover, so Decrement to Borrow from High Byte, too!
nodec_remainM:
movf denomM,w
subwf remainM, f ; Subtract denominator from remainder value in Middle Byte
btfss STATUS, C
decf remainH ,f ; Decrement High Byte, to borrow 1 bit
movf denomH,w
subwf remainH, f ; Subtract denominator from remainder value in High Byte
bsf STATUS, C ; set CARRY bit to rotate in Numerator Result next.
nogo:
rlf nratorL, f ; rotate Numerator result left 1 bit
rlf nratorM, f
rlf nratorH, f
decfsz BCount, f ; decrement the Loop Bit Counter
goto dloop
;
return ; all done
;--------------------------------------------------------------------------------