piclist 2001\03\05\121349a >
www.piclist.com/techref/microchip/math/index.htm?key=divide
BY : Mike Mansheim email (remove spam text)

>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
divide?

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
reserved.
; 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
; DIVISORHI:DIVISORLO.
;
; Exits with quotient in QUOTIENTHI:QUOTIENTLO, remainder in
; REMAINDERHI:REMAINDERLO.

DIVIDE16:

CLRF    REMAINDERHI     ;CLEAR THE REMAINDER.
CLRF    REMAINDERLO     ;

MOVLW   16              ;WE'RE DIVIDING BY A 16-BIT
DIVISOR.
MOVWF   COUNT           ;

DIVLOOP:

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
INTO REMHI.

BNZ     CHECKLESS       ;IF THE TWO HI-BYTES AREN'T THE
SAME, JUMP

MOVF    DIVISORLO,W     ;OTHERWISE, WE HAVE TO COMPARE
THE
SUBWF   REMAINDERLO,W   ;LO-BYTES.

CHECKLESS:

BNC     NOSUB           ;IF THE SHIFTED PORTION OF THE
DIVIDEND WAS
;LESS THAN THE DIVISOR, JUMP

MOVF    DIVISORLO,W     ;OTHERWISE, REMAINDER =
REMAINDER - DIVISOR.
SUBWF   REMAINDERLO     ;
MOVF    DIVISORHI,W     ;
SKPC                    ;
INCFSZ  DIVISORHI       ;
SUBWF   REMAINDERHI     ; (CARRY'S ALWAYS SET AT THIS
POINT.)

NOSUB:

RLF     QUOTIENTLO      ;IF WE JUST SUBTRACTED, SHIFT A
"1" INTO
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
REMAINDER IS
; IN REMAINDERHI:LO.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

<OF2A97FDCA.1D4167A1-ON86256A06.005E3B2B@graco.com>