Question

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?

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top