Reputation: 192
Assume that the variables f and g are assigned to registers $s0, $s1. Assume that the base address of the arrays A and B are in registers $s6 and $s7 respectively. Below is the MIPS code I have written to translate A[2*(f+g)] = B[B[16 + f/2] ]:
# accessing the correct address for A[2*(f+g)]
line 1. add $t0, $s0, $s1 # $t0 = f + g
line 2. add $s6, $s6, $t0 # A[0] should update to A[(f+g)/4]
line 3. sll $s6, $s6, 3 # A[(f+g)/4] should update to A[(8*((f+g)/4)]
line 4. srl $s0, $s0, 1 # f = f/2
line 5. addi $s0, $s0, 16 # f = f/2 + 16
line 6. sll $s0, $s0, 2 # f = (f/2 + 16) * 4
line 7. add $s7, $s0, $s7 # B[0] should update to B[f/2 + 16]
line 8. sll $s7, $s7, 2 # B[f/2 + 16] should update to (B[f/2 + 16]) * 4
line 9. add $t0, $s7, $0 # $t0 = (B[f/2 + 16]) * 4
line 10. sw $s6, $t0($s7) # should be storing B[(B[f/2 + 16]) * 4] in A[2*(f+g)]
I think that I am messing up with access the memory location inside of array B when you need to access it twice. Can someone help?
Upvotes: 0
Views: 187
Reputation: 192
I believe the correct solution is the following:
add $t0, $s0, $s1
sll $t0, $t0, 3
add $s6, $t0, $s6
srl $t1, $s0, 1
addi $t1, $t1, 16
sll $t1, $t1, 2
add $t2, $s7, $t1
lw $t3, 0($t2)
add t4, $s7, $t3
lw $t5, 0($t4)
sw $t5, 0($s6)
Upvotes: 1