;Divide 16bit dividend (dividL,H) by 16bit divisor (divisL,H)
;Result (quotient) in dividL,H and remdrL,H
;processor 16f84 (I'm using 18f2420)
;instructions in [] have been changed by me becaue of build error
Divide
movf divisL,w
iorwf divisH,w
btfsc STATUS,Z ;[skpnz]
goto div0 ;Division by zero !
movlw .16 ;16 bit division
movwf bitcnt
clrf remdrH ;Clear remainder
clrf remdrL
dvloop
bcf STATUS,C ;[clrc] ;Set quotient bit to 0
rlcf dividL ;Shift left dividend and quotient
rlcf dividH ;Msb into carry
rlcf remdrL ;and then into partial remainder
rlcf remdrH
movf divisH,w ;Compare partial remainder and divisor
subwf remdrH,w
btfsc STATUS,Z
goto testgt ;Not equal so test if remdrH is greater
movf divisL,w ;High bytes are equal,compare low bytes
subwf remdrL,w
testgt
btfsc STATUS,C ;[skpc];Carry set if remdr >= divis
goto remrlt
movf divisL,w ;Subtract divisor from partial remainder
subwf remdrL
btfsc STATUS,C ;[skpc];Test for borrow
decf remdrH ;Subtract borrow
movf divisH,w
subwf remdrH
bsf dividL,0 ;Set quotient bit to 1
;Quotient replaces dividend which is lost
remrlt
decfsz bitcnt
goto dvloop
bcf STATUS,Z ;[clrz];Clear error flag (z)
div0
return ;Return with z set if error