Full Version : Hemsley's 32-by16 Divide (PIC ASM)
avr >>MATH ROUTINES >>Hemsley's 32-by16 Divide (PIC ASM)


Admin3- 04-18-2006
32 bits by 16 by Peter Hemsley

divide movlw 32

CODE
; 32-bit divide by 16-bit
      movwf bitcnt
      clrf remdrH  ; Clear remainder
      clrf remdrL

dvloop clrc         ; Set quotient bit to 0
                   ; Shift left dividend and quotient
      rlf divid0   ; lsb
      rlf divid1
      rlf divid2
      rlf divid3   ; lsb into carry
      rlf remdrL   ; and then into partial remainder
      rlf remdrH

      skpnc        ; Check for overflow
      goto subd
      movfw divisH ; Compare partial remainder and divisor
      subwf remdrH,w
      skpz
      goto testgt  ; Not equal so test if remdrH is greater
      movfw divisL ; High bytes are equal, compare low bytes
      subwf remdrL,w
testgt skpc         ; Carry set if remdr >= divis
      goto remrlt

subd   movfw divisL ; Subtract divisor from partial remainder
      subwf remdrL
      skpc         ; Test for borrow

      decf remdrH  ; Subtract borrow
      movfw divisH
      subwf remdrH
      bsf divid0,0 ; Set quotient bit to 1
                   ; Quotient replaces dividend which is lost
remrlt decfsz bitcnt
      goto dvloop
      return


Comments:

QUOTE
It wouldn't hurt to change "movlw 32" to "movlw .32" (without quotes). If default radix isn't decimal, assembler will misinterpret that number...



Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.