Question

Using MIPS Assembly language, sort 3 integers from greatest to smallest. Do this without using an...

Using MIPS Assembly language, sort 3 integers from greatest to smallest. Do this without using an array and instead use conditional branches, using the algorithm below:

1. Get Numbers From User

2.IF(#1 < #2) Swap the two numbers

3.IF(#2 < #3) Swap the two numbers

4.IF(#1 < #3) Swap the two numbers

5. Display Numbers in from greatest to smallest

6. End Program

I have not been able to figure out the transfer of control when using branches and am completely lost. Please help!

Here is what I have so far:


       .text
       .globl __start #for QTSpim
      
main:
       #Display message to get 1st number from User
       la $a0, pmt1    #Loads Message for 1st Number
       li $v0, 4       #Loads String
       syscall           #Executes call code 4, printing string to user
      
       #Read integer from user
       li $v0, 5       #Reads in integer
       syscall
      
       #Move integer into $t1
       move $t1, $v0    #Moves #1 into $t1
      
       #Display message to get 2nd number from User
       la $a0, pmt2   #Loads Message for 2nd Number
       li $v0, 4       #Loads String
       syscall
      
       #Read Integer from user
       li $v0, 5       #Reads in integer
       syscall
      
       #Move integer into $t2
       move $t2, $v0   #Moves #2 into $t2
      
       #Display Message to get 3rd number from User
       la $a0, pmt3   #Loads Message for 3rd Number
       li $v0, 4       #Loads String
       syscall
      
       #Read integer from User
       li $v0, 5   #Reads in integer
       syscall
      
       #Move integer into $t4
       move $t4, $v0   #Moves #3 into $t4
      
       #IF (#1 < #2) swap nums
       blt $t1, $t2, swap1      
      
       swap1:  
           #Swap #1 and #2
           move $t3, $t1   #Move #1 into $t3
           move $t1, $t2   #Move #2 into $t1
           move $t2, $t3   #Move #1 into $t1
          
       #IF(#2 < #3) swap nums
       blt $t2, $t4, swap2
      
       swap2:
           #Swap #2 and #3
           move $t3, $t2   #Move #2 into $t3
           move $t2, $t4   #Move #3 into $t2
           move $t4, $t3   #Move #2 into $t4
          
          
      
       #IF (#1 < #2) swap nums
       blt $t1, $t2, swap3
      
       swap3:
           #Swap #1 and #2
           move $t3, $t1   #Move #1 into $t3
           move $t1, $t2   #Move #2 into $t1
           move $t2, $t3   #Move #1 into $t2
             
          
       #Display Result Header
       la $a0, rslt   #Loads result header
       li $v0, 4        #Code to print string
       syscall  
      
       #Print Largest Number from $t1
       move $a0, $t1   #Moves number from $t1 into $a0 for printing
       li $v0, 1       #Prints Number
       syscall
      
       #Print Middle Number from $t2
       move $a0, $t2   #Moves number from $t2 into $a0 for printing
       li $v0, 1       #Prints Number
       syscall
      
       #Print Smallest number from $t4
       move $a0, $t4   #Moves number from $t4 into $a0 for printing
       li $v0, 1       #Prints Number
       syscall
      
      
       #End Program
       li $v0, 10
       syscall
      
      
       .data
pmt1:   .asciiz "Enter first number : "
pmt2:   .asciiz "Enter second number: "
pmt3:   .asciiz "Enter third number : "
rslt:   .asciiz "Numbers from Greatest to Smallest:\n "

Homework Answers

Answer #1

This code has logical error while executing the code.

In the above case even though "blt" condition fails swap is executing.

To correct it modify the code as given below

Check the inverse of the condition in the loop so that if condition fails shift to next loop

Thank you.

Output has been verified .

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions