Domanda

Sto scrivendo un codice MIPS per una semplice calcolatrice e mi chiedevo come si ramificasse la funzione corrispondente in base all'input dell'utente. Ad esempio, se l'utente desidera aggiungere due numeri, come assicureresti che la calcolatrice salti sull'etichetta aggiunta, anziché forse moltiplicare o sottrarre?

È stato utile?

Soluzione

Porta l'input dell'utente in un registro.

Quindi confrontalo con il primo valore ASCII, di '+', usando un'istruzione beq.

.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.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top