Full Version : Division by Ten (PIC ASM)
avr >>MATH ROUTINES >>Division by Ten (PIC ASM)


Admin3- 04-18-2006
QUOTE
I can save 600 bytes in a lookup table if I can figure out a good way to
divide a 16 bit number by 10.


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.

CODE


N_hi    equ     0x20
N_lo    equ     0x21

count   equ     0x22

R_hi    equ     0x23
R_lo    equ     0x24


;----------------------------------
;divby10
;  Divides the unsigned integer N_hi:N_lo by the constant 10.
;
;Input
;      N_lo - Low byte of  the 16 bit dividend
;      N_hi - High     "                  "
;Output
;      R_lo - Low byte of the result
;      R_hi - High    "         "
;
; 14  words
; 149 cycles

divby10_ver3

      CLRF    R_lo           ;Only need to clear R_lo. R_hi is cleared by
shifting(below)
      MOVLW   13
      MOVWF   count          ;
v3_1    MOVLW   0xa0           ;If the high byte is greater than or equal to
0xa0,
      SUBWF   N_hi,W         ;then this subtraction causes no borrow (i.e.
C=1)
      SKPNC
        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.
      RLF     N_hi,F
      DECFSZ  count,F
        goto  v3_1

      RETURN



Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.