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

标签 c mips translate

我正在尝试将 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];

这不是代码的作用。

关于无法理解 MIPS 转换为 C 时的代码顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33179567/

相关文章:

c - 使用 switch 语句和函数时程序 (C) 崩溃

arrays - MIPS中的冒泡排序算法

Css 翻译和居中元素

Java 到 VB.Net 的转换 [小片段]

c - 为函数参数指定寄存器?

c++ - SFML 不会打开一个窗口?

c++ - 在socket编程的select()中重新启动定时器

c - 如何在该 MIPS 代码的基本数组中存储偏移量?

Linux Binutils 使用 'as' 组装 Mips

java - 如何在处理中根据对象的旋转来移动对象? (局部轴)