Z80 Divide by 10

dwight elvey dkelvey at hotmail.com
Fri Jan 25 23:20:28 CST 2008

> From: cclist at sydex.com
> On 25 Jan 2008 at 12:00, cctalk-request at classiccmp.org wrote:
>> I figure that one might do part of the multiplication and then
>> right shift the result some since were are going to truncate the
>> LSBs anyway. The first result is expected to error some. With
>> the correct value for the multiply, the error will always be on
>> the low side, keeping the error calculation simple. The largest
>> error seems to grow linearly so even with some truncation,
>> one should be able to hit 10K or so with only 1 or2 conditionals,
>> using a large fractional munber to multiply.
>> I don't think I'd ever use this but it was fun to think about.
> I suspected that this is what you might be doing (that ADD HL,H
> really had me wondering), but I wonder if your method will hold
> together for accuracy or be faster than a simple unrolled 10 bit
> divide. Remember that without the need for an iteration test you
> can use BC to hold the scaled +10 and use a DAD instead of an SBC,
> shaving a byte from the loop.
> Cheers,
> Chuck

 The ADD HL,H was a mistake. It should have been ADD HL,HL.
Here is my loopless and conditional less version:

; divides 0-799 by 10
; hl is input dividend
; a= qoutient
; h=remainder

ld d,h
add hl,hl
add hl,hl
add hl,de
add hl,hl
ld b,h
ld c,l
add hl,hl
add hl,hl
add hl,bc
add hl,de
ld a,h         ; add little more accuracy
add a,#16d ; tweak value
ld c,a
ld b,#0
add hl,bc
ld a,h       ; quo*2
rra          ; quo=a
ld l,a
ld c,a
ld h,b
add hl,hl
add hl,hl
add hl,bc
add hl,hl  ; quo*10 to calc Remainder
ex de,hl
sbc hl,de ; l= remainder

230 clock cycles and no conditionals

Connect and share in new ways with Windows Live.

More information about the cctech mailing list