ON 20050317@5:33:07 PM at page: http://www.piclist.com/microchip/math/div/24by24tn.htm#38426.8549305556 James Newton[JMN-EFP-786] published post 38426.8549305556 programsetc@netscape.net shares this code:
Correcting High byte remainder error and added comments to Tony Nixon Code and Fred Mahers update to the 24bit by 24bit divide routine. Thank you all for the USEFUL Code references and comments here. note Nikolai Golovchenko and Zet P.
Fred Finster 3/15/2005 Error Corrected
Numerator Denominator Remainder Answer Divisor Remainder
FFFFFF FFFFFE FF0001 000001 FFFFFE 000001
8FFFFF 8FFFFE FF0001 000001 8FFFFE 000001
2FFFFF 2FFFFE FF0001 000001 2FFFFE 000001
2FFFFE 2FFFFE 000001 2FFFFE 000000
2FFFFD 2FFFFE 000000 2FFFFE 2FFFFD
0F4240 004240 00003A 004240 003FC0
;-----------------------------------------------------
; Here is the changed code, that corrects the HIGH byte remainder error
; 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.
;
;
;--------------------------------------------------------------------
; 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 .24 ; set decimal 24 loop count
movwf BCount
movf nratorH,w ; copy Numerator into Shift Holding ram registers
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
|Delete 'P-' before: '' but after: '