Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #prompt user for size of array (t0)
- #set array = size t0
- #prompt user for numbers and input them into array
- #otput numbers
- #sort numbers
- #prompt user to enter a number to search for, if not there, report not found
- #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
- .globl main
- .data
- array .byte 101:36 #fill the array with 101s so that they can be discarded during the output
- entries_prompt:
- .asciiz "Please input how many characters you have. Value must be between 2 and 36."
- number_error:
- .asciiz "The value must be between 2 and 36."
- number_input:
- .asciiz "Enter a number: "
- array_output:
- .asciiz "The array contains the following: "
- array_output_sort:
- .asciiz "The sorted array contains the following: "
- search_prompt:
- .asciiz "What number do you want to search for (-9999 to exit)? "
- search_not_found:
- .asciiz "Number not in array."
- search_found:
- .asciiz "Number found in element: "
- program_complete:
- .asciiz "Program complete. Exiting. "
- newline:
- .asciiz "\n"
- .text
- main:
- li $v0, 4 #string output
- la $a0, entries_prompt
- syscall
- li $v0, 5 #prep for user input
- syscall
- move $t0, $v0 #store in $t0, size of our array
- la $a0, array
- li $t1, 0 #this will be our index in the first loop and will be incremented by 4 (because we are dealing with bytes)
- li $t2, 0 #this will be our counter for the first loop
- li $t4, 0 #this will be our index in the output loop and will be incremented by 4 (because we are dealing with bytes)
- input_loop:
- #exit conditions
- blt $t0, 2, input_error
- bgt $t0, 36, input_error #these two are no good, give error message
- beq $t0, $t2, output_loop #this is our signal to keep going, as our loop counter is now equal to our target number
- li $v0, 4 #output user instruction
- la $a0, number_input
- syscall
- li $v0, 5 #prep user input
- syscall
- move $t3, $v0
- la $a0, array #load array back into a0
- #the main body of the loop, the array has been loaded, so now we start feeding values in and incrementing the loop counter
- sb $t3, ($a0) #feed the user input into the array
- addi $t1, $t1, 4 #increment $t1 - our index has moved to the next byte
- addi $t2, $t2, 1 #our loop counter goes up by 1
- j input_loop #go back to the start of the loop
- output_loop:
- #output numbers unsorted
- lb $t5, ($a0) #move value from array into t5
- #exit conditionals
- blt $t5, -100, sort_loop
- bgt $t5, 100, sort_loop #either of these indicate that we are done with our output and can move on to the sort
- move $v0, $t5 #move the value from t5 $v0
- $v0, 1 #print what's in v0
- syscall
- addi $t5, $t5, 4 #move to the next byte in the array
- j sort_loop
- li $t6, 0 #initialize the starting point of the
- sort_loop:
- #just going to implement a bubble sort, because why not. since the numbers are just being compared, i'll use $s0 and $s1
- #set s0 and s1 in preparation for comparison
- lb $t6, ($a0) #pull the number from the address
- move $s0, $t6 #store number from placeholder into $s0
- addi $t6, $t6, 4 #increment the adress pointer by 4
- lb $t6, ($a0) #pull the number from the address
- move $s1, $t6 #store number from pointer into $s1
- #compare s0 and s1. we want the smallest number up front, so we'll keep using 'ble.'
- #here is the engine prototype, and i have basically no idea how to implement it
- #1 initial conditions:
- #2 t1 = 0
- #3 s0 = byte 0
- #4 t2 = t1+4
- #5 s1 = byte 4
- #6 compare s0 and s1
- #7 if s0 <= s1 AND s1 != (-101 OR 101)
- #8 increment t2 by 4
- #9 s1 = byte 4 + 4x
- #10 repeat
- #11 else if s0 > s1 AND s1 != (-101 OR 101)
- #12 swap s0 and s1
- #13 go back to step 6
- #14 else
- #15 increment $t1 by 4
- #16 go back to step 2
- #
- find_loop:
- #1 user prompt
- #2 store user entry in s2
- #3 set t1 = 0
- #4 point t1 at the sorted array
- #5 load position t1 in array
- #6 if s2 == (t1) AND s2 != -9999
- #7 print "number found in byte" t1
- #8 go back to step 1
- #9 else if t1 < 36 AND s2 != -9999
- #10 increment t1 by 4
- #11 go back to step 6
- #12 else if s2 != -9999
- #13 print "number not found"
- #14 go back to step 1
- #15 else
- #16 terminate program
Advertisement
Add Comment
Please, Sign In to add comment