MIPS 汇编 - 如何只接受 1 到 15 之间的整数?
想知道我需要做什么指令来确保用户使用 PCspim 输入的整数将确保只能输入数字 1-15,如果不显示错误消息?
我已阅读 SLT 等说明,但我不太明白其定义 - ""如果 $s 小于 $t,则 $d 设置为 1。否则它会为零。"" 我不想打印零....
有没有办法有效地执行大于 1 但小于 15 的操作?
我会做
beq $t0, 1, add_num #if content in $t0 = 1, branch to add numbers
beq $t0, 2, add_num
beq $t0, 3, add_num
beq $t0, 4, add_num
beq $t0, 5, add_num etc...right up to 15. but this is soo inefficient
was wondering what instruction i would need to do to make sure the integers inputted from the user using PCspim would make sure only numbers 1-15 can be entered and if not display an error message?
i have read the instructions such as SLT but i dont quite understand the definition -
""If $s is less than $t, $d is set to one. It gets zero otherwise."" i dont want to print zero....
is there a way to efficiently do a greater than 1 but less than 15?
i would do
beq $t0, 1, add_num #if content in $t0 = 1, branch to add numbers
beq $t0, 2, add_num
beq $t0, 3, add_num
beq $t0, 4, add_num
beq $t0, 5, add_num etc...right up to 15. but this is soo inefficient
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要为每个可能的值使用
beq
。您可以使用范围检查,如下所示(说明可能需要稍作修改):You don't need a
beq
for every possible value. You could use a range check, with something like the following (instruction may need slight modifications):MIPS 处理器没有传统的条件代码。相反,条件测试将寄存器的内容设置为 1 或 0,如您所示。然后,您可以使用 beq 针对零寄存器来测试结果寄存器。
MIPS processors don't have traditional condition codes. Conditional tests instead set the contents of a register to 1 or 0, as you indicated. You can then test the result register using a beq against the zero register.