了解此 MIPS 代码的工作原理
Understanding how this MIPS code works
所以我的教授将这个问题放在他的幻灯片中,这就是他的答案,不理解他是如何将它转换成 MIPS 的,所以如果有人能帮助解释这个问题,那就太好了。
变量i在$s3,k在$s5,save[]的基地址在$s6
他给了我们这段 C 代码:
while( save[i] == k ) {
i += 1;
}
并给了我们这个 MIPS 代码作为回应:
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit:
Loop: sll $t1, $s3, 2 # $t1 = 4*i (this is the offset to get to the ith element in your array)
add $t1, $t1, $s6 # $t1 = 4*i + base addr of save (getting the address of save[i])
lw $t0, 0($t1) # $t0 = save[i] (actually loading 4B from address of save[i], so getting the actual number here)
bne $t0, $s5, Exit # branch to Exit if save[i] != k
addi $s3, $s3, 1 # i++
j Loop
Exit:
这里需要注意的事项:
save
是一个int
数组,所以每个元素都是4B。这就是偏移量为 4*i
.
的原因
所以我的教授将这个问题放在他的幻灯片中,这就是他的答案,不理解他是如何将它转换成 MIPS 的,所以如果有人能帮助解释这个问题,那就太好了。 变量i在$s3,k在$s5,save[]的基地址在$s6
他给了我们这段 C 代码:
while( save[i] == k ) {
i += 1;
}
并给了我们这个 MIPS 代码作为回应:
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit:
Loop: sll $t1, $s3, 2 # $t1 = 4*i (this is the offset to get to the ith element in your array)
add $t1, $t1, $s6 # $t1 = 4*i + base addr of save (getting the address of save[i])
lw $t0, 0($t1) # $t0 = save[i] (actually loading 4B from address of save[i], so getting the actual number here)
bne $t0, $s5, Exit # branch to Exit if save[i] != k
addi $s3, $s3, 1 # i++
j Loop
Exit:
这里需要注意的事项:
save
是一个int
数组,所以每个元素都是4B。这就是偏移量为 4*i
.