Professional Documents
Culture Documents
Solution For SPIM Oncourse MaxArray
Solution For SPIM Oncourse MaxArray
Solution For SPIM Oncourse MaxArray
text
main:
li $t0, 1 # increment i in $t0
lw $s0, A($zero) # load a[0] into $s0 - it will contain the largest
value at the end
lw $s1, N # total amount of array elements (N) in $s1
# li $s1, 7 # uncomment for non-general solution
m1: bge $t0, $s1, m3 # bge (branch on graeter or equal): check if we have
already looked at all array elements
mul $t1, $t0, 4 # update increment i (byte-wise number here, so
multiply increment by 4)
lw $t2, A($t1) # load a[i] into $t2
ble $t2, $s0, m2 # ble (branch on less or equal): compare the current
array element value with the largest one,
# if smaller - go to the next array element
move $s0, $t2 # store the current value as largest array value (max
= a[i])
m3: move $a0, $s0 # end of loop, prepare the largest value to print
li $v0, 1 # print number syscall
syscall
li $v0, 10 # exit syscall
syscall