Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. .data
  2. prompt: .asciiz "Please input 5 numbers.\n"
  3. outPrompt: .asciiz "The sorted numbers are:\n"
  4. newline: .asciiz "\n"
  5. .align 4
  6. array: .space 20 #4 bytes per number, 5 numbers
  7.  
  8. .text
  9. #storing the array
  10. la $s0, array
  11.  
  12. #tell user for input
  13. li $v0, 4
  14. la $a0, prompt
  15. syscall
  16.  
  17. #jump to collect input
  18. jal input
  19.  
  20. #variables for sorting
  21. li $t0, -1 #$t0 = i
  22. li $t1, 25 #number of possible total swaps that can occur, given 5 numbers (n^2)
  23.  
  24. #jump to begin sorting (loop1)
  25. j loop1
  26. j exit
  27.  
  28. input:
  29. #get input and store it
  30. li $v0, 5
  31. syscall
  32. sw $v0, 0($s0)
  33.  
  34. #get input and store it
  35. li $v0, 5
  36. syscall
  37. sw $v0, 4($s0)
  38.  
  39. #get input and store it
  40. li $v0, 5
  41. syscall
  42. sw $v0, 8($s0)
  43.  
  44. #get input and store it
  45. li $v0, 5
  46. syscall
  47. sw $v0, 12($s0)
  48.  
  49. #get input and store it
  50. li $v0, 5
  51. syscall
  52. sw $v0, 16($s0)
  53.  
  54. jr $ra
  55.  
  56. loop1:
  57. addi $t0, $t0, 1
  58. beq $t0, $t1, output
  59. #code
  60. jal loop2
  61.  
  62. loop2:
  63. #loading the value we're checking to swap
  64. lw $t2, 0($s0)
  65.  
  66. #loading j + 1, the next value in the array
  67. lw $t3, 4($s0)
  68.  
  69. #if j < j + 1, switch
  70. blt $t2, $t3, switch2
  71.  
  72. #otherwise, return to loop 1
  73. j loop1
  74.  
  75. switch2:
  76. #switch
  77. sw $t3, 0($s0)
  78. sw $t2, 4($s0)
  79.  
  80. jal current
  81.  
  82. #to the next loop
  83. j loop3
  84.  
  85. loop3:
  86. #loading the value we're checking to swap
  87. lw $t2, 4($s0)
  88.  
  89. #loading j + 1, the next value in the array
  90. lw $t3, 8($s0)
  91.  
  92. #if j < j + 1, switch
  93. blt $t2, $t3, switch3
  94.  
  95. #otherwise, return to loop 1
  96. j loop1
  97.  
  98. switch3:
  99. #switch
  100. sw $t3, 4($s0)
  101. sw $t2, 8($s0)
  102.  
  103. jal current
  104.  
  105. #to the next loop
  106. j loop4
  107.  
  108. loop4:
  109. #loading the value we're checking to swap
  110. lw $t2, 8($s0)
  111.  
  112. #loading j + 1, the next value in the array
  113. lw $t3, 12($s0)
  114.  
  115. #if j < j + 1, switch
  116. blt $t2, $t3, switch4
  117.  
  118. #otherwise, return to loop 1
  119. j loop1
  120.  
  121. switch4:
  122. #switch
  123. sw $t3, 8($s0)
  124. sw $t2, 12($s0)
  125.  
  126. jal current
  127.  
  128. #to the next loop
  129. j loop5
  130.  
  131. loop5:
  132. #loading the value we're checking to swap
  133. lw $t2, 8($s0)
  134.  
  135. #loading j + 1, the next value in the array
  136. lw $t3, 12($s0)
  137.  
  138. #if j < j + 1, switch
  139. blt $t2, $t3, switch4
  140.  
  141. #otherwise, return to loop 1
  142. j loop1
  143.  
  144. switch5:
  145. #switch
  146. sw $t3, 12($s0)
  147. sw $t2, 16($s0)
  148.  
  149. jal current
  150.  
  151. #to the next loop
  152. j loop5
  153.  
  154. current:
  155. #pull value in array and print it
  156. lw $t7, 0($s0)
  157. li $v0, 1
  158. move $a0, $t7
  159. syscall
  160.  
  161. #pull value in array and print it
  162. lw $t7, 4($s0)
  163. li $v0, 1
  164. move $a0, $t7
  165. syscall
  166.  
  167. #pull value in array and print it
  168. lw $t7, 8($s0)
  169. li $v0, 1
  170. move $a0, $t7
  171. syscall
  172.  
  173. #pull value in array and print it
  174. lw $t7, 12($s0)
  175. li $v0, 1
  176. move $a0, $t7
  177. syscall
  178.  
  179. #pull value in array and print it
  180. lw $t7, 16($s0)
  181. li $v0, 1
  182. move $a0, $t7
  183. syscall
  184.  
  185. #print new line
  186. li $v0, 4
  187. la $a0, newline
  188. syscall
  189.  
  190. jr $ra
  191.  
  192. output:
  193.  
  194.  
  195. exit:
  196. li $v0, 10
  197. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement