mips 中的 for 循环不起作用?可能有什么问题?

发布于 2025-01-20 06:02:04 字数 238 浏览 1 评论 0原文

我的MIPS循环代码不起作用。当我尝试运行此代码时,此显示我空白。可能是一个问题。

.data
.text
.globl main
main:

li $t0,0

for:
ble $t0,5,end //for (int i=0;i<=5;i++)

li $v0,1
move $a0,$t0
syscall

add $t0,$t0,1
j for

end:

li $v0,10
syscall

My Mips loop code not working. When i try to run this code this show me blank. what could be a problem.

.data
.text
.globl main
main:

li $t0,0

for:
ble $t0,5,end //for (int i=0;i<=5;i++)

li $v0,1
move $a0,$t0
syscall

add $t0,$t0,1
j for

end:

li $v0,10
syscall

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2025-01-27 06:02:04

当手动编译 C/伪代码时,特别是 if 语句和 for 循环的条件时,请记住,当条件不成立时,您通常希望“跳过”代码。在您的示例中,

//for (int i=0;i<=5;i++)
//{
//    ... some code
//}

您希望在不满足条件时跳过 for 主体。当 i 小于或等于 5 时,您必须进行分支,而不是在 i 大于 5 时进行分支。

然后您可以使用以下命令进行编译:

for:
 bgt $t0,5,end //for (int i=0;i<=5;i++)  so you use bgt instead of ble
  ...
 j for
end:

When hand compiling C/pseudo code, in particular if statements and the condition of for loops, remember that you usually want to jump "over" the code when the condition does not hold. In your example you have

//for (int i=0;i<=5;i++)
//{
//    ... some code
//}

so you want to jump over the for body when the condition is not met. Instead of branching when i is less than or equal to 5, you have to branch when its greater than 5.

You would then compile that for with this:

for:
 bgt $t0,5,end //for (int i=0;i<=5;i++)  so you use bgt instead of ble
  ...
 j for
end:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文