16bit divide by 10
Scott Dattalo email (remove spam text)
David E. Queen wrote: (a few weeks ago)
> I can save 600bytes in a lookup table if I can figure out a good way to
> divide a 16 bit number by 10.
Did you ever code a 16 bit divide by 10 routine? If so what did you end
Out of curiosity, I wrote a few versions. Here are some approximate cycle
and word counts:
Version 1: 50 cycles 50 words
Version 2: 90 cycles 40 words
Version 3: 136 cycles 14 words
Version 1 is an implementation of the solution I had originally posted.
Version 2 is an implementation of a variation of Andy Warren's solution.
Version 3 is an old-fashioned shift and subtract routine.
The first two versions exist only on paper. The third has been tested over
several, but not all 2^16 possible dividends.
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 " "
; 14 words
; 149 cycles
CLRF R_lo ;Only need to clear R_lo. R_hi is cleared by
MOVWF count ;
v3_1 MOVLW 0xa0 ;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.
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