MIPS 汇编 - 如何只接受 1 到 15 之间的整数?

发布于 2024-12-17 02:58:34 字数 437 浏览 2 评论 0原文

想知道我需要做什么指令来确保用户使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多 2024-12-24 02:58:34

您不需要为每个可能的值使用 beq。您可以使用范围检查,如下所示(说明可能需要稍作修改):

blez $t0, error_msg        ; disallow antyhing less than 1.

addi $t0, $t0, -15         ; subtract 15 from t0, now
                           ;   valid values are <= 0.

bgtz $t0, error_msg        ; disallow anything greater than 15.

addi $t0, $t0, 15          ; re-adjust

...                        ; add the numbers here

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):

blez $t0, error_msg        ; disallow antyhing less than 1.

addi $t0, $t0, -15         ; subtract 15 from t0, now
                           ;   valid values are <= 0.

bgtz $t0, error_msg        ; disallow anything greater than 15.

addi $t0, $t0, 15          ; re-adjust

...                        ; add the numbers here
伊面 2024-12-24 02:58:34

MIPS 处理器没有传统的条件代码。相反,条件测试将寄存器的内容设置为 1 或 0,如您所示。然后,您可以使用 beq 针对零寄存器来测试结果寄存器。

slt  $t5, $t3, $t4                set $t5 = 1 if $t3 < $t4
beq  $t5, $zero, done             branch if $t5 = 0

if here, $t3 < $t4

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.

slt  $t5, $t3, $t4                set $t5 = 1 if $t3 < $t4
beq  $t5, $zero, done             branch if $t5 = 0

if here, $t3 < $t4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文