Full Version : Hillman's 24-by-8-Bit Divide (PIC ASM)
avr >>PIC 8051 ZILOG ARM TI H8 ETC >>Hillman's 24-by-8-Bit Divide (PIC ASM)


Admin3- 04-18-2006
Divide 24 bit int by 8 bit int to 24 bit int and remainder

James Ashley Hillman of Industrial Interface Research Ltd shares this code:

The archive on this page links to 2 divide 24 by 8 routines.

During testing I discovered that high numbers cause the wrong
answers to be calculated (eg 0xF00000 / 0xFD = wrong answer)

I wrote my own routine which is Nikolai's 24 bits by 16,
modified to divide by 8 bits, remainder is also 8 bits:

CODE

;***********************************************************
;Unsigned 24 bit by 8 bit divide routine
;
; Inputs:
;   Dividend  - x,x+1,x+2 (x+2 - most significant!)
;   Divisor   - y
; Temporary:
;   Counter   - counter
; Output:
;   Quotient  - x,x+1,x+2 (x+2 - most significant!)
;   Remainder - x+3
;
; Size: 17
; Timing: 342 cycles (including call and return)
;
; This is basically Nikolai Golovchenko's 24 by 16 bit
; divide routine, with some instructions removed to
; optimise it for an 8 bit divide.
; Thanks to Nikolai for the original post.
;
; James Hillman, 2 December 2005
;***********************************************************

FXD248U:
   CLRF    x+3         ;remainder
   MOVLW   d'24'
   MOVWF   counter

LOOPU248
   RLF     x,W     ;shift dividend left to move next bit to remainder
   RLF     x+1,F   ;
   RLF     x+2,F   ;
   RLF     x+3,F   ;shift carry (next dividend bit) into remainder
   RLF     x,F     ;finish shifting the dividend and save carry in x.0,
                   ;since remainder can be 9 bit long in some cases
                   ;This bit will also serve as the next result bit.
       
   MOVF    y,W     ;substract divisor from 8-bit remainder
   SUBWF   x+3,F        

;here we also need to take into account the 9th bit of remainder, which
;is in x.0. If we don't have a borrow after subtracting from
;8 bits of remainder, then there is no borrow regardless of 9th bit
;value. But, if we have the borrow, then that will depend on 9th bit
;value. If it is 1, then no final borrow will occur. If it is 0, borrow
;will occur. These values match the borrow flag polarity.

   BTFSC   STATUS,0 ;if no borrow after 8 bit subtraction
   BSF     x,0      ;then there is no borrow in result. Overwrite
                    ;x.0 with 1 to indicate no borrow.
                    ;if borrow did occur, x.0 already
                    ;holds the final borrow value (0-borrow,
                    ;1-no borrow)

   BTFSS   x,0      ;if no borrow after 9-bit subtraction
   ADDWF   x+3,F    ;restore remainder. (w contains the value
                    ;subtracted from it previously)
   DECFSZ  counter,F    
   GOTO    LOOPU248    
   RETURN



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