无法理解 MIPS 转换为 C 时的代码顺序

Can't Understand the Code Order When MIPS is translated to C

我正在尝试将MIPS代码转换成C。我得到了问题的答案,但我猜的答案与答案略有不同。所以我想问问你。这是问题和建议的答案:

问题:

sll $t0, $s0, 2 

add $t0, $s6, $t0 

sll $t1, $s1, 2 

add $t1, $s7, $t1 

lw $s0, 0($t0) 

addi $t2, $t0, 4 

lw $t0, 0($t2) 

add $t0, $t0, $s0 

sw $t0, 0($t1)

答案:

B[g] = A[f + 1] + A[f];
f = A[f];

我以为答案正好相反,因为f = A[f]是先从上到下计算的。所以这是我的答案:

 f = A[f];
 B[g] = A[f + 1] + A[f];

我知道问题中有正确答案,但为什么呢?我只是卡在那里。

从现在开始感谢,

您的分析是正确的,但您的解释是错误的:

;These 2 compute the address of A[f], using pointer arithmetic
; s0 is f and s6 is A
sll  $t0, $s0, 2     ; t0 = s0 << 2
add  $t0, $s6, $t0   ; t0 = s6 + t0  -- t0 is now the address of A[f]

;These 2 compute the address of B[g], using pointer arithmetic
; s1 is g and s7 is B
sll  $t1, $s1, 2     ; t1 = s1 << 2
add  $t1, $s7, $t1   ; t1 = s7 + t1  -- t1 is now the address of B[g]

; load A[f] into s0. s0 used to be f so we can read this as f = A[f]
lw   $s0, 0($t0)     ; s0 = A[f]

; Compute address of A[f+1]
addi $t2, $t0, 4     ; t2 = t0 + 4 -- t2 is now the address of A[f+1]

; Load A[f+1]
lw   $t0, 0($t2)     ; t0 = Mem[t2] -- which is t0 = A[f+1]

; Add A[f] + A[f+1]
add  $t0, $t0, $s0   ; t0 = t0 + ts -- which is A[f] + A[f+1]
; Store  A[f] + A[f+1] into B[g]
sw  $t0, 0($t1)      ; Mem[t1] = t0 -- which is B[g] = A[f] + A[f+1]

如果要用高级语言来表达,那就是:

B[g] = A[f + 1] + A[f];
f = A[f];

你的顺序执行结果不正确,使用简单替换来检查这些在顺序执行时的含义:

f = A[f];
B[g] = A[f + 1] + A[f];

相同
B[g] = A[A[f] + 1] + A[A[f]];
f = A[f];

这不是代码的作用。