please dont rip this site

PIC Microcontoller Math Method

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. 

-PICList post "24bit by 16bit Division" (actually 24 / 8)
-PICList post "Divide 24/8 routine" ASM embedded in C  

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:

;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 

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

    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    


file: /Techref/microchip/math/div/24by8-jah.htm, 3KB, , updated: 2018/3/7 09:17, local time: 2024/2/27 16:17,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> PIC Microcontoller Math Method Divide 24 bit int by 8 bit int to 24 bit int and remainder</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!


Welcome to!