分支到标签

发布于 2024-08-13 00:32:18 字数 94 浏览 1 评论 0原文

我正在为一个简单的计算器编写 MIPS 代码,并且想知道如何根据用户输入分支到相应的函数。例如,如果用户希望将两个数字相加,您如何确保计算器跳转到加法标签,而不是乘法或减法?

I'm writing a MIPS code for a simple calculator, and was wondering how you branch to the corresponding function according to the user input. For example, if the user wishes to add two numbers, how would you make sure the calculator jumps to the add label, instead of perhaps the multiply or subtract?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无畏 2024-08-20 00:32:18

将用户输入存入寄存器。

然后使用 beq 指令将其与第一个 ascii 值(例如“+”)进行比较。

.data
plus: .asciiz "+"
sub:  .asciiz "-"
prod: .asciiz "*"
div   .asciiz "/"

.text
.global calculator
.align 2
.ent calculator

calculator:
    //t0 holds user input

    la  $t1,plus
    lb  $t1,0($t1)
    beq $t0,$t1,add

    //now check for subtraction, division product. Same code, just change the address (add)

    //if none matched, jump to error
    b   error

add:
    //addition code goes here
division:
    //division code goes here
product:
    //product code goes here
subtraction:
    //subtraction code goes here.

error:
   //error code goes here.

Take user input into a register.

Then compare that to the first ascii value, say '+', using a beq instruction.

.data
plus: .asciiz "+"
sub:  .asciiz "-"
prod: .asciiz "*"
div   .asciiz "/"

.text
.global calculator
.align 2
.ent calculator

calculator:
    //t0 holds user input

    la  $t1,plus
    lb  $t1,0($t1)
    beq $t0,$t1,add

    //now check for subtraction, division product. Same code, just change the address (add)

    //if none matched, jump to error
    b   error

add:
    //addition code goes here
division:
    //division code goes here
product:
    //product code goes here
subtraction:
    //subtraction code goes here.

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