#example 1, find the max element .data array: .word 5, 8, 3, 2, 9 size: .word 5 .text .globl main main: la $t0, array # $t0 = base address of array lw $t1, size # $t1 = size of array (5) li $t2, 0 # $t2 = index (i = 0) lw $t3, 0($t0) # $t3 = max = array[0] loop: addi $t2, $t2, 1 # i++ beq $t2, $t1, end # if i == size, exit loop sll $t4, $t2, 2 # $t4 = i * 4 (offset) add $t5, $t0, $t4 # $t5 = address of array[i] lw $t6, 0($t5) # $t6 = array[i] # compare max ($t3) and array[i] ($t6) slt $t7, $t3, $t6 # if max < array[i], $t7 = 1 beq $t7, $zero, loop # if not less, continue loop move $t3, $t6 # else, update max j loop end: # exit li $v0, 10 syscall ---------------------------------------------------------- #example 2, find the max element and print it in console .data array: .word 5, 8, 3, 2, 9 length: .word 5 msg: .asciiz "Max value: " .text .globl main main: la $t0, array # $t0 = base address of array lw $t1, length # $t1 = number of elements lw $t2, 0($t0) # $t2 = max = array[0] li $t3, 1 # $t3 = index = 1 loop: slt $t4, $t3, $t1 # if index < length beq $t4, $zero, end # if not, end loop sll $t5, $t3, 2 # offset = index * 4 add $t6, $t0, $t5 # address = base + offset lw $t7, 0($t6) # $t7 = array[index] slt $t8, $t2, $t7 # if max < array[i] beq $t8, $zero, skip # if not, skip move $t2, $t7 # max = array[i] skip: addi $t3, $t3, 1 # index++ j loop end: # print "Max value: " li $v0, 4 la $a0, msg syscall # print max value li $v0, 1 move $a0, $t2 syscall # exit li $v0, 10 syscall