tags:

views:

59

answers:

1

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?

+1  A: 

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.
Tom
As far as I can see, this method can only work for 3 different types of input, not 4. So how can I make sure it branches to another label if there is a 4th value it has to be compared to?Say the user inputs '+' (ascii 43) and it is compared to the ascii 45 (-), you can use branch if less than but the ascii for '*' is also less than 45, so how do I make sure it goes to the '+' label rather than the '*'.
Taylor
dont think of it in terms of greater / lower than. Think of it more like a switch statement.
Tom
I have coded an example of this. Will post it if neccesary, but try to understand this.
Tom
Thanks for your help
Taylor