Reputation: 39
addi $s7, $s7, -4
add $s7, $s7, $s1
lw $s0, 4($s7)
Assume integer variables i and j are in registers $s0 and $s1. Assume base address of an integer array X is in register $s7.
So far I have this:
X = X - 4
X = X + j
i = X - 4 + j
Is this correct? I'm not overly sure so just looking for confirmation.
Upvotes: 0
Views: 531
Reputation: 22585
It is incorrect. Note that lw
instruction reads a word from memory.
In C it would look something like
//int *x;
x--; // addi $s7, $s7, -4 decrements pointer to x one element
x = (int*)((char*)x + j); // add $s7, $s7, $s1 increments the address pointed by x j elements
i = *(x+1); // lw $s0, 4($s7) reads the next element pointed by x
Addendum after OPs comment:
If j = $s7
is multiple of 4 (note that each integer occupies 4 bytes), then it could be rewritten in C as i = x[j/4]
.
Upvotes: 4
Reputation: 96
lw-->Load word
syntax:lw $t, offset($s) operation: $t = MEM[$s + offset]; advance_pc (4);
Upvotes: 0