在 MIPS 中表达此语句

发布于 2024-11-25 13:40:02 字数 149 浏览 2 评论 0原文

我刚刚开始使用 SPIM 模拟器来使用 MIPS。有人可以帮我转换这个语句吗?

if(as>47 && as<58) function();
else continue;

提前致谢。 :)

I've just started on MIPS with SPIM simulator. Can someone help me converting this statement ?

if(as>47 && as<58) function();
else continue;

Thanx in advance. :)

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

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

发布评论

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

评论(1

我的奇迹 2024-12-02 13:40:02

我的 MIPS 有点生疏,所以如果不进行细微调整就无法工作,请提前道歉,但这应该能让您很好地了解您正在尝试做什么。

(如果您确实发现这不起作用,请告诉我,以便我编辑帖子)

# Assume 'as' is in $s0
li $t2, 1           # $t2 = 1
slti $t0, $s0, 58   # $t0 = $s0 < 58
addi $t1, $s0, 1    # $t1 = $s0 + 1
slti $t1, 47, $t1   # $t1 =  47 < $t1($s0 + 1) (sgti does not exist)
and $t0, $t0, $t1   # $t0 = $t0 && $t1

bne $t0, $t2, cont      # if ($t0 != $t2) goto cont

function: # Label is optional.
# If both conditions are true, the bne won't branch, so we will
# fall through to function: and run whatever code it has.
# otherwise, we jump to cont: and all the func code is skipped.
    # ...

cont: # continue;

    # ...

请注意,现在,function() 实际上并不是一个函数。但是,您可以jal function 并将该块驻留在其他地方。这是MIPS 指令集的良好参考

MIPS 中的技巧是,由于没有大于指令,因此必须使用相反的指令。

请记住,> 的反义词是不是 <,而是 <=。

My MIPS is a bit rusty, so apologies in advance if this does not work without minor tweaking, but this should hopefully give you a good idea of what you are trying to do.

(If you do find that this does not work, please let me know so I can edit the post)

# Assume 'as' is in $s0
li $t2, 1           # $t2 = 1
slti $t0, $s0, 58   # $t0 = $s0 < 58
addi $t1, $s0, 1    # $t1 = $s0 + 1
slti $t1, 47, $t1   # $t1 =  47 < $t1($s0 + 1) (sgti does not exist)
and $t0, $t0, $t1   # $t0 = $t0 && $t1

bne $t0, $t2, cont      # if ($t0 != $t2) goto cont

function: # Label is optional.
# If both conditions are true, the bne won't branch, so we will
# fall through to function: and run whatever code it has.
# otherwise, we jump to cont: and all the func code is skipped.
    # ...

cont: # continue;

    # ...

Take note that right now, function() is not actually a function. you could however jal function and have that block reside somewhere else. Here's a good reference of the MIPS instruction set.

The trick in MIPS is that since you don't have a greater than instruction, you must use the opposite.

Remember that the opposite of > is NOT <, it is <=.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文