0

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

4

1 回答 1

1

将用户输入输入寄存器。

然后使用 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.
于 2009-11-28T18:34:13.700 回答