Lw t0 a1 move count out of memory and t0 t0 0 set

This preview shows page 2 - 5 out of 7 pages.

lw $t0, ($a1)# Move count out of memory and $t0, $t0, $0# Set count equal to zero add $t4, $a0, $0# Moves memory address out of a0 to prevent lossing data addi $t1, $0, 4# Sets the value between each value in the array a topdowhile: li $v0, 4# loads the display function into the register la $a0, prompt1# loads the variable name into the register syscall# displays the variable name li $v0, 5# Loads the read command into $v0 syscall# Reads the integer into $v0 move $t2, $v0# Places the user value into $t2 m beq $t2, $0, readend# if user entered zero jump to end of while loop b beq $t0, $0, countzero# if count equal to zero jump to store the word
Sheet1 Page 3 add $t4, $t4, $t1# Adds the value to retrieve the right address in memory a sw $t2, ($t4)# loads the value in the memory address into $t3 addi $t0, $t0, 1# adds one to count j decision countzero: sw $t2, ($t4)# loads the value in the memory address into $t3 addi $t0, $t0, 1# adds one to count a decision: bge $t0, 20, readend# jump to readend if count is greater or equal to 20 j topdowhile# jump to the top of the while loop j readend: sw $t0, ($a1)# loads the value of count in the memory jr $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/A sort: sw $ra, ($sp)# stores the orginal position in the stack pointer addi $sp, $sp, -20# moves sp out to store values in memory addi $a1, $a1, -1# The total amount of times the for loop needs to run sw $a0, 4($sp)# places address of the array on sp sw $a1, 8($sp)# places the count value on sp topfor1: blt $a1, $0, endfor1# logic for the first loop li $t2, 1# Starting value of the second for loop sw $t2, 12($sp)# Saves the first count on the stack s topfor2: bgt $t2, $a1, endfor2# Logic for the second loop addi $t3, $t2, -1# This becomes j-1
Sheet1 Page 4 sll $t3, $t3, 2# moves over two: multiplying by four sll $t2, $t2, 2# moves over two: multiplying by four add $t3, $t3, $a0# places j-1 address into $t3 add $t4, $t2, $a0# places j address into $t4 a addi $sp, $sp, 20# protects the values before going to swap lw $t0, ($t3)# loads the j-1 value into $t0 lw $t1, ($t4)# loads the j value into $t1 l move $a0, $t3# moves the value before address into a1 move $a1, $t4# moves the current value into a0

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture