【发布时间】:2018-09-04 01:03:13
【问题描述】:
如何检查寄存器是否大于 0?
例如:我想检查R2是否大于0
这就是我所做的:
添加 R2、R2、#0
但这并不检查 R2 是否大于 0,它似乎将 R2 的值设置为 0
【问题讨论】:
标签: lc3
如何检查寄存器是否大于 0?
例如:我想检查R2是否大于0
这就是我所做的:
添加 R2、R2、#0
但这并不检查 R2 是否大于 0,它似乎将 R2 的值设置为 0
【问题讨论】:
标签: lc3
检查寄存器是否大于零是一个两步过程。
首先您需要设置条件代码寄存器,然后您将使用 BR 指令根据条件进行分支。
ADD R2, R2, 0 ; Store R2 in R2, this has no effect other than setting CC register.
BRNZ LESS_THAN_OR_ZERO ; Branch if R2 is <= 0, based on the CC register set in last instruction
[statements here] ; if we are here then R2 > 0
BR DONE ; optional if we don't want to execute the next section of code. unconditional branch to done
LESS_THAN_OR_ZERO
[more statements here] ; if we are here then R2 <= 0
DONE
[more statements here]
关于 CC 寄存器的更多信息,它根据写入寄存器的最后一条指令用 N、Z 或 P 更新,这意味着 LD、LEA、LDI、LDR、ADD、AND 和 NOT 将更新 CC 寄存器自动。
查看 ISA 文档了解 BR 指令。
【讨论】: