Re: A86: Re: Efficiency Problem


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

Re: A86: Re: Efficiency Problem




>I don't understand from here...
>>   add a,a
>>   ld e,a
>>   ld d,0
>>   add hl,de
>to here. What does this do? I'm guessing it has something to do with
getting
>the pointer into hl? But why is de in there?
>
>>   call _ldhlind


Remember that addresses are 16bit.  This means that W1, W2, etc each take up
2 bytes apiece.  Remember the table:

big_fat_table .dw W1, W2, ...

W1:
    ...
    ret

W2:
    ...
    ret

etc..

The code that you're having trouble with is the code that points hl to the
correct address.  First off, HL is set to the address of the table
containing the series of 2byte entries.

ld    hl, big_fat_table

Now lets say you want W3.  That's the 3rd one down and it starts on the 6th
byte from where HL currently points.  What if you want the 5th one?  That's
10 bytes away from where HL points.  Get it?  Each entry is 2bytes.  So
basically, if you want the xth entry (Wx), then it would be x * 2 bytes
offset of the start of the table.  The code goes to entry A.  So right now,
A holds the W# that we want to go to.

add a, a    ; multiply by 2

There, now A is the correct number of bytes offset.  Note that the largest
number you could input is 127 otherwise A would overflow.

ld    d, 0
ld    e, a

The above 2 lines of code sets DE to equal A.
Then:

add    hl, de

moves HL up DE bytes.  If you wanted W3, then HL now equals HL + 6.

The table concept is something you really have to know if you want to keep
your program small and efficient.  Even though the table will add to your
program size, in most cases the code saved is greater.  Heck, I use tables
for just about everything that I can.  I don't have to write so much code if
I do so =).  A very good use of a table is when you have a bunch of data
that isn't consecutive.

For the sake of argument, let's say pixel plotting.  If you want to plot a
bunch of dots all over the sky at specific points, you could put all of the
X and Y's in a table and then make a loop run thru the table and plot each
point.  This is much more efficient than writing "call putpixel" a hundred
times.

I've used tables for flagging also although I can't remember exactly how I
did it.

toggle_table .db 1, 0

toggle:
    ld a, (some_flag)
    ld hl, toggle_table
    ld b, 0
    ld c, a
    add hl, bc
    ld a, (hl)
    ld (some_flag), a
    ret

Hmm.  Someone's probably going to say that there's some single instruction
that does that also =)  When I did it before in my actual program it was
worth putting here, but I don't think it was that.  I remember using some
2-3 element table that ended up saving me from using a single CP.  Anyways,
you get the idea.  See how it toggled the flag without even using a jump ?
Please don't nail me too bad on this one guys, I only put that there for the
sake of example.  The original wasn't an on-off toggle like that.

-Justin Karneges [Infiniti]