16bit divide by 10
Scott Dattalo email (remove spam text)
Tom Van Baak wrote: (note: Tom did not send this to the PIC list)
> Is it me or does your divby10_ver3 fail on 16?
No, it's me! I didn't take (==have) the time to test the routine thoroughly
enough during lunch. The problem was that under certain conditions, a borrow
was not occuring during the subtraction (when I was expecting one to occur).
To fix the problem, I shifted the input right one bit and divided by 0x50
of 0xa0. I tested this version over more but still not all of the possible
(16 was the first value I tested).
N_hi equ 0x20
N_lo equ 0x21
count equ 0x22
R_hi equ 0x23
R_lo equ 0x24
; Divides the unsigned integer N_hi:N_lo by the constant 10.
; N_lo - Low byte of the 16 bit dividend
; N_hi - High " "
; R_lo - Low byte of the result
; R_hi - High " "
; 17 words
; 152 cycles
CLRF R_lo ;Only need to clear R_lo. R_hi is cleared by
MOVWF count ;
v3_1 MOVLW 0x50 ;If the high byte is greater than or equal to
SUBWF N_hi,W ;then this subtraction causes no borrow (i.e.
MOVWF N_hi ;Replace N_hi with N_hi - 0xa0 if
RLF R_lo,F ;Shift result left one bit and
RLF R_hi,F ; pick up the carry bit in the process.
RLF N_lo,F ;Adjust N for the next iteration.
Thanks Tom for catching this one.
PS. Next time, I'll make my butthead project manager insist I go to lunch.
Oops, sorry E. S., I meant that other butthead project manager.
See also: www.piclist.com/techref/method/math.htm?key=divide
You must be a member of the
piclist mailing list
(not only a www.piclist.com member) to post to the