# NOTES: 
# This is RISC-V version; 2020
# Use simulator RARS: https://github.com/TheThirdOne/rars/releases (Download only: rars1_4.jar)
# If unsure about an instruction or system-call (i.e., environment-call), always check:
# [instructions]  https://github.com/TheThirdOne/rars/wiki/Supported-Instructions 
# [system calls]  https://github.com/TheThirdOne/rars/wiki/Environment-Calls 

.data

Size:       .word 10
Vals:       .word 78, 23, 41, 555, 18, 37, 81, 49, 74, 62 
MaximumIs:  .asciz "Maximum value is: \n"

.text

main: 
  lw    s7, Size              # s7 is the array size
  li    s0, 4                 # word size
  mul   s7, s7, s0            # calculate offset to end of array

  la    s0, Vals              # s0 points to current array element
  add   s7, s7, s0            # calculate stop address
  lw    s1, 0(s0)             # initial max value is 0-th element
  addi  s0, s0, 4             # step pointer to next element

loop: 
  bge   s0, s7, done          # see if we're past the array end
  lw    s2, 0(s0)             # get next array element
  bge   s1, s2, foo           # no new max
  mv    s1, s2                # reset max
foo:  
  addi  s0, s0, 4             # step pointer to next element
  j     loop

done: 
  li a7, 4                    # print message
  la a0, MaximumIs
  ecall  
  
  li a7, 1                    # print the result
  mv a0, s1
  ecall  

exit: 
  li a7, 10                   # terminate the program
  ecall
