######## # sum5.s # # int sum5(int p1,int p2,int p3,int p4,int p5) # # Sums 5 input parameters and returns the result in $v0. # # Expects the parameters to be passed in order on the stack. # Upon entry to this function should have the following: # | ... | # | p1 | # | p2 | # | p3 | # | p4 | # | p5 | # $sp ->| | # # The stack pointer should be pointing at the next empty slot. # ######## .text .globl _sum5 _sum5: sw $ra, 0($sp) # PUSH subu $sp, $sp, 4 sw $fp, 0($sp) # PUSH subu $sp, $sp, 4 addu $fp, $sp, 28 # point fp to first parameter, 5 params for sum5 subu $sp, $fp, 28 # size of frame = 4 + 4 + 4*numparams + 4*numlocals lw $t0, 0($fp) # p1 lw $t1, -4($fp) # p2 add $t0,$t0,$t1 # $t0 = p1 + p2 lw $t1, -8($fp) # p3 add $t0,$t0,$t1 # $t0 = $t0 + p3 lw $t1, -12($fp) # p4 add $t0,$t0,$t1 # $t0 = $t0 + p4 lw $t1, -16($fp) # p5 add $t0,$t0,$t1 # $t0 = $t0 + p5 move $v0, $t0 # return result exit_sum5: lw $ra, -20($fp) move $t0, $fp lw $fp, -24($fp) move $sp, $t0 jr $ra ############# # main_sum5 # # Example main routine that calls sum5. ############# .text .globl main main: sw $ra, 0($sp) # PUSH subu $sp, $sp, 4 sw $fp, 0($sp) # PUSH subu $sp, $sp, 4 addu $fp, $sp, 8 # point fp to first parameter, no params for main subu $sp, $fp, 8 # size of frame = 4 + 4 + 4*numparams + 4*numlocals li $t0, 1 # PUSH parameter 1 sw $t0, 0($sp) subu $sp, $sp, 4 li $t0, 2 # PUSH parameter 2 sw $t0, 0($sp) subu $sp, $sp, 4 li $t0, 3 # PUSH parameter 3 sw $t0, 0($sp) subu $sp, $sp, 4 li $t0, 4 # PUSH parameter 4 sw $t0, 0($sp) subu $sp, $sp, 4 li $t0, 5 # PUSH parameter 5 sw $t0, 0($sp) subu $sp, $sp, 4 jal _sum5 move $t0, $v0 # get return value sw $t0, 0($sp) # PUSH parameter 1 subu $sp, $sp, 4 jal _printint exit_main: lw $ra, 0($fp) move $t0, $fp lw $fp, -4($fp) move $sp, $t0 jr $ra