title: Maximum value in array mips
body: <p>I'm trying to find the maximum value in a array and print out that value in the end.</p>
  
  <pre><code>.globl start
  .data
  array:  .word   8,2,31,81,12,10
  size:   .word   6
  max:    .word 0
  .text
  main:
  jal start
  start:
  lw $t3, size #size
  la $t1, array # get array address
  lw $s5, ($t1)       # set max, $s5 to array[0]
  add $t1, $t1, 4    # skip array[0]
  add $t3, $t3, -1       # len = len - 1 
  loop:
  
  lw $t4, ($t1) # get n of array[n]
  
  ble $t4,$s5,L1 #if t4 is not less than t5 we got a new max otherwise not.
  lw $s5, 0($t4) #get element in array  &lt;-- Pretty sure im doing something wrong here.
  #sw $s5,0($t1) #max
  
  
  L1:
  add $t3, $t3, -1             #counter-1
  addi $t1, $t1, 4 # advance array pointer
  bnez $t3, loop #if not 0 then go on and loop
  
  sw $s5, max #printing the max val of the array
  lw $a0, max
  li $v0, 1
  syscall  
  li $v0, 10
  </code></pre>
  
  <p>If I change the lw instruction to move the s5 var just changes value everytime t4 changes value. My problem is that s5 just changes everytime t4 changes. With the move instruction instead of lw the max value is 10, the last element of the array. </p>
  
  <p>To clarify: I want the s5 variable to keep the value it has and ONLY change when i've found a new max value. At the moment it changes everytime t4 changes when I use the move instruction. </p>
  
  <p>Sorry if the code looks horrible, new to MIPS so I'm pretty clueless. </p>