piclist 1996\05\06\173356a >
Thread: A math problem?
picon face BY : Bob Fehrenbach email (remove spam text)

Xaq <KILLspamxaqTakeThisOuTspamEraseMEINDIRECT.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.


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

  movlw   8
  movwf   count
  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


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

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

  retlw   0

Bob Fehrenbach    Wauwatosa, WI     spam_OUTbfehrenbspam_OUTspam@spam@execpc.com
<l8mjxYi8aEWM089yn@execpc.com> 8bit

In reply to: <199605061922.MAA27141@ns2.indirect.com>
See also: www.piclist.com/techref/microchip/math/index.htm?key=math
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) A math problem?

month overview.

new search...