Searching \ for 'Divide routine error' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/method/math.htm?key=divide
Search entire site for: 'Divide routine error'.

Truncated match.
PICList Thread
'Divide routine error'
1997\03\27@105614 by Bob Fehrenbach

picon face
Red face mode on.

Sometime ago in response to someone's request for a divide routine
I posted a 32 by 32 bit routine.  Yesterday I received a private
message from Lieven Leroy pointing out that in the four byte subtract,
the carry was incorrectly propagated.  (Great catch)  Here is the
revised version.


;*******************************************************************
;  Division:  Divide 32 bits by 32 bits,
;             32 bit quotient and remainder.
;  Standard shift and subtract algorithm.
;
;   q_4,3,2,1 / n_4,3,2,1 -> q_4,3,2,1   rem in t_4,3,2,1
;
;  Uses count
;
;  Execution time: Extremes seem to be 620 to 1350 clock cycles
;  Program memory: 52
;  Author: BF
;  Bug reported by Lieven Leroy regarding carry propagation in four
;  byte subtract - fixed.
;*******************************************************************

div32b32: macro
  local   loop, check_sign, check_count
  clrf    t_1
  clrf    t_2
  clrf    t_3
  clrf    t_4

  movlw   32
  movwf   count

loop:
  rlf     q_1, f
  rlf     q_2, f
  rlf     q_3, f
  rlf     q_4, f
  rlf     t_1, f
  rlf     t_2, f
  rlf     t_3, f
  rlf     t_4, f

  movf    n_4, w
  subwf   t_4, w
  skpz
  goto    check_sign
  movf    n_3, w
  subwf   t_3, w
  skpz
  goto    check_sign
  movf    n_2, w
  subwf   t_2, w
  skpz
  goto    check_sign
  movf    n_1, w
  subwf   t_1, w
check_sign:
  skpc
  goto    check_count

  movf    n_1, w      ;subtract least significant byte
  subwf   t_1, f
                      ;2nd byte
  movf    n_2, w      ;subtract with borrow
  skpc
  incfsz  n_2, w
  subwf   t_2, f
                      ;3rd byte
  movf    n_3, w
  skpc
  incfsz  n_3, w
  subwf   t_3, f
                      4th byte
  movf    n_4, w
  skpc
  incfsz  n_4, w
  subwf   t_4, f

  bsf     STATUS, C

check_count:
  decfsz  count, f
  goto    loop
  rlf     q_1, f
  rlf     q_2, f
  rlf     q_3, f
  rlf     q_4, f

  endm

--
Bob Fehrenbach    Wauwatosa, WI     spam_OUTbfehrenbTakeThisOuTspamexecpc.com

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...