# addn.spim # Input: A number of inputs, n, and n integers. # Output: The sum of the n inputs. .data # Constants. prmpt1: .asciiz "How many inputs? " prmpt2: .asciiz "Next input: " sum: .asciiz "The sum is " nl: .asciiz "\n" .text # Main. .globl main main: li $v0, 4 # Syscall to print prompt string. la $a0, prmpt1 syscall li $v0, 5 # Syscall to read an integer. syscall move $t0, $v0 # n stored in $t0. li $t1, 0 # sum stored in $t1. while: blez $t0, endwhile li $v0, 4 la $a0, prmpt2 syscall li $v0, 5 syscall add $t1, $t1, $v0 # Increase sum by new input. sub $t0, $t0, 1 # Decrement n. b while endwhile: li $v0, 4 la $a0, sum syscall move $a0, $t1 # Syscall to print an integer. li $v0, 1 syscall li $v0, 4 la $a0, nl syscall li $v0, 10 # Syscall to exit. syscall