Re: A82: DIVsion and such..


[Prev][Next][Index][Thread]

Re: A82: DIVsion and such..



On Sat, 16 Aug 1997 "Salix" <Salix@xpress.se> writes:

>I'm REALY new to z80 programming and as such i havent found a way to do
>division , "real" or with a "rest".. anyone got a routine or the
>instruction ? :)

If you just want to divide by powers of 2, you can use the shift
instructions.  Each shift to the right divides by 2.  However, if you
want to divide by a variable, or by constants that aren't powers of 2,
you need a real division routine.

One of the best division routines there is is in the calculator's ROM!
To divide a 16-bit integer by 10, put the integer in HL and call
UNPACK_HL.  If you want to divide HL by anything else, load the divisor
into A and call UNPACK_HL+2.  This will return the quotient in HL and
the remainder in A.  

I have disassembled this routine and it seems to me that it is about the
fastest one possible.  Including the time taken in the call and return,
this routine will take 827 to 875 clock cycles.  The worst case occurs
when dividing 65535 by 1.

You could speed it up by unwinding the loop.  This could save up to 211
clock cycles, but take about 150 bytes of memory.  However, if it were in
your program and you accessed it with CALL_(), it would be nearly
pointless anyway since that takes almost as many clock cycles as you
might save!  If 
you didn't need to preserve the BC register, your version could save a
few cycles by not pushing/popping it.  You could also put it right inside
the
code that uses it and save a little bit of time by not having the
CALL/RET,
but this would really enlarge your program if you used it from many
places.

--
Patrick Davidson (ariwsi@juno.com)
Visit my home page!  Stuff for Amiga, TI-85, TI-92, even DOS!
http://www.calweb.com/~kwdavids/patrick/
http://www.toptown.com/hp/ariwsi/


Follow-Ups: References: