#Program: Compute Summation #This sample program uses a procedure call (function/method call) #to compute the summation of a set of integers. .data NumIntegersToInput: .asciiz "Please enter the number of integers to read from the console: " ReturnChar: .asciiz "\n" IntArray: .word 0:100 .text main: #Prompt for the number of integers to read from the console. li $v0, 4 la $a0, NumIntegersToInput syscall #Read in the number of integers li $v0, 5 syscall move $t1, $v0 #number of integers is stored in $t1 #Enter each integer one by one. li $t0, 0 #$t0 is the counter la $t3, IntArray #$t3 is the array location for: li $v0, 5 # read in an integer syscall move $t2, $v0 #move the input value to another register sw $t2,0($t3) #store the input value in memory #Store the integers in an array addi $t0, $t0, 1 addi $t3, $t3, 4 #keep adding 4 to the array index counter #Compare slt $t4, $t0, $t1 bne $t4, $zero, for #Print each integer one by one. li $t0, 0 #$t0 is the counter la $t3, IntArray for2: lw $a0, 0($t3) #load a value from the array into the argument register li $v0, 1 # print an integer syscall la $a0, ReturnChar #print a return character li $v0, 4 syscall addi $t0, $t0, 1 #advance the array counter addi $t3, $t3, 4 #keep adding 4 to the array index counter slt $t4, $t0, $t1 bne $t4, $zero, for2 #pass the address of the integer array and the number of elements #in the array into the sumProcedure la $a0, IntArray move $a1, $t1 jal sumProcedure move $a0, $v0 #print the sum li $v0, 1 syscall la $a0, ReturnChar # print a return character li $v0, 4 syscall li $v0, 10 #end program syscall #Pass the array and number of integers into the Summation procedure sumProcedure: #Compute the sum li $t0, 0 #loop counter li $t1, 0 #summation variable move $t2, $a0 #address counter sum_for: lw $t3, 0($t2) add $t1, $t1, $t3 #add to the sum addi $t2, $t2, 4 #advance the address addi $t0, $t0, 1 #advance the loop counter bne $t0, $a1, sum_for #Return the sum move $v0, $t1 jr $ra #return