piclist 1996\06\11\204818a >
Thread: 16bit divide by 10
www.piclist.com/techref/method/math.htm?key=divide
face
flavicon
face BY : 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
instead
of 0xa0. I tested this version over more but still not all of the possible
values.
(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


;----------------------------------
;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    "         "
;
; 17  words
; 152 cycles

divby10_ver3
       CLRC
       RRF     N_hi,F
       RRF     N_lo,F
       CLRF    R_lo            ;Only need to clear R_lo. R_hi is cleared by
shifting(below)
       MOVLW   13
       MOVWF   count           ;
v3_1    MOVLW   0x50            ;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


Thanks Tom for catching this one.

Scott

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.
<31BE134B.707E@unix.sri.com> 7bit

See also: www.piclist.com/techref/method/math.htm?key=divide
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) 16bit divide by 10

month overview.

new search...