16fxdu/8 divide routine
Mike Mansheim email (remove spam text)
>http://home.netcom.com/~fastfwd/answers.html#PIC00094 from Andrew
>Warren: 16-bit by 8 - site unreachable
Here is the info from Andrew's site:
I saw the 8-bit divide routine in Question #64, but I can't figure out how
to extend it to divide one 16-bit number by another. How do you do a 16-bit
Like this. The following routine is written for clarity, not absolute
speed, but it should still perform relatively quickly:
; 16-Bit Divide. Written by Andy Warren.
; Copyright (C) 1996 Fast Forward Engineering. All rights
; Permission is hereby granted for any non-commercial use,
; so long as this copyright notice remains intact.
; Enter with Dividend in DIVIDENDHI:DIVIDENDLO, divisor in
; Exits with quotient in QUOTIENTHI:QUOTIENTLO, remainder in
CLRF REMAINDERHI ;CLEAR THE REMAINDER.
CLRF REMAINDERLO ;
MOVLW 16 ;WE'RE DIVIDING BY A 16-BIT
MOVWF COUNT ;
RLF DIVIDENDLO ;SHIFT DIVIDEND LEFT 1 BIT INTO
RLF DIVIDENDHI ;REMAINDERHI:REMAINDERLO.
RLF REMAINDERLO ;
RLF REMAINDERHI ;
MOVF DIVISORHI,W ;COMPARE THE DIVISOR TO THE
PORTION OF THE
SUBWF REMAINDERHI,W ;DIVIDEND THAT'S BEEN SHIFTED
BNZ CHECKLESS ;IF THE TWO HI-BYTES AREN'T THE
MOVF DIVISORLO,W ;OTHERWISE, WE HAVE TO COMPARE
SUBWF REMAINDERLO,W ;LO-BYTES.
BNC NOSUB ;IF THE SHIFTED PORTION OF THE
;LESS THAN THE DIVISOR, JUMP
MOVF DIVISORLO,W ;OTHERWISE, REMAINDER =
REMAINDER - DIVISOR.
SUBWF REMAINDERLO ;
MOVF DIVISORHI,W ;
INCFSZ DIVISORHI ;
SUBWF REMAINDERHI ; (CARRY'S ALWAYS SET AT THIS
RLF QUOTIENTLO ;IF WE JUST SUBTRACTED, SHIFT A
RLF QUOTIENTHI ;THE QUOTIENT. OTHERWISE,
SHIFT A "0".
DECFSZ COUNT ;HAVE WE SHIFTED ENOUGH BITS?
GOTO DIVLOOP ;IF NOT, LOOP BACK.
; AT THIS POINT, THE QUOTIENT IS IN QUOTIENTHI:LO AND THE
; IN REMAINDERHI:LO.
http://www.piclist.com hint: The PICList is archived three different
ways. See http://www.piclist.com/#archives for details.
See also: www.piclist.com/techref/microchip/math/index.htm?key=divide
You must be a member of the
piclist mailing list
(not only a www.piclist.com member) to post to the