# Sum-soln.asm # # Author : Archana Chidanandan, 09/13/2004 # # This program calculates the sum of the first N integers. # # This program demonstrates the use of procedures using the "jal" and "jr" instructions. # It is Sum.asm modified to follow the MIPS register conventions. # # NOTE : THIS PROGRAM ADHERES TO THE MIPS REGSITERS CONVENTIONS. .text .globl main .globl SumCal .globl loop .globl exit main: la $t0, N lw $s0, 0($t0) # Load the value of N move $a0, $s0 # Move the argument(N) to register $a0 jal SumCal # Call method "SumCal" move $s3, $v0 # Move the return value to register $s3 la $t0, SumOfN sw $s3, 0($t0) # Store sum of first # N integers to memory li $v0, 10 # Prepare to Exit syscall # End of program ##################################################################### # # SumCal - Procedure that calculates the sum of first N integers. # input parameter - N # return value - Sum # ##################################################################### SumCal: # Procedure to calculate # sum of first N integers li $t2, 0 # i = 0 li $t3, 0 # Sum = 0 loop: beq $t2, $a0, exit # if ( i == N ), quit addi $t2, $t2, 1 # i = i + 1 add $t3, $t3, $t2 # Sum = Sum + i j loop exit: move $v0, $t3 jr $ra # Return to calling program .data N: .word 3 SumOfN : .word 0