2

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

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

提前谢谢。:)

4

1 回答 1

5

我的 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 的诀窍在于,由于您没有大于指令,因此您必须使用相反的指令。

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

于 2011-07-24T08:44:41.263 回答