> In general, for a loop with more than 256 steps, I use the following
> code:
>
> ld bc,ITERATIONS
> LOOP: push bc
> ...
> (whatever must be done ITERATIONS times)
> ...
> pop bc
> dec bc
> ld a,b
> or c
> jr nz,LOOP
Faster is:
ld b,ITERATIONS mod 256
ld c,(ITERATIONS-1) / 256 +1
LOOP:
{do whatever you want to do, just don't mess with BC}
djnz LOOP
dec c
jp nz,LOOP
You use the 8-bit djnz for a 16-bit loop (much faster than dec bc : ld a,b : or
c : jp nz,LOOP, difference is 11 T-states). The additional '16-bit code' (still
faster than the dec bc : ld a,b : or c : jp nz,LOOP sequence, to be precise 2 T
states :)) is only executed once every 256 loops. Plus, it doesn't modify the A
register, which can be quite useful.
If you want to use a variable loop count (number of loops provided in DE), you
can use the following code instead of the initial ld b,bla and ld c,bla
instructions:
dec de
inc d
inc e
ld b,e
ld c,d
Very fast code (only 22 t-states), and only executed once.
Greetings,
~Grauw
--
For info, see http://www.stack.nl/~wynke/MSX/listinfo.html