SX Microcontroller Math Method

Divide 24 bit int by 16 bit int to 24 bit int

by Nikolai Golovchenko

;Inputs:
;   Dividend - AARGB0:AARGB1:AARGB2 (0 - most significant!)
;   Divisor  - BARGB0:BARGB1
;Temporary:
;   Counter  - LOOPCOUNT
;   Remainder- REMB0:REMB1
;Output:
;   Quotient - AARGB0:AARGB1:AARGB2
;   Remainder- REMB0:REMB1
;
;       Size: 28
; Max timing: 4+24*(6+6+4+3+7)-2+3+3=632 cycles (with return)
; Min timing: 4+24*(6+6+6+7)-2+3+3=608 cycles (with return)
;

FXD2416U:
	clr	REMB0
	clr	REMB1
	mov	W, #24
	mov	LOOPCOUNT, W
LOOPU2416
	rl	AARGB2		;shift left divider to pass next bit to remainder
	rl	AARGB1		;and shift in next bit of result
	rl	AARGB0

	rl	REMB1		;shift carry into remainder
	rl	REMB0

	rl	LOOPCOUNT		;save carry in counter

	mov	W, BARGB1		;substract divisor from remainder
	sub	REMB1, W
	mov	W, BARGB0
	sb	C
	movsz	W, ++BARGB0
	mov	W, REMB0-w	;keep that byte in W untill we make sure about borrow

	snb	C		;if no borrow
	setb	LOOPCOUNT.0	;set bit 0 of counter (saved carry)

	snb	LOOPCOUNT.0	;if no borrow
	jmp	UOK46LL		;jump

	mov	W, BARGB1		;restore remainder if borrow
	add	REMB1, W
	mov	W, REMB0		;read high byte of remainder to W
        				;to not change it by next instruction
UOK46LL
	mov	REMB0, W		;store high byte of remainder
	clrb	C		;copy bit 0 to carry
	rr	LOOPCOUNT		;and restore counter
	decsz	LOOPCOUNT		;decrement counter
	jmp	LOOPU2416		;and repeat loop if not zero

	rl	AARGB2		;shift in last bit of result
	rl	AARGB1
	rl	AARGB0
	ret


Interested: