无法理解将MIPS转换为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];

这不是代码的作用

最新更新