如何将嵌套 If/AND 语句转换为 MIPS?
How to Translate Nested If/AND Statements into MIPS?
作为我正在处理的 class 项目的一部分,必须包含调试功能。我在处理某些 if/and 语句时遇到了问题。这是我写的:
/前面的代码,用于查找状态矩阵中包含 1 的位置/
`beq s0, 1, _debug_mode #debug feature is ON if s0 contains 1
_debug_mode:
li a0, 0 #tile flipped to say NOT revealed
_else:
li a0, 1
`
我没有得到预期的结果,所以我的想法是代码编写不正确。任何见解都会很棒!
计算出最小的 C 代码:
if (debug)
status = !status;
在MIPS中,可以这样写(假设s0是debug,a0是status):
blez s0, no_debug_mode
xori a0, a0, 1
no_debug_mode:
# ...
或更详细
blez s0, no_debug_mode
blez a0, set_status_high
li a0, 0
j no_debug_mode
set_status_high:
li a0, 1
no_debug_mode:
# ...
您发布的示例仅对其中一个值进行了分支,没有使用否定,因此会给出错误的结果。此外,您的 _debug_mode
标签末尾没有跳转,因此 _else
将 始终 执行。
好久没做MIPS了。如果需要分支延迟槽,则必须将它们添加到上述两个答案中。
作为我正在处理的 class 项目的一部分,必须包含调试功能。我在处理某些 if/and 语句时遇到了问题。这是我写的:
/前面的代码,用于查找状态矩阵中包含 1 的位置/
`beq s0, 1, _debug_mode #debug feature is ON if s0 contains 1
_debug_mode:
li a0, 0 #tile flipped to say NOT revealed
_else:
li a0, 1
` 我没有得到预期的结果,所以我的想法是代码编写不正确。任何见解都会很棒!
计算出最小的 C 代码:
if (debug)
status = !status;
在MIPS中,可以这样写(假设s0是debug,a0是status):
blez s0, no_debug_mode
xori a0, a0, 1
no_debug_mode:
# ...
或更详细
blez s0, no_debug_mode
blez a0, set_status_high
li a0, 0
j no_debug_mode
set_status_high:
li a0, 1
no_debug_mode:
# ...
您发布的示例仅对其中一个值进行了分支,没有使用否定,因此会给出错误的结果。此外,您的 _debug_mode
标签末尾没有跳转,因此 _else
将 始终 执行。
好久没做MIPS了。如果需要分支延迟槽,则必须将它们添加到上述两个答案中。