# max.s -- Find the largest element in a 10-element array of double. # Assume that the array is already in the data segment, # # $s0 = base address of array # $s1 = offset into the array # $s2 = total address (base + offset) # $s3 = maximum offset, useful for stopping the loop # $f14 = element loaded from array # $f16 = value of largest element #--------------------------------------------------------------------- .data array: .double 5.6 .double 11.9 .double 8.7 .double -4.3 .double -15.6 .double 0.2 .double -2.3 .double 9.9 .double 7.5 .double 1.0 biggest_str: .asciiz "The largest number in the array is " newline: .asciiz "\n" #--------------------------------------------------------------------- .text main: la $s0, array # set the base address of the array li $s1, 0 # initial offset = 0 li $s3, 72 # maximum offset = 72 add $s2, $s0, $s1 # compute total address l.d $f16, 0($s2) # load first element from array # make it largest by default loop: addi $s1, $s1, 8 # point to next element in array add $s2, $s0, $s1 # compute total address l.d $f14, 0($s2) # load element from array c.lt.d $f14, $f16 # is new element less than largest? bc1t skip_update # if so, don't update largest mov.d $f16, $f14 # if not, we have new largest skip_update: slt $t0, $s1, $s3 # done with array? bnez $t0, loop li $v0, 4 # introduce largest element la $a0, biggest_str syscall li $v0, 3 # print the largest element mov.d $f12, $f16 syscall li $v0, 4 # print newline la $a0, newline syscall li $v0, 10 # done with program syscall