# Notes example .data Size: .word 10 List: .word 78, 23, 41, 55, 18, 37, 81, 49, 74, 89 .text main: la $a0, List # $a0 is a pointer to the array lw $a1, Size # $a1 is the array size jal FindMax # call FindMax procedure move $a0, $v0 # pass return value to system call li $v0, 1 # print the result syscall exit: li $v0, 10 # terminate the program syscall ######################################################################### # Returns largest value in an array of integers. # # Pre: $a0 points to the first array element # $a1 is the number of elements in the array # Post: $a0 points one past the end of the array # $a1 is unchanged # $v0 is the largest value in the array # Uses: # FindMax: li $t0, 4 # word size mul $t0, $a1, $t0 # calculate offset to end of array add $t0, $a0, $t0 # calculate stop address lw $v0, 0($a0) # initial max value is 0-th element addi $a0, $a0, 4 # step pointer to next element fmaxLoop: bge $a0, $t0, fmaxDone # see if we're past the array end lw $t1, 0($a0) # get next array element bge $v0, $t1, noNewMax # no new max move $v0, $t1 # reset max noNewMax: addi $a0, $a0, 4 # step pointer to next element j fmaxLoop # return to loop test fmaxDone: jr $ra # return to caller