# SXMicrocontrollerMathMethod

## Divide 16 bit int by 16 bit int to 16 bit int with 16 bit int remander

```; uint16 x = uint16 x / uint16 y
;
; Input:
;  x, x+1 - 16 bit unsigned integer dividend (x - lsb, x+1 - msb)
;  y, y+1 - 16 bit unsigned integer divisor
; Output:
;  x, x+1 - 16 bit unsigned integer quotient
; Temporary:
;  counter
;  x+2, x+3 - 16 bit remainder
;  temp - remainder extension
; Size: 36 instructions
; Max timing: 6+16*(5+14+4)-2+2+3=377 cycles

div16by16
clr	x+2	;clear
clr	x+3	;remainder
div16by16loopinit
clr	temp	;clear remainder extension
mov	W, #16
mov	counter, W
stc		;first iteration will be subtraction
div16by16loop
;shift in next result bit and shift out next
;dividend bit to remainder

rl	x	;shift lsb
rl	x+1	;shift msb
rl	x+2
rl	x+3
rl	temp

mov	W, y
sb	x.0

;subtract divisor from remainder
sub	x+2, W
mov	W, y+1
sc
movsz	W, ++y+1
sub	x+3, W
mov	W, #1
sc
sub	temp, W
jmp	div16by16next

;add divisor to remainder
mov	W, y+1
snc
movsz	W, ++y+1
mov	W, #1
snc

div16by16next
;carry is next result bit
decsz	counter
jmp	div16by16loop

;shift in last bit
rl	x
rl	x+1
ret

```

• /techref/scenix/member/NG--944/index.htm An improved version: smaller, faster, one fewer temp, and computes the remainder.
```; uint16 x = uint16 x / uint16 y;; Input:;  x, x+1 - 16 bit unsigned integer dividend (x - lsb, x+1 - msb);  y, y+1 - 16 bit unsigned integer divisor; Output:;  x, x+1 - 16 bit unsigned integer quotient;  x+2, x+3 - 16 bit unsigned integer remainder; Temporary:;  counter; Size:  28 instructions; Max timing: 4+16*(4+15+4)-2+2+3=375 cycles

div16by16        clr     x+2     ;clear        clr     x+3     ;remainder        mov     W, #16        mov     counter, Wdiv16by16loop        ;shift in next result bit and shift out next        ;dividend bit to remainder        rl      x        rl      x+1        rl      x+2        rl      x+3        ;subtract divisor from remainder        mov     W, y        sub     x+2, W        mov     W, y+1        sc        movsz   W, ++y+1        sub     x+3, W
;if no borrow, set next quotient bit        snc        jmp div16by16next
;if borrow, clear next quotient bit and restore the remainder
;add divisor to remainder        mov     W, y        add     x+2, W        mov     W, y+1        snc        movsz   W, ++y+1        add     x+3, W        clc
div16by16next
decsz   counter        jmp     div16by16loop
;shift in last result bit        rl      x        rl      x+1
ret
```
• +

 file: /Techref/scenix/lib/math/div/16by16ng.htm, 3KB, , updated: 2006/9/18 13:00, local time: 2020/9/29 20:37, TOP NEW HELP FIND:  3.237.71.23:LOG IN

 ©2020 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 16 bit int by 16 bit int with 16 bit remander

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 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? "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" "No. But I'm interested. me at when this page is expanded."

### Welcome to sxlist.com!

Site supported by
& kind contributors
just like you!

(here's why

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

.