ON 20060918@12:55:10 PM at page:
http://www.sxlist.com/techref/scenix/lib/math/div/16by16ng.htm#38977.9129398148
James Newton[JMN-EFP-786] published post 38977.9129398148
/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, W
div16by16loop
;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
|Delete 'P-' before: '' but after: '