please dont rip this site

SX Microcontroller Math Method

Divide 16 bit int by 8 bit int to 8 bit int and remainder

by Lou Zher

;-[ Div ]--------------------------------------------------------------
;	call	w/: Number in f_divhi:f_divlo
; Returns: Quotient in f_divlo, remainder in f_divhi. W preserved.
;          Carry set if error. Z if divide by zero, NZ if divide overflow.
; Notes:   Works by left shifted subtraction.
;          Size = 29, Speed(w/ call&ret) = 9 cycles if div by zero
;          Speed = 104 minimum, 142 maximum cycles
;          Not compatible with the CARRYX option.

	or	W, #0		; w |= 0 (to test for div by zero)
	setb	C		; set carry in case of error
	snb	Z		; if zero
	ret			;   return (error C,Z)

	call	DivSkipHiShift
	call	DivCode
	call	DivCode
	call	DivCode
	call	DivCode

	call	DivCode
	call	DivCode
	call	DivCode
	call	DivCode

	rl	f_divlo		; C << lo << C

    ; If the first subtract didn't underflow, and the carry was shifted
    ; into the quotient, then it will be shifted back off the end by this
    ; last RLF. This will automatically raise carry to indicate an error.
    ; The divide will be accurate to quotients of 9-bits, but past that
    ; the quotient and remainder will be bogus and carry will be set.

	clrb	Z		; NZ (in case of overflow error)
	ret			; we are done!

	rl	f_divlo		; C << lo << C
	rl	f_divhi		; C << hi << C
	sb	C		; if Carry
	jmp	DivSkipHiShift	;
	sub	f_divhi, W	;   hi-=w
	setb	C		;   ignore carry
	ret			;   done
                         	; endif
	sub	f_divhi, W	; hi-=w
	snb	C		; if carry set
	ret			;   done
	add	f_divhi, W	; hi+=w
	clrb	C		; clear carry
	ret			; done


file: /Techref/scenix/lib/math/div/16by8lz_sx.htm, 2KB, , updated: 2005/10/10 17:01, local time: 2024/7/17 09:38,

 ©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 16 bit int by 8 bit int to 8 bit int and remainder</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?

  PICList 2024 contributors:
o List host: MIT, Site host, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!