不理解这个问题中左移的目的

Not understanding the purpose of the left shift in this problem

我是MIPs的新手,对这个问题的解决方案有些疑惑。我尝试阅读各种来源,但没有一个真正解释了如何获得转移或为什么需要转移。

For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables i, and j are assigned to registers $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.

Problem is: B[8] = A[i − j];

解决方法是:

sub $t0, $s3, $s4

sll $t0, $t0, 2

添加 $t1、$s6、$t0

lw $t2, 0($t1)

sw $t2, 32($s7)

我不明白为什么i和j相减后还需要这个左移。我知道它会将 i 和 j 的结果移动 4 位,但为什么需要这样做?

需要左移来计算项目的偏移量A[i-j]

数组A和B的每一项都是32位宽(4字节)。但是 MIPS 有字节寻址,所以数组的每一项都需要 4 个地址。

左移 2 位实际上是乘以 4。所以 (i-j) * 4 是数组第 i-j 项的偏移量。 这也是代码 sw $t2, 32($s7) 的最后一行将结果存储在数组的第 8 项中的原因,它使用 32 作为偏移量。