从 MIPS 转换到 C 时遇到的问题
我试图解决这个家庭作业,但无法想出解决方案。下面是问题,
将以下 MIPS 代码翻译成高级语言程序。 假设$t0、$t1和$t2包含数组A、B的基地址 分别为C。
add $t4, $zero, $zero
Loop:
add $t5, $t4, $t1
lw $t6, 0($t5)
add $t5, $t4, $t2
lw $t7, 0($t5)
add $t6, $t6, $t7
add $t5, $t4, $t0
sw $t6, 0($t5)
addi $t4, $t4, 4
slti $t5, $t4, 256
bne $t5, $zero, Loop
同时查找bne的offset字段中的十进制值
说明。
这是我尝试过的,但我还没有找到 256
的位置。
int *temp4 = 0;
while(1)
{
*int temp5 = temp4 +B[0];
a:
*int temp6 = temp5;
temp5 = C[0] + temp4;
*temp7 = temp5;
temp6 = temp6 + temp7;
temp5 = temp4 + A[0];
temp6 = temp5;
temp4 += 4;
if(temp5 < temp4)
goto __;
if(temp5 != 0)
goto a;
}
I was trying to solve this homework assignment but was unable to come up with a solution. Below is the problem,
Translate the following MIPS code into a high-level language program.
Assume that $t0, $t1, and $t2 contain base addresses of arrays A, B
and C respectively.
add $t4, $zero, $zero
Loop:
add $t5, $t4, $t1
lw $t6, 0($t5)
add $t5, $t4, $t2
lw $t7, 0($t5)
add $t6, $t6, $t7
add $t5, $t4, $t0
sw $t6, 0($t5)
addi $t4, $t4, 4
slti $t5, $t4, 256
bne $t5, $zero, Loop
Also find the decimal value in the offset field of bne
instruction.
Here is what I have tried, but I yet don't find the position of 256
.
int *temp4 = 0;
while(1)
{
*int temp5 = temp4 +B[0];
a:
*int temp6 = temp5;
temp5 = C[0] + temp4;
*temp7 = temp5;
temp6 = temp6 + temp7;
temp5 = temp4 + A[0];
temp6 = temp5;
temp4 += 4;
if(temp5 < temp4)
goto __;
if(temp5 != 0)
goto a;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你想太多了。
该代码的作用是这样的
不会解释为什么,因为这个
看起来很像是家庭作业。你把事情搞混了。看到
slti
末尾的i
了吗?这意味着它使用中间操作数,在本例中为256
。那么slti $t5, $t4, 256
指令的作用是在寄存器$t5
中设置1
,如果的内容>$t4
低于 256。否则,$t5
得到0
。因此,循环将需要 256/4 次迭代,因为只有当
$t4
的内容大于256< 时,
bne
才会失败(即不跳转)。 /代码>。I think that you are overthinking things.
What that code does is something like this
Not going to explain why, since this
looks a lot likeis homework.You are mixing things up. See that
i
at the end ofslti
? That means that it uses an inmediate operand, in this case256
. So what theslti $t5, $t4, 256
instruction is doing, is setting a1
in register$t5
if the contents of$t4
are lower than 256. Otherwise,$t5
gets a0
.Consequently, the loop will take 256/4 iterations, since the
bne
will fallthrough (i.e not jump) only when the contents of$t4
are greater than256
.