开发者

Branching to labels

I'm writing a MIPS code for a simple calculator, and was wondering how you branch to the correspond开发者_如何学编程ing 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?


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜