# sort.s -- MIPS assembly program to sort 3 numbers in ascending order. # Let's assume the numbers are 4, 6 and 2. # In the main program, these numbers are stored in $s0-$s2, # and when we are done, these numbers will be in ascending order. .data swap_str: .asciiz "Need to swap the values " and_str: .asciiz " and " sorted_str: .asciiz "The sorted values are " comma: .asciiz "," newline: .asciiz "\n" .text main: li $s0, 4 # begin by initializing values li $s1, 6 # to be sorted li $s2, 2 move $a0, $s0 # get procedure parameters ready move $a1, $s1 move $a2, $s2 jal sort3 # call procedure that sorts them move $s0, $a0 # move sorted values back into their move $s1, $a1 # original registers move $s2, $a2 li $v0, 4 # OUTPUT: Print "sorted" message la $a0, sorted_str syscall li $v0, 1 # first number to print (smallest) move $a0, $s0 syscall li $v0, 4 # print comma la $a0, comma syscall li $v0, 1 # second number to print (middle value) move $a0, $s1 syscall li $v0, 4 # print comma la $a0, comma syscall li $v0, 1 # third number to print (largest) move $a0, $s2 syscall li $v0, 4 # print new line la $a0, newline syscall li $v0, 10 # end of program syscall #---------------------------------------------------------------------- # sort3 -- sorts the numbers in $a0-$a2 so that $a0 <= $a1 <= $a2 # Inside this function, you should call sort2 three times. # First call: pass the first and second numbers. # Second call: pass the second and third numbers. # Third call: pass the first and second numbers again. # sort2 expects its arguments to be in $a0 and $a1. # To simplify the juggling of registers, I recommend you use # s or t registers to hold the values of a0,a1,a2 before calling sort2. sort3: ############################################ # Insert the code to complete this function. ############################################ #--------------------------------------------------------------------- # sort2 -- sort the values that are in $a0 and $a1 sort2: sw $ra, 0($sp) # save return address addi $sp, $sp, -4 # Also need to save $s0-$s1 so we have a place to put # our arguments. sw $s0, 0($sp) # save $s0 addi $sp, $sp, -4 sw $s1, 0($sp) # save $s1 addi $sp, $sp, -4 move $s0, $a0 # copy parameters into $s0 and $s1 move $s1, $a1 # Now we need to make sure $s0 and $s1 are in <= order. # If $s1 < $s0, we need to swap their values. slt $t0, $s1, $s0 beqz $t0, skip li $v0, 4 # tell user that we're going to swap la $a0, swap_str syscall li $v0, 1 # print value of $s0 move $a0, $s0 syscall li $v0, 4 # print word "and" la $a0, and_str syscall li $v0, 1 # print value of $s1 move $a0, $s1 syscall li $v0, 4 # print newline la $a0, newline syscall move $t0, $s0 # The actual swapping is done with 3 moves. move $s0, $s1 move $s1, $t0 skip: # We're done, so now we put $s0 and $s1 back in $a0 and $a1, # and restore $s0, $s1 and $ra. move $a0, $s0 move $a1, $s1 addi $sp, $sp, 4 lw $s1, 0($sp) # restore $s1 addi $sp, $sp, 4 lw $s0, 0($sp) # restore $s0 addi $sp, $sp, 4 lw $ra, 0($sp) # restore $ra jr $ra # return