From: cclist at
sydex.com
Date: Sun, 20 Jan 2008 20:57:27 -0800
From: Brent Hilpert
Subject: Re: div by 10 on Z80 was RE: Reading Polymorphic ---
(I expect you may have noticed already..) the
subroutine (div10s) is
only called once now, you could inline it to save another 4 bytes.
You can do a bit better with that and by using the carryout to form
the quotient bits:
div10:
xor a
ld de,0fd80h ; largest power of 2 times 10 less then 800d
ld b,07 ; only seven loops needed to finish divide 80
divloop:
add hl,de ; trial subtract
jr c,div10s1 ; carry means trial passed
sbc hl,de ; undo previous add, carry is clear
Hi chuck
I think the restore operation will cause a carry meaning I need
to clear it here with something like:
ora a ; or other carry clearing instruction
If someone has a debugger running, they might check this
but it does look as though the SBC will cause a carry that needs to be
cleared. The result of the add hl,de that didn't create a carry left
a negative number that will be modified back to a positive
number with a subtract. I'm relatively sure that creates a
carry.
Still, it removes one instruction from the outer loop which is 4
cycles time 7. That is 28 clocks!
This is great. Fine tuning this has been fun.
div10:
xor a
ld de,0fd80h ; largest power of 2 times 10 less then 800d
ld b,07 ; only seven loops needed to finish divide 80
divloop:
add hl,de ; trial subtract
jr c,div10s1 ; carry means trial passed
sbc hl,de ; undo previous add, carry is set??
ora a ; clears carry
div10s1:
rl a
add hl,hl ; shift number to reuse same constant, 640 decimal
djnz divloop
add hl,hl ; push full remainder in HL
ret
Dwight
_________________________________________________________________
Shed those extra pounds with MSN and The Biggest Loser!
http://biggestloser.msn.com/