# array5.s # This program illustrates how we declare and access some array elements. # We declare an array called 'array' of 100 integers and initialize all # elements to the value 7. The program finds the sum of the first five # elements, and prints this sum to the screen. # The distinctive feature about how we use arrays in this program # is that we refer to constant indices like a[0], ..., a[4]. .data array: .word 7:100 # We could have said ".space 400", # but that would have initialized elements to 0 # and we'd like all elements to be 7. intro_ans: .asciiz "The sum of the first five elements is " newline: .asciiz "\n" # use of registers # $s0 = base address of array # $s1 = element value loaded from array # $s2 = sum .text main: li $s2, 0 # initialize sum to 0 la $s0, array # load the base address into $s0 lw $s1, 0($s0) # get first element of array add $s2, $s2, $s1 # add to sum lw $s1, 4($s0) # get second element of array add $s2, $s2, $s1 # add to sum lw $s1, 8($s0) # get third element of array add $s2, $s2, $s1 # add to sum lw $s1, 12($s0) # get fourth element of array add $s2, $s2, $s1 # add to sum lw $s1, 16($s0) # get fifth element of array add $s2, $s2, $s1 # add to sum li $v0, 4 # introduce output la $a0, intro_ans syscall li $v0, 1 # print the sum move $a0, $s2 syscall li $v0, 4 # print newline (done with output) la $a0, newline syscall li $v0, 10 # end of program syscall