假定变量f和g分配给寄存器$ s0,$ s1。假定数组A和B的基地址分别在寄存器$ s6和$ s7中。以下是我编写的用于转换A [2 *(f + g)] = B [B [16 + f / 2]]的MIPS代码:
# 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)]
我认为当您需要两次访问数组B内的内存位置时,我搞砸了。有人可以帮忙吗?
答案 0 :(得分:1)
我相信正确的解决方法是:
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)