please dont rip this site

SX Microcontroller Math Method

Divide 24 bits by 16

Andrew David (akdavid at, Software Manager, Ultronics Ltd, Cheltenham says:

This division routine uses the standard binary long-division algorithm. The loop iterates once for each bit in the numerator, so it goes round 24 times. For each loop the numerator (sorry, but I can never remember which is the dividend and the divisor - I guess the divisor is the denominator, but I'd hate to get it wrong) is left shifted 1 bit into the remainder, then the remainder is compared with the denominator. If the remainder is greater than the denominator, the denominator is subtracted from the remainder and a 1 is shifted into the quotient, otherwise a 0 is shifted into the quotient. If you can't see how the routine works, try running through an example on paper, in binary, then think about how you'd write the routine.
; DIV24_16u
;       Divides a 24bit number by a 16bit number. Unsigned.
;       Inputs:
;		24-but numerator in ACCcLO:ACCdHI:ACCdLO
;		16-bit denominator in ACCbHI:ACCbLO
;       Outputs:
;		24-bit quotient in ACCcLO:ACCdHI:ACCdLO
;		16-bit rem in ACCaHI:ACCaLO
;	Locals used:
;		R5Hi
; Inputs are not preserved.
; No timing analysis performed.
; Andrew David, Software Manager, Ultronics Ltd, Cheltenham
; akdavid at

	mov	W, #24	; for 24 shifts
	mov	R5Hi, W	;

	clr	ACCaHI	; clear remainder.
	clr	ACCaLO	;

d2416lp:rlcf    ACCdLO,f        ; build up remainder.
        rlcf    ACCdHI,f        ;
        rlcf    ACCcLO,f        ;
        rlcf    ACCaLO,f        ;
        rlcf    ACCaHI,f        ;

; remainder is 16-bit, but may have spilled over into carry.

	sb	ALUSTA.C	; check for remainder spill into carry.
	jmp	d2416s	;

        movfp   ACCbLO,WREG     ;
        subwfb  ACCaLO,f        ; Carry bit is the 17th bit of this
        movfp   ACCbHI,WREG     ;
        subwfb  ACCaHI,f        ;
	setb	ALUSTA.C	; bit is known to be zero here.
	jmp	d2416ns	;

d2416s: movfp   ACCbLO,WREG     ; Compare remainder with divisor.
	mov	W, ACCaLO-w	;
        movfp   ACCbHI,WREG     ;
        subwfb  ACCaHI,w        ;
	sb	ALUSTA.C	;
	jmp	d2416ns		; (remainder < divisor), shift in a '0'
        movfp   ACCbLO,WREG     ; The remainder is larger, so subtract
	sub	ACCaLO, W	; ... divisor FROM the remainder and
        movfp   ACCbHI,WREG     ; ... shift a '1' into the quot.
        subwfb  ACCaHI,f        ;

d2416ns:decfsz  R5Hi,f          ; check all bits
	jmp	d2416lp	;
        rlcf    ACCdLO,f        ; shift in final quotient bit.
        rlcf    ACCdHI,f        ;
        rlcf    ACCcLO,f        ;


file: /Techref/SCENIX/lib/math/div/24by16ad_sx.htm, 3KB, , updated: 2004/6/10 14:40, local time: 2024/5/30 02:31,

 ©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=""> SX Microcontroller Math Method - Divide 24 bits by 16</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!

Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.

Welcome to!