Reputation: 11
I am trying to write a program in mips that creates a word array of memory addresses.
Array: .word 0x10010008,0x1001000C, 0x1001000D, 0x10010007, 0x1001000A
I then need to iterate through the array and retrieve the value at each memory address, specified in the array.
I have a few questions:
How do I get the array to initially store the memory address not at 0x10010000 but at let’s say 0x10010080?
How do I then treat the memory address declared in the array as memory addresses and not values.So that the program can then go to 0x10010008 and get the value stored there?
Upvotes: 0
Views: 990
Reputation: 26646
How do I get the array to initially store the memory address not at 0x10010000 but at let’s say 0x10010080?
Since you're talking about an initialized global data array, and in an area of memory commonly used for global data storage, you have several options. By default, .data
on MIPS starts at 0x10010000. So, if you want your array at 0x10010080, you can ask for 0x80 bytes of padding as follows:
.data
.space 0x80
array:
.word ...
Next, some assemblers will allow a number placed after the .data
, as in
.data 0x10010080
array:
.word ...
How do I then treat the memory address declared in the array as memory addresses and not values.So that the program can then go to 0x10010008 and get the value stored there?
It doesn't make sense to attempt to access the address 0x10010008 to get values store there if you have moved them to 0x10010080.
Addresses are numeric constants, and labels are used to equate names to the numeric values of addresses.
If you want content at a memory location you have to dereference a pointer — form a pointer (e.g. using a label) or use a pointer passed as a parameter and use a lw
or sw
to dereference it.
To form a pointer from a label, use the la
pseudo instruction (it will make a 32-bit address to a label and) can put that in a register, which you can then use as a pointer to the base of the array and access elements by using that base + constant or do further addressing/indexing to access elements of the array.
Upvotes: 1