# Finds the maximum number in an array of 8 non-negative 1-word integers and stores it in $a2 # # index = 0 # maxSoFar = 0 # while not (index == number of elements in the array): # if array1[index] > maxSoFar: # maxSoFar = array1[index] # index = index + 1 # .text main: la $a0, array1 # $a0 pointer to the array addi $a1, $zero, 8 # $a1 = number of elements in the array jal maximum # "maximum" function call j finish ## maximum function. The arguments are passed in $a0 and $a1 # $a0 contains a pointer to the array (the address of the first element) # $a1 contains the number of elements in the array # Remember, each element is 4 bytes long, so the addresses of the # array elements are 4 bytes apart maximum: addi $t7, $zero, 0 # $t7 index = 0 addi $t2, $zero, 0 # $t2 maxSoFar = 0 maximum_loop: # if index == number of elements in the array, exit loop beq $t7, $a1, maximum_finish lw $t3, 0($a0) # $t3 = array1[index] slt $t1, $t3, $t2 # if array1[index] < maxSoFar then $t1 = 1 # o/w, $t1 = 0 bne $t1, $zero, notmore # if $t1 is not equal to $zero, then jump to notmore # o/w ($t1 is 0), stay here # Basically, you stay here if the # above test was false --- # array1[index] >= maxSoFar. So, we # update maxSoFar addi $t2, $t3, 0 # maxSoFar = array1[index] notmore: addi $t7, $t7, 1 # index = index + 1 addi $a0,$a0, 4 # point to the next element of the array j maximum_loop maximum_finish: jr $ra finish: addi $a2, $t2, 0 # $a2 now has maximum element # Now we'll set up the array # .data tells the assembler we are storing things in the data segment # of memory. As we know, that will store them starting at 0x10010000. # But because we use the label "array1", we don't have to use the # actual number in our assembly code. .data array1: .word 3,5,7,9,10,8,6,4