I've read through 28 days 2 paragraph snippet on arrays and a few forum posts but I still don't fully understand them.
So from what I understand, to create an array just do something like this

Code:

; Totaly random unused of data im using for an example
Array:
   .db   7,0,0,0,0,0,0,0,0,9,0,0,0
   .db   0,0,0,0,0,2,0,0,0,0,0,3,0
   .db   0,0,0,0,0,2,6,0,0,0,0,0,6
   .db   2,0,4,0,0,0,0,0,0,8,0,0,0
   .db   4,0,0,0,1,1,2,8,0,0,0,0,0

But im not 100% on how to access this array. What I'm trying to do is store a map in an array, and then read through the array, printing some predertimined char for each number in the array (0=' ', 1='+', simple stuff).
But I have no clue how to access the array, I assume its something like

Code:

LD HL,(Array)+2

or something, but if someone could clear this up It'd be appreciated Good Idea
In fact it's just a large list. Since you are using 1-byte values, you need register A and not HL, and you can access elements with
Code:
ld a, (Array + N)
where N is the element index. Hope this helps Smile
There's two ways to access this. First is to create a pointer to the Array. Second is use the Array label (which is a pointer itself).

Pointer to Array in HL

Code:
ld hl, Array
ld bc, [Offset in Data]
add hl, bc
ld a, (hl) // your byte is in a


Use Array Itself

Code:
ld a, (Array + Offset)



Sometimes, you need to use the first example. For instance, if you need to iterate through the data or a segment of it within a loop, you will need the first example, because in the ld a, (Array + Offset) example, Offset must be defined at compile time and cannot be a variable.


Edit:
In your example: ld hl, (Array)+2, that would load into HL, the value at Array, and then add 2 to it.
So are these two arrays equivalent?

Code:

Array1:
 .dw 0,1,2,3,4,5,6,7,8,9

Array2:
 .dw 0,1,2,3,4
 .dw 5,6,7,8,9


Are elements in 2D arrays just stored one after another in memory?If so would this run and exit correctly or would this do what the comment says?

Code:

LD A,(Array1 + 6)
LD B,A
LD A,(Array2 + 6)
CP B
RET z
; If not zero read through fancy important calculator code and crash everything


Edit:
Also, I'm having an issue where elements in my array aren't correct, Let's say I have this;

Code:

LD A,(Map + N)
BCALL(_PutMap)

Map:
   .dw $20,$21,$22,$23,$24,$25,$26,$27,$28


So Would the first element in the array be N=0 or N=1?
Also, can I do what the code above is doing? or is my problem that When N=0 in loading $2 into A and when N=1 in loading $0 into A?
You are using .dw when you should be using .db
.dw is 'define word' - regarding z80 this is a 16-bit, or 2 byte word. If you want to get any specific word it would be (Map + (N * 2)) since each element in your array would be 2 bytes if defined this way.

.db is 'define byte' - as Mateo suggests, would suit (Map + N). And you would indeed stat at N=0, since this would leave you with just 'Map', which is the location of your array.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement