piclist 1996\05\06\173356a >
www.piclist.com/techref/microchip/math/index.htm?key=math
BY : Bob Fehrenbach email (remove spam text)

Xaq <xaqINDIRECT.COM> wrote:

>I need to solve this simple equation using a 16C74:
>
>        ans = (X*Y)/Z
>
>ans, X, Y, and Z are all one byte variables (0-255)
>Z is ALWAYS greater than Y, but X can be ANY number except zero.
>None of the numbers are ever zero.

Zach,

Since Z > Y, the answer should always fit in one byte.  You will,
however, need a two byte intermediate result.   Try the following.
I think you will find the code reasonably compact.

movf    X, w
movwf   mulplr
movf    Y, w
movwf   mulcnd
call    mpy                     ;-> accHI:accLO
movf    Z, w
call    div8                    ;Final result in accLO

;Divide 16 bits by 8 bits, 8 bit result.
;Conventional shift and subtract
;Result MUST fit in 8 bits, otherwise erroneous result.
;110 clock cycles.
;Source: Bob Fehrenbach
;   accHI : accLO / dvsr  -> accLO, rem in accHI

div8:
movlw   8
movwf   count
div_loop:
rlf     accLO, f
rlf     accHI, f
rlf     temp, f                 ;save "borrow"
movf    dvsr, w
subwf   accHI,w
btfss   temp, 0                 ;Save result if borrow = 1
skpnc                           ;Else, save only if positive
movwf   accHI
btfsc   temp, 0                 ;Make sure carry is a 1 if
rrf     temp, f                 ;borrow = 1
decfsz  count, f
goto    div_loop
rlf     accLO, f                ;quotient in accLO
div_exit:                          ;remainder in accHI
return

;**************************************************************************

;                   8x8 Multiply, 16 bit Result
;
;  mulplr * mulcnd ->  accHI:accLO
;  71 clock cycles
;  (From Microchip app note AN526)
;
mpy:

clrf    accHI
clrf    accLO
movlw   8
movwf   count
movf    mulcnd,w
bcf     STATUS,C
loop
rrf     mulplr, f
btfsc   STATUS,C
rrf     accHI, f
rrf     accLO, f
decfsz  count, f
goto    loop

retlw   0

--
Bob Fehrenbach    Wauwatosa, WI     bfehrenbexecpc.com
<l8mjxYi8aEWM089yn@execpc.com> 8bit