LC3模拟器检查寄存器是否大于0



如何检查寄存器是否大于 0?
例如:我想检查 R2 是否大于 0

这就是我所做的:
添加 R2、R2、#0

但这不会检查 R2 是否大于 0,似乎它将 R2 的值设置为 0

检查寄存器是否大于零是一个两步过程。

首先,您需要设置条件代码寄存器,然后您将使用 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 和 NOT 将自动更新 CC 寄存器。

查看 ISA 文档,了解 BR 指令。

最新更新