Z80 indirect addressing help needed please!

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
Post Reply
Lardo Boffin
Posts: 2169
Joined: Sat Nov 26, 2016 2:42 am

Z80 indirect addressing help needed please!

Post by Lardo Boffin »

Hello all

I have a table of data containing records and want to be able to step through and display them in any order without having to physically re-order them so the obvious way forward is to have a small index table that just holds the address of each record in the order in which I wish to display them.

So for example I would have: -

Index table (starting at 4200) containing: -
1 - 4500
2 - 4650
3 - 4600
4 - 4550

Assuming the records are 50 chars in length that would in effect have the sort order of record 1, 4, 3, 2 assuming the actual records start at 4500: -
4500 record one.....
4550 record two.....
etc.

So far so simples (insert your own meerkat noise).

I'm really struggling with how to read the address held at 4200 (4500 in the example above) and then get the data from that location (the address of the record). I would then on the second go through the loop need to get the data from 4202 to get the location of the second record to display etc.

I have a variable called tablePointer which holds the current location within the index table (so will hold 4200, then 4202 etc.). However if I do something like ld hl, (tablePointer ) I just end up with the location 4500 in HL. How do I then start reading the memory at 4500?

This is all really easy to do in 6502 (which I am more used to) as you have many addressing modes including:-
. Immediate - LDA #50 (load 50 into the accumulator)
. Direct - LDA &5000 (load the contents of the location &5000 into the accumulator, so if &5000 contained 45, 45 would be loaded into A)
. Indirect - LDA (&5000) (in this indirect addressing mode it is assumed &5000 and &5001 form an address - this address is retrieved and then it loads the data pointed to by this address into A, so if &5000 held the address &6000 and &6000 held the value 33, this would load 33 into A)

How do I do this in Z80 on the ZX81? I am running in slow mode so cannot use IX. I tried switching to fast mode before calling the routine to step through the data but then calling slow doesn't seem to do much - the screen is blank and stays that way.

I have tried using IY but this seems to crash out the zeddy as well. I thought this was safe to use? It seems to go horribly wrong when the screen update NMI occurs (based on stepping through the code in EightyOne debug mode).

Any advice appreciated!

Lardo
ZX80
ZX81 iss 1 (bugged ROM, kludge fix, normal, rebuilt)
TS 1000 iss 3, ZXPand AY and +, ZX8-CCB, ZX-KDLX & ChromaSCART
Tatung 81 + Wespi
TS 1500 & 2000
Spectrum 16k (iss 1 s/n 862)
Spectrum 48ks plus a DIVMMC future and SPECTRA
User avatar
Andy Rea
Posts: 1606
Joined: Fri May 09, 2008 2:48 pm
Location: Planet Earth
Contact:

Re: Z80 indirect addressing help needed please!

Post by Andy Rea »

the way i usually appraoch this kind of thing is
works for tables of 128 16bit entries

Code: Select all

LD   A , index
ADD  A , A    ; double it 
LD   L , A 
LD   H,HighByteOfTableAddress

LD   E , (HL)
INC  HL
LD   D , (HL)   ; DE now contains value from table

hope this helpa

Andy
what's that Smell.... smells like fresh flux and solder fumes...
Lardo Boffin
Posts: 2169
Joined: Sat Nov 26, 2016 2:42 am

Re: Z80 indirect addressing help needed please!

Post by Lardo Boffin »

Andy Rea wrote: Fri Apr 07, 2017 7:41 pm the way i usually appraoch this kind of thing is
works for tables of 128 16bit entries

Code: Select all

LD   A , index
ADD  A , A    ; double it 
LD   L , A 
LD   H,HighByteOfTableAddress

LD   E , (HL)
INC  HL
LD   D , (HL)   ; DE now contains value from table

hope this helpa

Andy
Thanks Andy. That looks far neater than the code I am currently using to step through the data. I assume the low byte of the table address should start at 0 in order for the index process to work?

If I'm reading this correctly (forgive me if I'm not - z80 is still new to me) at the end of this process I will have the value of the 'index' no. record in DE? (as this is what is stored in my lookup or index table).

So in my case if the index is 2 I will have in effect the address of the second record (4650 in my example) in DE? How do I then get the data from the memory location 4650? Hope that question makes sense - I am basically looking up an address in table 1 to read data from table 2 at that address.

Thanks

Phil
ZX80
ZX81 iss 1 (bugged ROM, kludge fix, normal, rebuilt)
TS 1000 iss 3, ZXPand AY and +, ZX8-CCB, ZX-KDLX & ChromaSCART
Tatung 81 + Wespi
TS 1500 & 2000
Spectrum 16k (iss 1 s/n 862)
Spectrum 48ks plus a DIVMMC future and SPECTRA
User avatar
Andy Rea
Posts: 1606
Joined: Fri May 09, 2008 2:48 pm
Location: Planet Earth
Contact:

Re: Z80 indirect addressing help needed please!

Post by Andy Rea »

Yes the index starts at 0

If it's a single byte at the destination address one could simply

LD. A , (DE)

So A would now hold the contents of the memory address pionted to by the contents of the indexable array

if it's a 2 byte then you could

EX. DE , HL
LD. E ,(HL)
INC. HL
LD D ,(HL)

And here DE would hold a 2 bute value

Regards andy
what's that Smell.... smells like fresh flux and solder fumes...
Lardo Boffin
Posts: 2169
Joined: Sat Nov 26, 2016 2:42 am

Re: Z80 indirect addressing help needed please!

Post by Lardo Boffin »

That looks awesome thanks Andy. It was EX I was needing then!

I will give it a go tonight.

Lardo
ZX80
ZX81 iss 1 (bugged ROM, kludge fix, normal, rebuilt)
TS 1000 iss 3, ZXPand AY and +, ZX8-CCB, ZX-KDLX & ChromaSCART
Tatung 81 + Wespi
TS 1500 & 2000
Spectrum 16k (iss 1 s/n 862)
Spectrum 48ks plus a DIVMMC future and SPECTRA
Lardo Boffin
Posts: 2169
Joined: Sat Nov 26, 2016 2:42 am

Re: Z80 indirect addressing help needed please!

Post by Lardo Boffin »

Didn't have the energy last night but updated my code tonight and it works a treat thanks Andy!
ZX80
ZX81 iss 1 (bugged ROM, kludge fix, normal, rebuilt)
TS 1000 iss 3, ZXPand AY and +, ZX8-CCB, ZX-KDLX & ChromaSCART
Tatung 81 + Wespi
TS 1500 & 2000
Spectrum 16k (iss 1 s/n 862)
Spectrum 48ks plus a DIVMMC future and SPECTRA
Post Reply