0

This is a simple program to take two numbers from the user and perform #basic arithmetic functions such as addition, subtraction and multiplication with them #Program flow: #1. Print statements to ask the user to enter the two different numbers #2. Store the two numbers in different registers and print the ‘menu’ of arithmetic instructions to the user #3. Based on the choice made by the user, create branch structures to perform the commands and print the result #4. Exit the program.

My struggle: When I enter number 3 (which is 'multiply' option) everything works fine. But when I enter '1' my program prints out Your final result is: 5Your final result is: -1Your final result is: 6 It prints out 3x times! And when I enter '2' it prints out Your final result is: 1Your final result is: 6 I'm struggling to see where my logic is missing. Also - how to prevent user from entering number that is different from 1-3?

    prompt1:    .asciiz      "Enter the first number: "
    prompt2:    .asciiz      "Enter the second number: "
    menu:      .asciiz      "Enter the number associated with the operation 
    you want performed: 1 => add, 2 => subtract or 3 => 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
    
     #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,multiplyProcess #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 #This is to 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 #This is to terminate the program
    
    multiplyProcess:
    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 #This is to terminate the program
    ```
Szarlotka
  • 27
  • 5
  • 2
    You are missing a `syscall` after assigning `10 to `$v0` to terminate the program. Side note, too much comments and would really use some refactoring. – gusbro Jun 09 '22 at 17:54
  • I have to write comments, it's part of an assignment – Szarlotka Jun 09 '22 at 17:55
  • Ok works fine now, thanks. Do you know how to write an 'if statement' that if user enters number that is different from 1,2,3 the program will stop running? – Szarlotka Jun 09 '22 at 18:06
  • Yes, but you really should try it first. Also note that it would be better especially if you are just starting with assembly to first write the code in a higher level language like C, then manually translate it to assembly. – gusbro Jun 09 '22 at 18:14
  • Comments are good, but comments like `#this adds the values stored in $t0 and $t1 and assigns them to the temporary register $t6` are useless. The `add $t6,$t0,$t1` already tells you that. A useful comment might be `# tmp = in1 + in2` or something to attach meaningful names to the registers. (Also, comments that are too low-level are a pain to maintain, and get out of sync easily as you change the code. e.g. your comment on a `sub` still says add, but it's hard to notice because it's buried inside a long sentence.) – Peter Cordes Jun 09 '22 at 19:14
  • if (menu!=1 || menu!=2 || menu!=3) li $v0, 10 I just need to know how to write that statement in MIPS ;// – Szarlotka Jun 09 '22 at 20:40
  • [Mutiple conditions in if in MIPS](https://stackoverflow.com/q/15375267) . Or simplify it to `!( (menu - 1) < 3U )` with one addiu/sltiu. [How can I implement if(condition1 && condition2) in MIPS?](https://stackoverflow.com/q/55408890) – Peter Cordes Jun 11 '22 at 04:46

0 Answers0