Silver_Smoulder

[ASM][Inc]Project 3 Sort Search

May 5th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #prompt user for size of array (t0)
  2. #set array = size t0
  3. #prompt user for numbers and input them into array
  4. #otput numbers
  5. #sort numbers
  6. #prompt user to enter a number to search for, if not there, report not found
  7.  
  8.  
  9. #at some point, you'll see the point where i no longer understand how to implement it, so I'm just going to post the way I think it shoul work and hope for the best
  10.  
  11.  
  12. .globl main
  13.  
  14.  
  15. .data
  16. array .byte 101:36 #fill the array with 101s so that they can be discarded during the output
  17.  
  18. entries_prompt:
  19. .asciiz "Please input how many characters you have. Value must be between 2 and 36."
  20. number_error:
  21. .asciiz "The value must be between 2 and 36."
  22. number_input:
  23. .asciiz "Enter a number: "
  24. array_output:
  25. .asciiz "The array contains the following: "
  26. array_output_sort:
  27. .asciiz "The sorted array contains the following: "
  28. search_prompt:
  29. .asciiz "What number do you want to search for (-9999 to exit)? "
  30. search_not_found:
  31. .asciiz "Number not in array."
  32. search_found:
  33. .asciiz "Number found in element: "
  34. program_complete:
  35. .asciiz "Program complete. Exiting. "
  36. newline:
  37. .asciiz "\n"
  38.  
  39. .text
  40. main:
  41.  
  42. li $v0, 4 #string output
  43. la $a0, entries_prompt
  44. syscall
  45.  
  46. li $v0, 5 #prep for user input
  47. syscall
  48. move $t0, $v0 #store in $t0, size of our array
  49.  
  50.  
  51. la $a0, array
  52. li $t1, 0 #this will be our index in the first loop and will be incremented by 4 (because we are dealing with bytes)
  53. li $t2, 0 #this will be our counter for the first loop
  54. li $t4, 0 #this will be our index in the output loop and will be incremented by 4 (because we are dealing with bytes)
  55.  
  56. input_loop:
  57. #exit conditions
  58. blt $t0, 2, input_error
  59. bgt $t0, 36, input_error #these two are no good, give error message
  60. beq $t0, $t2, output_loop #this is our signal to keep going, as our loop counter is now equal to our target number
  61.  
  62. li $v0, 4 #output user instruction
  63. la $a0, number_input
  64. syscall
  65.  
  66. li $v0, 5 #prep user input
  67. syscall
  68. move $t3, $v0
  69.  
  70. la $a0, array #load array back into a0
  71.  
  72. #the main body of the loop, the array has been loaded, so now we start feeding values in and incrementing the loop counter
  73. sb $t3, ($a0) #feed the user input into the array
  74. addi $t1, $t1, 4 #increment $t1 - our index has moved to the next byte
  75. addi $t2, $t2, 1 #our loop counter goes up by 1
  76. j input_loop #go back to the start of the loop
  77.  
  78. output_loop:
  79.  
  80.  
  81. #output numbers unsorted
  82. lb $t5, ($a0) #move value from array into t5
  83.  
  84. #exit conditionals
  85. blt $t5, -100, sort_loop
  86. bgt $t5, 100, sort_loop #either of these indicate that we are done with our output and can move on to the sort
  87.  
  88. move $v0, $t5 #move the value from t5 $v0
  89. $v0, 1 #print what's in v0
  90. syscall
  91. addi $t5, $t5, 4 #move to the next byte in the array
  92. j sort_loop
  93.  
  94.  
  95. li $t6, 0 #initialize the starting point of the
  96. sort_loop:
  97. #just going to implement a bubble sort, because why not. since the numbers are just being compared, i'll use $s0 and $s1
  98.  
  99. #set s0 and s1 in preparation for comparison
  100.  
  101.  
  102. lb $t6, ($a0) #pull the number from the address
  103. move $s0, $t6 #store number from placeholder into $s0
  104. addi $t6, $t6, 4 #increment the adress pointer by 4
  105. lb $t6, ($a0) #pull the number from the address
  106. move $s1, $t6 #store number from pointer into $s1
  107.  
  108. #compare s0 and s1. we want the smallest number up front, so we'll keep using 'ble.'
  109.  
  110. #here is the engine prototype, and i have basically no idea how to implement it
  111.  
  112. #1 initial conditions:
  113. #2 t1 = 0
  114. #3 s0 = byte 0
  115. #4 t2 = t1+4
  116. #5 s1 = byte 4
  117.  
  118. #6 compare s0 and s1
  119. #7 if s0 <= s1 AND s1 != (-101 OR 101)
  120. #8 increment t2 by 4
  121. #9 s1 = byte 4 + 4x
  122. #10 repeat
  123.  
  124. #11 else if s0 > s1 AND s1 != (-101 OR 101)
  125. #12 swap s0 and s1
  126. #13 go back to step 6
  127.  
  128. #14 else
  129. #15 increment $t1 by 4
  130. #16 go back to step 2
  131.  
  132. #
  133.  
  134.  
  135. find_loop:
  136. #1 user prompt
  137. #2 store user entry in s2
  138. #3 set t1 = 0
  139. #4 point t1 at the sorted array
  140.  
  141. #5 load position t1 in array
  142. #6 if s2 == (t1) AND s2 != -9999
  143. #7 print "number found in byte" t1
  144. #8 go back to step 1
  145.  
  146. #9 else if t1 < 36 AND s2 != -9999
  147. #10 increment t1 by 4
  148. #11 go back to step 6
  149.  
  150. #12 else if s2 != -9999
  151. #13 print "number not found"
  152. #14 go back to step 1
  153.  
  154. #15 else
  155. #16 terminate program
Advertisement
Add Comment
Please, Sign In to add comment