# array of integers # this program reads in array elements, prints the elements, # adds up the array elements, and prints the total # .data size: .word 5 # size of array array: .space 20 # array of 5 ints prompt: .asciiz "Enter an element: " result: .asciiz "\n The total is: " prmsg: .asciiz "\n The array elements are: " blanks: .asciiz " " .globl main .text main: lui $t0, 0x1001 # $t0 <- base addr of data segment lw $t1, 0($t0) # $t1: size of array ori $t2, $0, 0 # $t2: sum, sum = 0 ori $t3, $0, 0 # $t3: loop counter, initally 0 addi $t4, $t0, 4 # $t4: will contain addr of array element readloop: li $v0, 4 # syscall code for print string la $a0, prompt # load addr of prompt in $a0 syscall # print prompt msg li $v0, 5 # syscall code for read int syscall # read an int sw $v0, 0($t4) # store it in the array addi $t4, $t4, 4 # addr of next array element addi $t3, $t3, 1 # increment loop counter blt $t3, $t1, readloop # if not done keep looping ori $t3, $0, 0 # reinitialize loop counter to 0 addi $t4, $t0, 4 # reinitialize pointer to first array element li $v0, 4 # syscall code for print string la $a0, prmsg # load addr of result in $a0 syscall # print result msg loop: lw $t5, 0($t4) # $t5: next array element add $t2, $t2, $t5 # add array element to sum move $a0, $t5 # move array element to $a0 li $v0, 1 # code to print an int syscall # print the element li $v0, 4 # syscall code for print string la $a0, blanks # load addr of blanks in $a0 syscall # print blanks addi $t3, $t3, 1 # increment loop counter addi $t4, $t4, 4 # addr of next array element blt $t3, $t1, loop # if not done keep looping li $v0, 4 # syscall code for print string la $a0, result # load addr of result in $a0 syscall # print result msg move $a0, $t2 # move sum to $a0 li $v0, 1 # code to print an int syscall # print the sum li $v0, 10 # terminate execution and syscall # return control to system