# SXMicrocontrollerMathMethod

## Divide 48 bit int by 23 bit int to 48 bit int

```Dividend	DS 6
Divisor		DS 3
Temp		DS 3
BitCount	DS 1

;**********************************************************************
;
;48 BY 23 BIT UNSIGNED DIVISION (Non-restoring method)
;
;**********************************************************************
;
;     Input: Dividend, 6 bytes (Dividend+0 MSB, ... , Dividend+5 LSB)
;            Divisor, 3 bytes (Divisor+0 MSB, ... , Divisor+2 LSB)
;
;    Output: Dividend, 6 bytes (Dividend = Dividend / Divisor)
;            Z  - division by zero
;            NZ - okay
;
; Temporary: Temp, 3 bytes - remainder
;            BitCount, 1 byte - counter
;
;      Size: 52 instructions
;
;Max timing: 3+5+6+28*48-2+7+3=1366 cycles
;
;**********************************************************************

DIVIDE_48by23

; Test for zero division
mov	W, Divisor
or	W, Divisor+1
or	W, Divisor+2
snz
ret			; divisor = zero, not possible to calculate -
; return with set zero flag (Z)

; clear remainder
clr	Temp
clr	Temp+1
clr	Temp+2

mov	W, #48		; initialize bit counter
mov	BitCount, W

stc			; set carry to perform subtraction
; first in the following loop
DIVIDE_LOOP_48by23
; shift in next result bit and
; shift out next bit of dividend
rl	Dividend+5
rl	Dividend+4
rl	Dividend+3
rl	Dividend+2
rl	Dividend+1
rl	Dividend
; shift in highest bit from dividend through carry in temp
rl	Temp+2
rl	Temp+1
rl	Temp

mov	W, Divisor+2	; load w with LSB of divisor
; to use it in subtraction or

; If previous result bit was set (subtraction was performed
; subtract divisor from remainder,
; else add divisor to remainder
sb	Dividend+5.0

; subtract 23 bit divisor from 24 bit temp,
; divisor LSB is in w
sub	Temp+2, W	; subtract LSB

mov	W, Divisor+1	; get middle byte
sc			;  if overflow ( from prev. subtraction )
movsz	W, ++Divisor+1	; incresase source
sub	Temp+1, W	; and subtract from dest.

mov	W, Divisor	; get top byte
sc			;  if overflow ( from prev. subtraction )
movsz	W, ++Divisor	; increase source
sub	Temp, W		; and subtract from dest.

; add 23 bit divisor to 24 bit temp
; divisor LSB is in w

mov	W, Divisor+1	; middle byte
snc 			; check carry for overflow from previous addition
movsz	W, ++Divisor+1	; if carry set we add 1 to the source

mov	W, Divisor	; MSB byte
snc			; check carry for overflow from previous addition
movsz	W, ++Divisor

DIVIDE_NEXT_48by23
; carry contains next result bit
decsz	BitCount	; decrement loop counter (carry not influenced)
jmp	DIVIDE_LOOP_48by23 ; another run

; finally shift in the last carry
rl	Dividend+5
rl	Dividend+4
rl	Dividend+3
rl	Dividend+2
rl	Dividend+1
rl	Dividend

clz                     ; done, clear zero flag (NZ) and
ret			; return

;**********************************************************************

```

 file: /Techref/scenix/lib/math/div/48by23ng_sx.htm, 3KB, , updated: 2004/6/10 14:40, local time: 2024/7/17 08:56, owner: NG--944, TOP NEW HELP FIND:  44.213.60.33:LOG IN

 ©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! SX Microcontroller Math Method - Divide 48 bit int by 23 bit int to 48 bit int

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.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

 PICList 2024 contributors: o List host: MIT, Site host massmind.org, 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.

.