# A simple program to demonstrate loops, and array accessing
# Written by Scott D. Anderson, 9/24/96

	.text
	.globl main
main:
	nop
	nop

#
# This code corresponds roughly to the following C code:
#  int array = { 16, 32, 128, 256, 4096, 8192 }
#  int i,x=0xFFFFFFFF
#  for( i=0; i<6; i++ ) x += array[i]
#  array[5] = x	
#
	.data
x:	.word 0xFFFFFFFF
Array:	.word 16
	.word 32
	.word 128
	.word 256
	.word 4096
	.word 8192
	.text
	sw $0,x			# initialize x to zero
	move $s0, $0		# Use $s0 as i
	li $s1, 24		# Use $s1 for the upper bound of the loop
	move $s2, $0		# Use $s2 for the running sum
loop1:	bge $s0,$s1,done1	# while(s0<s1)
	lw $t0,Array($s0)	# t0 is an array element
	add $s2,$s2,$t0		# add it to the running sum
	sw $s2,x		# Is this instruction really necessary?	
	add $s0,$s0,4		# why 4?
	j loop1
done1:	la $t1 Array		# put base address of array in $t1
	sw $s2,20($t1)		# why 20?
	sw $0,0x14($t1)		# what does this do?
#
# Done !!!
#
	jr $ra			# Return from main

