# ==================================================================== # mean() # arguments: # $a0 A pointer to an array of integers # $a1 The number of elements in the array # returns: # $v0 The mean of the array values (as an integer) mean: addiu $sp, $sp, -4 # Make stack space sw $ra, 0($sp) # Preserve the return address jal sum # Sum the array elements div $v0, $v0, $a1 # Divide the sum by the number of # elements in the array lw $ra, 0($sp) # Restore the return address addiu $sp, $sp, 4 # Restore the stack pointer jr $ra # Return to the caller # ==================================================================== # ==================================================================== # sum() # arguments: # $a0 A pointer to an array of integers # $a1 The number of elements in the array # returns: # $v0 The sum of the array values # # Note: No need to create an activation frame since this procedure # does not call any other procedures. sum: li $v0, 0 # Initialize the sum to 0 loop: beq $a1, $zero, end # If there are no remaining elements # to visit, jump to the end. lw $t1, 0($a0) # Grab the next array element... add $v0, $v0, $t1 # ...and add it to the sum addi $a1, $a1, -1 # One less element left to visit addiu $a0, $a0, 4 # Move to the next array element j loop # Repeat! end: jr $ra # Return to the caller # ====================================================================