Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Q5.ASM
- ## AUTHOR:
- ## STUDENT ID:
- ## ABOUT: Application finds max of 10 values
- ## Using the MAX3 function
- ## Variable Mapping:
- # $t7, base address for T20Numbers
- # $t6, max index
- # $t5, current index
- # $t4, unused
- # $t3, unused
- # $t2, Value of Max loaded from address.
- # $t1, Address to Max
- # $t0, address of word to extract
- .data
- T20Numbers: .word 12,20,-9,13,15,18,-8,10,20,-14 # The 10 numbers
- Max: .space 4
- .macro getnum(%ax)
- sll $t0, $t5, 2 # multiply our index by 4 to get word offset
- add $t0, $t7, $t0 # add to get word address.
- lw %ax, 0($t0) # load word into ax (a0,a1,a2)
- .end_macro
- .text
- la $t7, T20Numbers # get address of first element of T20Numbers
- li $t6, 9 # maximum address for our word list.
- li $t5, 0 # track which element we're starting on.
- ## MAIN PROGRAM LOOP ##############################################################################
- getmax: getnum($a0) # populate $a0 with number from T20Numbers.
- addi $t5, $t5, 1 # add 1 to our index.
- getnum($a1) # populate $a1 with number from T20Numbers.
- addi $t5, $t5, 1 # add 1 to our index.
- getnum($a2) # populate $a2 with number from T20Numbers.
- jal max3 # check max between these three numbers
- jal checkmax # set the max value if need be.
- beq $t5, $t6, exit # exit procedure if finished.
- # note: this is dumb, but it works.
- addi $t5, $t5, -1 # otherwise decrement by 1 and repeat
- # end note
- j getmax # jump to label for repeat
- ## END MAIN PROGRAM LOOP ##########################################################################
- ## CHECK MAX SUBROUTINE ###########################################################################
- checkmax: la $t1, Max # load address of max into $t1
- lw $t2, 0($t1) # load content of max into $t2
- bge $v0, $t2, setmax # check if new max > old max if so set.
- jr $ra # otherwise just return
- setmax: sw $v0, 0($t1) # store $v0 as new max
- jr $ra
- ## END CHECK MAX SUBROUTINE #######################################################################
- ## MAX SUB ROUTINE ################################################################################
- max3: add $v0, $a0, $zero # copy $a0 to $v0
- sub $t0, $a1, $v0 # if $a1 < $v0 then $t0 is negative
- bltz $t0, good # if $t0 is < 0 nothing changes
- add $v0, $a1, $zero # otherwise $a1 is highest
- good: sub $t0, $a2, $v0 # if $a2 < $v0 then $t0 is negative
- bltz $t0, done # if $t0 is < 0 nothing changes
- add $v0, $a2, $zero # $a2 becomes highest
- done: jr $ra # return to caller
- ## END MAX SUB ROUTINE ############################################################################
- ## EXIT HANDLING ##################################################################################
- exit: lw $a0, 0($t1) # load the max value into $a0
- addi $v0, $zero, 1 # request system call function 1 output
- syscall # issue systemcall.
- ## END EXIT HANDLING ##############################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement