please dont rip this site

PIC Microcontoller Math Method

Divide 16 bit int by 8 bit int to 8 bit int and remainder

#define bf_carry 3, 0
#define bf_zero 3, 2

#define same 1
#define wreg 0

#define stc bsf bf_carry
#define clc bcf bf_carry

;-[ Div ]--------------------------------------------------------------
; Call w/: Number in f_divhi:f_divlo, divisor in W.
; Returns: Quotient in f_divlo, remainder in f_divhi. W preserved.
;          Carry set if error. Z if divide by zero, NZ if divide overflow.
; Notes:   Works by left shifted subtraction.
;          Size = 29, Speed(w/ call&ret) = 7 cycles if div by zero
;          Speed = 94 minimum, 129 maximum cycles

    addlw 0          ; w+=0 (to test for div by zero)
    stc              ; set carry in case of error
    btfsc bf_zero    ; if zero
     return          ;   return (error C,Z)

    call DivSkipHiShift
iDivRepeat = 8
while iDivRepeat

    call DivCode


    rlf f_divlo, same ; C << lo << C

    ; If the first subtract didn't underflow, and the carry was shifted
    ; into the quotient, then it will be shifted back off the end by this
    ; last RLF. This will automatically raise carry to indicate an error.
    ; The divide will be accurate to quotients of 9-bits, but past that
    ; the quotient and remainder will be bogus and carry will be set.

    bcf bf_zero  ; NZ (in case of overflow error)
    return       ; we are done!

    rlf f_divlo, same    ; C << lo << C
    rlf f_divhi, same    ; C << hi << C
    btfss bf_carry       ; if Carry
     goto DivSkipHiShift ;
    subwf f_divhi, same  ;   hi-=w
    stc                  ;   ignore carry
    return               ;   done
                         ; endif
    subwf f_divhi, same  ; hi-=w
    btfsc bf_carry       ; if carry set
     return              ;   done
    addwf f_divhi, same  ; hi+=w
    clc                  ; clear carry
    return               ; done


Very Good logic..I had been searching for a this type of division algorithm for a long time Thanks a Ton :), -Sid +



file: /Techref/microchip/math/div/16by8lz.htm, 8KB, , updated: 2010/2/24 13:30, local time: 2023/12/7 13:01,

 ©2023 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</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!