: Divide 10 bits by 10.

dwight elvey dkelvey at hotmail.com
Sun Jan 20 20:03:30 CST 2008


> From: cclist at sydex.com
>
>> Date: Sat, 19 Jan 2008 13:01:25 -0800
>> From: dwight elvey 
>
>
>> I'm not sure how effective that would be in Z80 code. It looks
>> like I'd at least need to do 32 bit coding to keep track of things.
>> I'd still need to calculate the remainder when done( I need both ).
>> The maximum sector index would be 800 decimal.
>
> Ah, then it's easy:
>
> ;* Divide 10 bits by 10.
> ; ---------------------
> ;
> ; Input in (HL)
> ;
> ; Quotient in (A), remainder in (L).
> ;
>
>
> Div10by10:
> ld de,(10 shl 6) ; divisor
> ld bc,0701h ; iteration count + 1 for xor
> xor a ; quotient
> Dtbt2:
> xor c ; assume set
> sbc hl,de ; subtract
> jr nc,Dbt4 ; if no carryout
> add hl,de ; add back
> xor c ; clear the bit
> Dbt4:
> rr d ; (carry is clear)
> rr e ; shift divisor
> add a,a ; shift quotient
> djnz dbt2 ; loop
> rra ; correct quotient
> ret ; a = quotient, l = remainder
>
> That will do it for values of a divident up to 1023. The algorithm
> can be extended somewhat by shifting the value of 10 left more places
> and increasing the number of iterations.
>
> I haven't tested it out on real hardware, but it should work. I've
> also got a divide 32-bits by 10 along the same line, if you're
> interested. It's fairly deterministic in terms of cycles; i.e.,
> there's not a lot of difference in timing between a dividends of 0
> and 799.
>
> Cheers,
> Chuck

Hi Chuck
 This is almost the same as the one Pete and I came up with.
I think ours is a little faster with shifting the dividen instead of the divisor.
We shift with a add hl,hl rather than the two rr's. The inc and dec
are slightly more efficient than the xor's because I don't need to load
the constant 1.
 It is good to see that your solution is similar to ours, It makes me
think it is close to optimal.
Dwight

_________________________________________________________________
Need to know the score, the latest news, or you need your Hotmail®-get your "fix".
http://www.msnmobilefix.com/Default.aspx


More information about the cctech mailing list