LinuxMan
18.11.2003, 01:47
;************************************************* ***********************
;* *
;* unsigned division 16 bit *
;* fast *
;* *
;************************************************* ***********************
udiv16rnd:
mov a, op2l
mov i, op2h
lsr i ;2.operand / 2
ror a
add op1l, a ;1.Op = 1.Op + 2.Op / 2
adc op1h, i
;cycles: 94/186 (min/max)
;words: 33
udiv16:
tst op2h
brne udi4
ldi i, 16 ;divisor < 256
udi1: lsl op1l ;loop: 16 * 11 - 1 = 175 cycles
rol op1h
rol op2h
brcs udi2
cp op2h, op2l
brcs udi3
udi2: sub op2h, op2l
inc op1l
udi3: dec i
brne udi1
mov op2l, op2h ;store remainder
clr op2h
ret
udi4: ldi i, 8 ;divisor >= 256
clr a
udi5: lsl op1l ;loop: 8 * 12 - 1 = 95 cycles
rol op1h
rol a
cp op1h, op2l ;test
cpc a, op2h
brcs udi6
sub op1h, op2l
sbc a, op2h
inc op1l
udi6: dec i
brne udi5
mov op2l, op1h ;store remainder
mov op2h, a
clr op1h
ret
;------------------------------------------------------------------------
;* *
;* unsigned division 16 bit *
;* fast *
;* *
;************************************************* ***********************
udiv16rnd:
mov a, op2l
mov i, op2h
lsr i ;2.operand / 2
ror a
add op1l, a ;1.Op = 1.Op + 2.Op / 2
adc op1h, i
;cycles: 94/186 (min/max)
;words: 33
udiv16:
tst op2h
brne udi4
ldi i, 16 ;divisor < 256
udi1: lsl op1l ;loop: 16 * 11 - 1 = 175 cycles
rol op1h
rol op2h
brcs udi2
cp op2h, op2l
brcs udi3
udi2: sub op2h, op2l
inc op1l
udi3: dec i
brne udi1
mov op2l, op2h ;store remainder
clr op2h
ret
udi4: ldi i, 8 ;divisor >= 256
clr a
udi5: lsl op1l ;loop: 8 * 12 - 1 = 95 cycles
rol op1h
rol a
cp op1h, op2l ;test
cpc a, op2h
brcs udi6
sub op1h, op2l
sbc a, op2h
inc op1l
udi6: dec i
brne udi5
mov op2l, op1h ;store remainder
mov op2h, a
clr op1h
ret
;------------------------------------------------------------------------