Advertisement
Guest User

Untitled

a guest
Oct 15th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Q5.ASM
  2. ## AUTHOR: BRAD LINDSAY
  3. ## STUDENT ID: 001199212
  4. ## ABOUT: Application finds max of 10 values
  5. ##        Using the MAX3 function
  6.  
  7.  
  8. ## Variable Mapping:
  9. #   $t7, base address for T20Numbers
  10. #   $t6, max index
  11. #   $t5, current index
  12. #   $t4, unused
  13. #   $t3, unused
  14. #   $t2, Value of Max loaded from address.
  15. #   $t1, Address to Max
  16. #   $t0, address of word to extract
  17.  
  18. .data
  19.  
  20. T20Numbers:     .word  12,20,-9,13,15,18,-8,10,20,-14 # The 10 numbers
  21. Max:        .space 4
  22.  
  23. .macro getnum(%ax)
  24. sll $t0, $t5, 2     # multiply our index by 4 to get word offset
  25. add $t0, $t7, $t0   # add to get word address.
  26. lw %ax, 0($t0)      # load word into ax (a0,a1,a2)
  27. .end_macro
  28.  
  29. .text  
  30.  
  31. la $t7, T20Numbers  # get address of first element of T20Numbers
  32. li $t6, 9       # maximum address for our word list.
  33. li $t5, 0       # track which element we're starting on.
  34.  
  35. ## MAIN PROGRAM LOOP ##############################################################################
  36. getmax:         getnum($a0)     # populate $a0 with number from T20Numbers.
  37.             addi $t5, $t5, 1    # add 1 to our index.
  38.             getnum($a1)     # populate $a1 with number from T20Numbers.
  39.             addi $t5, $t5, 1    # add 1 to our index.
  40.             getnum($a2)     # populate $a2 with number from T20Numbers.
  41.            
  42.             jal max3            # check max between these three numbers
  43.             jal checkmax        # set the max value if need be.
  44.            
  45.             beq $t5, $t6, exit  # exit procedure if finished.
  46.            
  47.                         # note: this is dumb, but it works.
  48.             addi $t5, $t5, -1   # otherwise decrement by 1 and repeat
  49.                         # end note
  50.                          
  51.             j getmax        # jump to label for repeat
  52. ## END MAIN PROGRAM LOOP ##########################################################################
  53.  
  54. ## CHECK MAX SUBROUTINE ###########################################################################
  55. checkmax:       la $t1, Max         # load address of max into $t1
  56.             lw $t2, 0($t1)          # load content of max into $t2
  57.             bge $v0, $t2, setmax        # check if new max > old max if so set.
  58.             jr $ra              # otherwise just return
  59. setmax:         sw $v0, 0($t1)          # store $v0 as new max
  60.             jr $ra
  61. ## END CHECK MAX SUBROUTINE #######################################################################
  62.  
  63. ## MAX SUB ROUTINE ################################################################################
  64. max3:           add $v0, $a0, $zero     # copy $a0 to $v0
  65.             sub $t0, $a1, $v0       # if $a1 < $v0 then $t0 is negative
  66.             bltz    $t0, good       # if $t0 is < 0 nothing changes
  67.             add     $v0, $a1, $zero     # otherwise $a1 is highest
  68. good:           sub $t0, $a2, $v0       # if $a2 < $v0 then $t0 is negative
  69.             bltz    $t0, done       # if $t0 is < 0 nothing changes
  70.             add $v0, $a2, $zero     # $a2 becomes highest
  71. done:           jr $ra              # return to caller
  72. ## END MAX SUB ROUTINE ############################################################################
  73.  
  74. ## EXIT HANDLING ##################################################################################
  75. exit:           lw $a0, 0($t1)          # load the max value into $a0
  76.             addi $v0, $zero, 1      # request system call function 1 output
  77.             syscall             # issue systemcall.
  78. ## END EXIT HANDLING ##############################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement