Reputation: 67
I am doing some homework for a Computer Architecture class where I need to load the value A[f] from memory using MIPS instructions, but the provided solution doesn't make sense to me. I'm very new to this obviously and I know I'm misunderstanding some essential component of how memory works.
Given that f is stored in $s0 and the array A begins at the address stored in $s6, how can the instruction "add $t0, $s6, $s0" get you A[f]?
Lets say the value of f is 5, and the array A begins at 0x000100. Doesn't "add $t0, $s6, $s0" give you 0x000105? That can't be the address of A[5].
Upvotes: 0
Views: 37
Reputation: 26656
Would you agree that the address of A[0] is 0x100?
Then, if A is an array of bytes, the address of A[5] would be 0x105.
However if A is an array of words, then the address of A[5] would be 0x100 + 5*4 or 0x114.
So, you see how the size of the elements of the array makes all the difference? We have to scale the index by the size (in bytes) of the elements.
... Also, the computed index address has to be used in a memory load instruction... if you want to fetch the value from the element of the array, or a memory store instruction if you want to update the value of the element of the array.
Upvotes: 1