Last question for today:
How do I use Indexed Addressing?
At the top of my code, in my RAM section, starts with something like ORG RAMStart, I am "declaring" some variables/registers, and at the end I want to have an "array" of registers, which could be a few or a lot of registers.
VAR1DS1
VAR2DS1
...
ARRAY:
ARRAY1DS1
ARRAY2DS1
ARRAY3DS1
ARRAY4DS1
...
I think putting a colon next to the first "ARRAY" is just making it a label, not sure.
So when I want to store something to the 2nd ARRAY down, would I use:
STAARRAY, X
assuming the accumulator has what I want to store, and X is 2.
What if the "array" ends up being 15 registers:
LDAARRAY, X
X is 15 to get the data that far down?
Does then name of the following ARRAY1, ARRAY2 even matter?
Or do I just use
ARRAYDS16
knowing I'll need 4-16 registers??
I'm really having a hard time understanding the way Assembly uses variables/registers compared to an HLL like Java
Thanks in advance,
-Mike
LiveMike wrote:Last question for today:
How do I use Indexed Addressing?
At the top of my code, in my RAM section, starts with something like ORG RAMStart, I am "declaring" some variables/registers, and at the end I want to have an "array" of registers, which could be a few or a lot of registers.
VAR1DS1
VAR2DS1
...
ARRAY:
ARRAY1DS1
ARRAY2DS1
ARRAY3DS1
ARRAY4DS1
...
I think putting a colon next to the first "ARRAY" is just making it a label, not sure.
Having text with no whitespace to the left of it makes it a label, the colon is optional.
So when I want to store something to the 2nd ARRAY down, would I use:
STAARRAY, X
assuming the accumulator has what I want to store, and X is 2.
Yes, but don't forget the first item is at offset zero.
What if the "array" ends up being 15 registers:
LDAARRAY, X
X is 15 to get the data that far down?
x = 14 is the last item of a array of size 15, as above.
Does then name of the following ARRAY1, ARRAY2 even matter?
No, if you want to directly reference an item in the array without using indexed addressing you can use ARRAY+2
Or do I just use
ARRAYDS16
knowing I'll need 4-16 registers??
Normally this is what you would do.
I'm really having a hard time understanding the way Assembly uses variables/registers compared to an HLL like Java
Thanks in advance,
-Mike
Hello LiveMike,
See the comments interspersed above.
The validity of my comments may vary depending on the unspecified assembler that you are using.
Peg,
Wow, thanks, that was very helpful!! One more thing though, I'm working with some old code that has this at the top where the rest of the variables are declared in RAM...
ARRAYNAME:
VAR1 DS 1
VAR2 DS 1
VAR3 DS 1
VARX DS 8
So NO whitespace before "ARRAYNAME:" makes it a label, but can it be the "label" of an array??
Elsewhere in the code, sometimes the data in VAR2 for example, is manipulated using "VAR2" and in other places it is manipulated by using "'ARRAYNAME, X" with x being 1. The code seems to work, but is it really possible to acces this register/byte using it's name "VAR2" or it's location in the array "ARRAYNAME, X" or "ARRAYNAME+1"??
Thanks so much!!
-Mike
Hello Mike,
Each of the following code snippets will achieve a write to the same memory location -
ldhx #ARRAYNAME
sta 2,x
ldhx #VAR3
sta ,x
ldhx #2
sta ARRAYNAME,x
sta VAR3
Regards,
Mac
Mac,
Again, thanks so much!! You're my fav on the forums, soooo very knowledgeable!! That was exactly what I needed to see! BUT... haha, always a BUT... I have seen this in my 'old' code I'm working with:
LDA ARRAYNAME-1, X
So is that loading the accumlator withe the (X - 1) element of the array?
I need to create a new array from scratch, but I have to figure out what the original is doing
And let me make sure I'm doing this correct (and hopefully efficient)...
Store the numbers 25,15,7,2,1 to 5 elements of an array...
DATAARRAY:MEMBERS DS 1CARS DS 1TRUCKS DS 1BOATS DS 1BIKES DS 1
CLRXLDA #25 ; 25 MembersSTA DATAARRAY, XINCXLDA #15 ; 15 CarsSTA DATAARRAY, XINCXLDA #7 ; 7 TrucksSTA DATAARRAY, XINCXLDA #2 ; 2 BoatsSTA DATAARRAY, XINCXLDA #1 ; 1 BikeSTA DATAARRAY, X
And then I could also updat Trucks later on like this:
LDA #8STA TRUCKS
And lets say I wanted to update Boats AND Bikes, how do I say "array element + 1"...
LDX #3 ; 'Boats' is element 3LDA #5 ; new number of BoatsSTA DATAARRAY, XLDA #3 ; new number of BikesSTA DATAARRY + 1, X
I know I could INCX before updating Bikes, but maybe its 13 elements away...
Anyway, THANK YOU SO MUCH for all the info and help.
-Mike
Hello Mike,
Consider the following code snippet -
INIT_TAB: ; Array initialisation data
DC.B 25 ; Members
DC.B 15,7,2,1 ; Cars, trucks, boats, bikes
...
; For indexed addressing, make sure that H is correctly set
; Initialise array - method 1
LDHX #5 ; Elements in array
LOOP1: LDA INIT_TAB-1,X
STA DATAARRAY-1,X
DBNZX LOOP1
; Initialise array - method 2
LDHX #5 ; Elements in array
LOOP1: DECX
LDA INIT_TAB,X
STA DATAARRAY,X
TSTX
BNE LOOP1 ; Loop while X > 0
Compare the two methods of initialising the array. My personal preference would be the first method.
If DATAARRAY happens to reside in Z_RAM, you could alternatively use MOV instructions for slightly better efficiency,
i.e. MOV #8,TRUCKS
MOV #5,DATAARRAY+3
MOV #3,DATAARRAY+4
Using indexed addressing in this instance is probably of no advantage.
Regards,
Mac
Hi Mike,
Using ARRAYNAME-1 is probably to cater for the fact that the first element is at zero offset and the calculation for the index produces avalue in the range 1 to ARRAYSIZE.
What you have shown is all correct.
Obviously, if you are going to load the array like in your second box then indexed addressing is not the most efficient way to do it. As you have labels for all the elements writing directly to those also self-documents (like 3rd box)
Don't force yourself to use indexed addressing just because you can or think you need to for an array.
Use the most efficient (or self explanatory) mode for what you are trying to do
Indexed addressing is mostly used for sequential accesses in a loop or plucking a value out of a table based on a calculation which is then used for the index into that table (array).
Hi Mike,
LiveMike wrote:Peg,
Wow, thanks, that was very helpful!! One more thing though, I'm working with some old code that has this at the top where the rest of the variables are declared in RAM...
ARRAYNAME:
VAR1 DS 1
VAR2 DS 1
VAR3 DS 1
VARX DS 8
So NO whitespace before "ARRAYNAME:" makes it a label, but can it be the "label" of an array??
Elsewhere in the code, sometimes the data in VAR2 for example, is manipulated using "VAR2" and in other places it is manipulated by using "'ARRAYNAME, X" with x being 1. The code seems to work, but is it really possible to acces this register/byte using it's name "VAR2" or it's location in the array "ARRAYNAME, X" or "ARRAYNAME+1"??
Thanks so much!!
-Mike
There is no formal declaration of the array.
In assembler an array is just a number of sequential address locations. There is nothing to warn you when you run off the end etc
As there is no instructions after ARRAYNAME: the address is still the same for VAR1.
ARRAYNAME and VAR1 are labels at the same address.
Either label could be used to index into the array as there is no formal array, just more memory following that label.
Heck if you had a label three locations subsequent to this you could index from there with an extra three added to your index
There are very few rules with assembler. This allows you to do wonderful things or to stuff up completely leaving you to figure out why.
There are spaces/tabs between VAR1 and DS and 1 and LDA and STA etc...
-Mike