所以我的教授把这个问题放在他的幻灯片中,这就是他的答案,不明白他如何将其转换为 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。