Searching \ for 'PICLIST Digest - 31 Aug 1997 to 1 Sep 1997' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'PICLIST Digest - 31 Aug 1997 to 1 Sep 1997'.

Truncated match.
'PICLIST Digest - 31 Aug 1997 to 1 Sep 1997'
1997\09\05@024338 by

Hi Folks,
Does anyone out there have any code for a reasonably
fast divide by 10 routine. I'm just looking for a rounded
up 8 bit signed value as the result I don't need the
remainder.

Andy
Hi Folks,
Does anyone out there have any code for a reasonably
fast divide by 10 routine. I'm just looking for a rounded
up 8 bit signed value as the result I don't need the
remainder.

Andy
Andy Shaw <andy.shawVIRGIN.NET> wrote:
>Hi Folks,
>Does anyone out there have any code for a reasonably
>fast divide by 10 routine. I'm just looking for a rounded
>up 8 bit signed value as the result I don't need the
>remainder.

It's not clear how large your dividend is, but if it is a
single byte, take a look at this from Andy Warren.

(Posting this for Andy W in case he is taking a nap.)

Note that this is unsigned arithmetic.

; 8-Bit Divide.  Written by Andy Warren.
;
; Permission is hereby granted for any non-commercial use,
; so long as this copyright notice remains intact.
;
; Enter with Dividend in DIVIDEND, divisor in DIVISOR.
;
; Exits with quotient in QUOTIENT and remainder in REMAINDER, DIVIDEND
; scrambled, DIVISOR unchanged, carry-flag set.
; It takes about 80 cycles.

DIVIDE  MOVLW   1
MOVWF   QUOTIENT

CLRF    REMAINDER

LOOP    RLF     DIVIDEND
RLF     REMAINDER

MOVF    DIVISOR,W
SUBWF   REMAINDER,W
SKPNC

MOVWF   REMAINDER

RLF     QUOTIENT
BNC     LOOP

--
Bob Fehrenbach    Wauwatosa, WI     bfehrenbexecpc.com
> Andy Shaw <andy.shawVIRGIN.NET> wrote:
> >Hi Folks,
> >Does anyone out there have any code for a reasonably
> >fast divide by 10 routine. I'm just looking for a rounded
> >up 8 bit signed value as the result I don't need the
> >remainder.

X / 10 = X * 1/10       Is this right ? ;)

1/10 = (1/16 + 1/32) + (1/256 + 1/512) + ....

1/16 + 1/32 + 1/256 =  0,09765625 (-0,0234375%)

( 1 + 1/2 + 1/16 ) / 16 ;

;code
;initial var X
;result var Y

;+1/2
clrc
rrf     X,W
movwf   Y

;+1/16
swapf   X,W
andlw   0xF

;+1
movfw   X

;/16
swapf   Y,W
andlw   0xF

skpnc
movwf   Y
;
; Caution: this code is not tested !!!

I think there are way to shrink this code .

WBR Dmitry.
Dmitry Kiryashov <zewsAHA.RU> wrote:
>> Andy Shaw <andy.shawVIRGIN.NET> wrote:
>> >Does anyone out there have any code for a reasonably
>> >fast divide by 10 routine.

Dmitry's creative solution with a suggestion:

>;+1/2
>        clrc
>        rrf     X,W
>        movwf   Y
>
>;+1/16
>        swapf   X,W
>        andlw   0xF

; ***  New code inserted  ***

swapf   X,W    ;  1/16 + 1/16:  improves round off
andlw   0xF    ;