.data SumOfN: .word .text Start: li $t2, 6 #going to compute the sum of n=6 sw $a0, ($sp) #store old a0 value so we don't loose it addi $sp, $sp, -4 #dec stack pointer sw $fp , -4($sp) #store the contents of the fp on the stack move $fp, $sp #fp points to proper place in AR addi $sp,$sp,-8 #adjust sp to point to next free space on stack move $a0, $t2 #load parameter register with n jal SumN_0 #call Sum of N procedure sw $v0, SumOfN #Store the return value somewhere lw $a0, 4($fp) #restore a0 which we saved on the stack move $fp, $sp #wipe out the AR by setting sp=fp addi $sp, $sp, 4 #adjust sp to top of previous AR lw $fp, -4($fp) #set fp to previous AR base lw $a0, SumOfN #Print result to show it works li $v0, 1 syscall li $v0, 10 syscall #Sum of N procedure SumN_0: sw $ra, ($fp) #store return address in AR sw $s1, 0($sp) #we are using the s registers sw $s2, -4($sp) #so we must save them addi $sp, $sp, -8 # adjust sp move $s1, $a0 move $s2, $s1 sub $s1,$s1,1 SumN_1: beqz $s1, SumN_3 add $s2, $s2, $s1 sub $s1, $s1, 1 b SumN_1 SumN_3: move $v0,$s2 lw $s1, 8($sp) lw $s2, 4($sp) addi $sp, $sp, 8 lw $ra, 0($fp) jr $ra