KeKDee
KeKDee

Reputation: 43

4 input MIPS Calculator

Im new to MIPS and my friend practice program is a calculator, instead of allowing users to have 2 inputs(eg. 1 + 2 or 1 * 4) how do allow them to have 4 inputs (eg. 1 + 3 + 3 + 2 or 1 * 4 * 5 * 2). The code below is a working 2 input calculator how do I make it to 4?

#data
.data    
prompt1:    .asciiz      "Type out your first number: "
prompt2:    .asciiz      "Type out your second number: "
prompt3:    .asciiz  "Type out your third number: "
prompt4:    .asciiz  "Type out your fourth number: "
menu:      .asciiz      "Now choose which of the operation you want do: 1 for add, 2 for subtract , 3 for multiply: "
resultText:    .asciiz      "Your final result is: "
    
.text
.globl main
main:
    #The following block of code is to pre-load the integer values representing the various instructions into registers for storage
    li $t3, 1    #This is to load the immediate value of 1 into the temporary register $t3
    li $t4, 2    #This is to load the immediate value of 2 into the temporary register $t4
    li $t5, 3    #This is to load the immediate value of 3 into the temporary register $t5
    li $t6, 4    #This is to load the immediate value of 4 into the temporary register $t6
    li $t7, 5    #This is to load the immediate value of 5 into the temporary register $t7
    
     #asking the user to provide the first number
    li $v0, 4     #command for printing a string
    la $a0, prompt1 #loading the string to print into the argument to enable printing
    syscall      #executing the command
    
    
    #the next block of code is for reading the first number provided by the user
    li $v0, 5    #command for reading an integer
    syscall      #executing the command for reading an integer
    move $t0, $v0     #moving the number read from the user input into the temporary register $t0
    
    #asking the user to provide the second number
    li $v0, 4    #command for printing a string
    la $a0, prompt2    #loading the string into the argument to enable printing
    syscall      #executing the command
    
    
    #reading the second number to be provided to the user
    li $v0, 5    #command to read the number  provided by the user
    syscall      #executing the command for reading an integer
    move $t1, $v0    #moving the number read from the user input into the temporary register $t1
    

    
    li $v0, 4    #command for printing a string
    la $a0, menu    #loading the string into the argument to enable printing
    syscall      #executing the command
    
    #the next block of code is to read the number provided by the user
    li $v0, 5    #command for reading an integer
    syscall      #executing the command
    move $t2, $v0    #this command is to move the integer provided into the temporary register $t2
    
    beq $t2,$t3,addProcess    #Branch to 'addProcess' if $t2 = $t3
    beq $t2,$t4,subtractProcess #Branch to 'subtractProcess' if $t2 = $t4
    beq $t2,$t5,multiplicationProcess #Branch to 'multiplyProcess' if $t2 = $t5
    
    addProcess:
    add $t6,$t0,$t1    #this adds the values stored in $t0 and $t1 and assigns them to the     temporary register $t6
    
    #The following line of code is to print the results of the computation above
    li $v0,4    #this is the command for printing a string
    la $a0,resultText    #this loads the string to print into the argument $a0 for printing
    syscall      #executes the command
    
    #the following line of code prints out the result of the addition computation
    li $v0,1
    la $a0, ($t6)
    syscall
    
    li $v0,10 #terminate the program
    
    subtractProcess:
    sub $t6,$t0,$t1 #this adds the values stored in $t0 and $t1 and assigns them to the temporary register $t6
    li $v0,4    #this is the command for printing a string
    la $a0,resultText    #this loads the string to print into the argument $a0 for printing
    syscall      #executes the command
    
    #the following line of code prints out the result of the addition computation
    li $v0,1
    la $a0, ($t6)
    syscall
    
    li $v0,10 #terminate the program

    multiplicationProcess:
    mul $t6,$t0,$t1 #this adds the values stored in $t0 and $t1 and assigns them to the temporary register $t6
    li $v0,4    #this is the command for printing a string
    la $a0,resultText    #this loads the string to print into the argument $a0 for printing
    syscall      #executes the command
    
    #the following line of code prints out the result of the addition computation
    li $v0,1
    la $a0, ($t6)
    syscall
    
    li $v0,10 #terminate the program

Upvotes: 0

Views: 426

Answers (1)

ConnerWithAnE
ConnerWithAnE

Reputation: 1168

Pretty much all you want to do is add, subtract or multiply the values from each other one right after another

addProcess:
add $t6,$t0,$t1    
add $t6, $t6, $t8  
add $t6, $t6, $t9  

this adds the two values saving them at $t6, then adds the value at register $t6 to the value at register $t8 while again saving to $t6, then once more.

subtractProcess:
sub $t6,$t0,$t1 
sub $t6,$t6, $t8
sub $t6, $t6, $t9 

multiplicatioProcess:
mul $t6,$t0,$t1 
mul $t6, $t6, $t8
mul $t6, $t6, $t9

Just add these where your single line is, then also add a spot at the top requesting the user enter more values

#asking the user to provide the first number
li $v0, 4     
la $a0, prompt1
syscall      


#the next block of code is for reading the first number provided by the user
li $v0, 5    
syscall      
move $t0, $v0     

#asking the user to provide the second number
li $v0, 4    
la $a0, prompt2    
syscall     

#reading the second number to be provided to the user
li $v0, 5   
syscall      
move $t1, $v0    

#asking the user to provide the third number
li $v0, 4   
la $a0, prompt3    
syscall
  
#reading the second number to be provided to the user
li $v0, 5    
syscall      
move $t8, $v0    

#asking the user to provide the third number
li $v0, 4   
la $a0, prompt4  
syscall      

#reading the second number to be provided to the user
li $v0, 5    
syscall      
move $t9, $v0    

you could also do all of this in the form of a procedure but its more advanced

Upvotes: 1

Related Questions