.data input: .space 80 file: .asciiz "input.txt" value: .word 1 array: .space 80 sp: .asciiz " " newl: .asciiz "\n" .text main: la $a0, file #load the name of the file into $a0 li $v0, 13 li $a1, 0 li $a2, 0 syscall #put the file into our buffer move $s0, $v0 #read the fine we just opened li $v0, 14 move $a0, $s0 la $a1, input li $a2, 80 syscall #load our buffer into $a0, convert it into an array la $a0, input la $a1, array jal convertToArray #puts the array into $t1 move $s1, $v0 move $a0, $s1 jal printArray move $a0, $s1 jal sortArray move $s2, $v0 move $a0, $s2 jal printArray j exit sortArray: #move our array to $t1 move $t1, $a0 # int j = 0 #hold onto the beginning of $t1 so we can return it later move $s5, $t1 addi $t2, $zero, 0 #for int j = 0; j < n - 1; j++ (outer for loop) outerloop: beq $t2, 19, exitsort #set the min element to j addi $t3, $t2, 0 #now find the minimum #for(i = j + 1); i < n; i++) #$t6 will be i addi $t6, $t2, 1 loopsmall: beq $t6, 20, smallestfound #$t7 will store the current element we are on sll $t0, $t3, 2 #multiply the smallest element by 4 to get to it #smallest element stored in $t5 add $t5, $t1, $t0 lw $t5, 0($t5) #get the current element i, store it in $t4 sll $t0, $t6, 2 add $t4, $t1, $t0 lw $t4, 0($t4) #now compare $t4 and t5 to see if we have a new smallest blt $t4, $t5, isSmaller addi $t6, $t6, 1 j loopsmall isSmaller: #set minimum element to i addi $t3, $t6, 0 #incrimant i addi $t6, $t6, 1 j loopsmall smallestfound: #if they are equal, dont swap beq $t6, $t3, dontswap #multiply the smallest element by 4 to get to it sll $t0, $t3, 2 #store smallest item in $t5, then load that into the jth element add $t5, $t1, $t0 lw $t9, 0($t5) #hold onto the jth element lw $t8, 0($t1) #store the smallest item in the jth element of the array sw $t9, 0($t1) #now store the jth element into t5, where our smallest element was sw $t8, 0($t5) #now store what was the jth element in what was the smallest element #go to the next element addi $t1, $t1, 4 dontswap: #add 1 to j addi $t2, $t2, 1 j outerloop exitsort: la $v0, ($t1) jr $ra printArray: move $t1, $a0 addi $t3, $zero, 0 #whike $t3 does not equal 20 l: beq $t3, 20, done #print the numner then the space lw $t5, ($t1) li $v0, 1 move $a0, $t5 syscall li $v0, 4 la $a0, sp syscall #go to the next array element, inc. t3 by 1 addi $t1, $t1, 4 addi $t3, $t3, 1 j l done: #print the newline la $a0, newl li $v0, 4 syscall jr $ra #function that converts our buffer into an array convertToArray: #use $t1 to traverse the string move $t1, $a0 move $t2, $a1 move $t9, $t2 #$t2 will be used as the array addi $t3, $zero, 0 #int i = 0 addi $t4, $zero, 80 #size = 20 * 4 #$t7 will be our number loop: move $t6, $zero digit: #keep looping until we get to a non-digit beq $t3, $t4, exitloop #while i < 80 lb $t5, ($t1) #get the byte from $t1 #if we have a new line, we are done with the current number beq $t5, 10, donedig #if its not a number, skip blt $t5, 48, skip bgt $t5, 57, skip subi $t5, $t5, 48 #convert from ascii to integer bne $t6, $zero, curdig addi $t6, $t6, 10 move $t7, $t5 #go to the next byte of the string addi $t1, $t1, 1 addi $t3, $t3, 1 j digit curdig: #add 10 * $t7 to $t5 mul $t7, $t7, $t6 add $t7, $t7, $t5 addi $t1, $t1, 1 addi $t3, $t3, 1 j digit skip: #skip this and move on addi $t1, $t1, 1 addi $t3, $t3, 1 j digit donedig: addi $t1, $t1, 1 addi $t3, $t3, 1 #store $t7 into $t2, then move $t2 4 spaces ahead #move ($t2), $t7 sw $t7, 0($t2) addi $t2, $t2, 4 addi $t7, $zero, 0 j loop exitloop: la $v0, ($t9) jr $ra exit: #end program li $v0, 10 syscall