100%(6)6 out of 6 people found this document helpful
This preview shows page 2 - 5 out of 7 pages.
lw $t0, ($a1)# Move count out of memoryand $t0, $t0, $0# Set count equal to zeroadd $t4, $a0, $0# Moves memory address out of a0 to prevent lossing dataaddi $t1, $0, 4# Sets the value between each value in the arrayatopdowhile:li $v0, 4# loads the display function into the registerla $a0, prompt1# loads the variable name into the registersyscall# displays the variable nameli $v0, 5# Loads the read command into $v0syscall# Reads the integer into $v0move $t2, $v0# Places the user value into $t2mbeq $t2, $0, readend# if user entered zero jump to end of while loopbbeq $t0, $0, countzero# if count equal to zero jump to store the word
Sheet1Page 3add $t4, $t4, $t1# Adds the value to retrieve the right address in memoryasw $t2, ($t4)# loads the value in the memory address into $t3addi $t0, $t0, 1# adds one to countj decisioncountzero:sw $t2, ($t4)# loads the value in the memory address into $t3addi $t0, $t0, 1# adds one to countadecision:bge $t0, 20, readend# jump to readend if count is greater or equal to 20j topdowhile# jump to the top of the while loopjreadend:sw $t0, ($a1)# loads the value of count in the memoryjr $ra# jumps back to main#---------------------------------------------------------------------------# Function sort# Description: It will sort the numbers in numerical order.# Parameters:$a0 = address of the array#$a1 = address of the count variable# return $v0 = N/Asort:sw $ra, ($sp)# stores the orginal position in the stack pointeraddi $sp, $sp, -20# moves sp out to store values in memoryaddi $a1, $a1, -1# The total amount of times the for loop needs to runsw $a0, 4($sp)# places address of the array on spsw $a1, 8($sp)# places the count value on sptopfor1:blt $a1, $0, endfor1# logic for the first loopli $t2, 1# Starting value of the second for loopsw $t2, 12($sp)# Saves the first count on the stackstopfor2:bgt $t2, $a1, endfor2# Logic for the second loopaddi $t3, $t2, -1# This becomes j-1
Sheet1Page 4sll $t3, $t3, 2# moves over two: multiplying by foursll $t2, $t2, 2# moves over two: multiplying by fouradd $t3, $t3, $a0# places j-1 address into $t3add $t4, $t2, $a0# places j address into $t4aaddi $sp, $sp, 20# protects the values before going to swaplw $t0, ($t3)# loads the j-1 value into $t0lw $t1, ($t4)# loads the j value into $t1lmove $a0, $t3# moves the value before address into a1move $a1, $t4# moves the current value into a0