daily pastebin goal
47%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     .data
  2. input:  .space 80
  3. file:   .asciiz "input.txt"
  4. value:  .word 1
  5. array:  .space 80
  6. sp:     .asciiz " "
  7. newl:   .asciiz "\n"
  8.     .text
  9. main:
  10.     la $a0, file #load the name of the file into $a0
  11.     li $v0, 13
  12.     li $a1, 0
  13.     li $a2, 0
  14.     syscall
  15.     #put the file into our buffer
  16.     move $s0, $v0
  17.    
  18.     #read the fine we just opened
  19.     li $v0, 14
  20.     move $a0, $s0
  21.     la $a1, input
  22.     li $a2, 80
  23.     syscall
  24.    
  25.     #load our buffer into $a0, convert it into an array
  26.     la $a0, input
  27.     la $a1, array
  28.     jal convertToArray
  29.    
  30.     #puts the array into $t1
  31.     move $s1, $v0
  32.    
  33.     move $a0, $s1
  34.     jal printArray
  35.    
  36.     move $a0, $s1
  37.     jal sortArray
  38.    
  39.    
  40.     move $s2, $v0
  41.     move $a0, $s2
  42.     jal printArray
  43.    
  44.     j exit
  45.    
  46.  
  47. sortArray:
  48.     #move our array to $t1
  49.     move $t1, $a0
  50.     # int j = 0
  51.     #hold onto the beginning of $t1 so we can return it later
  52.     move $s5, $t1
  53.     addi $t2, $zero, 0
  54.    
  55.    
  56.     #for int j = 0; j < n - 1; j++ (outer for loop)
  57. outerloop:
  58.     beq $t2, 19, exitsort
  59.     #set the min element to j
  60.     addi $t3, $t2, 0
  61.     #now find the minimum
  62.     #for(i = j + 1); i < n; i++)
  63.     #$t6 will be i
  64.     addi $t6, $t2, 1
  65. loopsmall:
  66.     beq $t6, 20, smallestfound
  67.     #$t7 will store the current element we are on
  68.     sll $t0, $t3, 2 #multiply the smallest element by 4 to get to it
  69.     #smallest element stored in $t5
  70.    
  71.     add $t5, $t1, $t0
  72.     lw $t5, 0($t5)
  73.    
  74.     #get the current element i, store it in $t4
  75.    
  76.     sll $t0, $t6, 2
  77.     add $t4, $t1, $t0
  78.    
  79.     lw $t4, 0($t4) 
  80.    
  81.     #now compare $t4 and t5 to see if we have a new smallest
  82.     blt $t4, $t5, isSmaller
  83.     addi $t6, $t6, 1
  84.     j loopsmall
  85.    
  86. isSmaller:
  87.     #set minimum element to i
  88.     addi $t3, $t6, 0
  89.     #incrimant i
  90.     addi $t6, $t6, 1
  91.     j loopsmall
  92.    
  93. smallestfound:
  94.    
  95.     #if they are equal, dont swap
  96.     beq $t6, $t3, dontswap
  97.     #multiply the smallest element by 4 to get to it
  98.     sll $t0, $t3, 2
  99.     #store smallest item in $t5, then load that into the jth element
  100.     add $t5, $t1, $t0
  101.     lw $t9, 0($t5)
  102.     #hold onto the jth element
  103.     lw $t8, 0($t1)
  104.     #store the smallest item in the jth element of the array
  105.     sw $t9, 0($t1)
  106.     #now store the jth element into t5, where our smallest element was
  107.     sw $t8, 0($t5)
  108.     #now store what was the jth element in what was the smallest element
  109.     #go to the next element
  110.     addi $t1, $t1, 4
  111. dontswap:
  112.     #add 1 to j
  113.     addi $t2, $t2, 1
  114.     j outerloop
  115.    
  116. exitsort:
  117.     la $v0, ($t1)
  118.     jr $ra
  119.  
  120. printArray:
  121.    
  122.     move $t1, $a0
  123.     addi $t3, $zero, 0
  124.     #whike $t3 does not equal 20
  125. l:
  126.     beq $t3, 20, done
  127.     #print the numner then the space
  128.    
  129.     lw $t5, ($t1)
  130.     li $v0, 1
  131.     move $a0, $t5
  132.     syscall
  133.    
  134.     li $v0, 4
  135.     la $a0, sp
  136.     syscall
  137.    
  138.     #go to the next array element, inc. t3 by 1
  139.     addi $t1, $t1, 4
  140.     addi $t3, $t3, 1
  141.    
  142.     j l
  143.    
  144. done:
  145.     #print the newline
  146.     la  $a0, newl      
  147.     li  $v0, 4         
  148.     syscall
  149.     jr $ra
  150.    
  151. #function that converts our buffer into an array   
  152. convertToArray:
  153.     #use $t1 to traverse the string
  154.     move $t1, $a0
  155.     move $t2, $a1
  156.     move $t9, $t2
  157.     #$t2 will be used as the array
  158.     addi $t3, $zero, 0 #int i = 0
  159.     addi $t4, $zero, 80 #size = 20 * 4
  160.     #$t7 will be our number
  161. loop:
  162.     move $t6, $zero
  163. digit:  #keep looping until we get to a non-digit
  164.     beq $t3, $t4, exitloop #while i < 80
  165.     lb $t5, ($t1) #get the byte from $t1
  166.    
  167.     #if we have a new line, we are done with the current number
  168.     beq $t5, 10, donedig
  169.     #if its not a number, skip
  170.     blt $t5, 48, skip
  171.     bgt $t5, 57, skip
  172.    
  173.     subi $t5, $t5, 48 #convert from ascii to integer
  174.    
  175.     bne $t6, $zero, curdig
  176.     addi $t6, $t6, 10
  177.     move $t7, $t5
  178.     #go to the next byte of the string
  179.     addi $t1, $t1, 1
  180.     addi $t3, $t3, 1
  181.    
  182.     j digit
  183.    
  184.    
  185. curdig:
  186.     #add 10 * $t7 to $t5
  187.     mul $t7, $t7, $t6
  188.     add $t7, $t7, $t5
  189.    
  190.     addi $t1, $t1, 1
  191.     addi $t3, $t3, 1
  192.     j digit
  193.  
  194. skip:
  195.     #skip this and move on
  196.     addi $t1, $t1, 1
  197.     addi $t3, $t3, 1
  198.     j digit
  199. donedig:
  200.     addi $t1, $t1, 1
  201.     addi $t3, $t3, 1
  202.     #store $t7 into $t2, then move $t2 4 spaces ahead
  203.     #move ($t2), $t7
  204.     sw $t7, 0($t2)
  205.     addi $t2, $t2, 4
  206.     addi $t7, $zero, 0
  207.     j loop
  208.    
  209. exitloop:
  210.     la $v0, ($t9)
  211.     jr $ra
  212.    
  213. exit:                                  
  214.     #end program
  215.     li $v0, 10
  216.     syscall
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top