# Sum.asm # # Author : Archana Chidanandan, 12/09/2003 # # This program calculates the sum of the first N integers. # # This program demonstrates the use of procedures using the "jal" and "jr" instructions. # # NOTE : THIS PROGRAM DOES NOT ADHERE TO THE MIPS REGSITERS CONVENTIONS # # Modify the program so that it adheres to the MIPS register conventions. # .text .globl main .globl SumCal .globl loop .globl exit main: la $t0, N lw $t0, 0($t0) # Load the value of N jal SumCal # Call method "SumCal" la $t0, SumOfN sw $t3, 0($t0) # Store sum of first # N integers to memory li $v0, 10 # Prepare to Exit syscall # End of program SumCal: # Procedure to calculate # sum of first N integers li $t2, 0 # i = 0 li $t3, 0 # Sum = 0 loop: beq $t2, $t0, exit # if ( i == N ), quit addi $t2, $t2, 1 # i = i + 1 add $t3, $t3, $t2 # Sum = Sum + i j loop exit: jr $ra # Return to calling program .data N: .word 3 SumOfN : .word 0