# max.s -- Program that finds the largest element in an array # # Purpose of registers: # $s0 = base address of array # $s1 = offset into array # $s2 = absolute address of an element within the array # $s3 = offset for last element in array (36) # $s4 = place to put element value # $s5 = value of largest element (initialized to first element) .data array: .word 24, -39, 7, -2, 4, 48, -23, -1, 10, 0 largest_str: .asciiz "The largest number in the array is " newline: .asciiz "\n" .text main: la $s0, array # get the base address li $s1, 0 # initially, offset will be zero li $s3, 36 # last offset is 36 for array[9] lw $s5, 0($s0) # set largest = array[0] loop: add $s2, $s0, $s1 # compute element address lw $s4, 0($s2) # At this point, the element we just retrieved is in $s4, # and we want to compare it to $s5, to see if we need to # update largest element. So, if $s5 < $s4, we update. bge $s5, $s4, skip # is $s5 < $s4? move $s5, $s4 # a new candidate for largest element skip: addi $s1, $s1, 4 # point to next element ble $s1, $s3, loop # if new offset > 36, exit loop # After loop, it's time for OUTPUT! li $v0, 4 # print "largest number" string la $a0, largest_str syscall li $v0, 1 # print this element now in $s5 move $a0, $s5 syscall li $v0, 4 # print newline la $a0, newline syscall li $v0, 10 # end of program syscall