# This program adds 16 array elements together and stores
# result in $t1.

	.text
	.globl main
main:	
	la	$s3, array1		# Load base address of Array 1 into $s3
	addi	$t1, $zero, 0		# Initialize $t1 to 0
	addi	$t0, $zero, 16		# $t0 holds the length of the array
	addi	$t2, $zero, 0		# Initialize loop counter $t2 to 0
	addi	$t3, $zero, 0		# Initial offset is counter * 4 = 0
	addi	$t4, $s3, $zero		# Initial Array 1 element address is $s3
L1:	lw	$t5, 0($t4)		# load Array 1 element counter + 0 into $t5
	lw	$t6, 4($t4)		# load Array 1 element counter + 1 into $t6
	lw	$t7, 8($t4)		# load Array 1 element counter + 2 into $t7
	lw	$t8, 12($t4)		# load Array 1 element counter + 3 into $t8
	add	$t5, $t5, $t6		# add Array 1 element counter+0 to element counter+1 and
					# store back in $t5 because $t5's data no longer needed
	add	$t7, $t7, $t8		# add element counter+2 to element counter+3 and
					# store back in $t7 because $t7's data is no longer needed
	add	$t1, $t1, $t5		# add $t5 to current running total
	addi	$t2, $t2, 4		# increment loop counter by 4 since we are
					# doing 4 iterations each time now
					# increment loop here so dependencies between the add
					# instruction above and the add instruction after are
					# minimized.
	add	$t1, $t1, $t7		# add $t7 to current running total
	sll	$t3, $t2, 2		# multiply counter by 2 to get offset
	slt	$t9, $t2, $t0		# check if counter less array length
					# Note, we have also scheduled another instruction between
					# the addi $t2, $t2, 4 above and this one which reads $t2
	add	$t4, $s3, $t3		# store address of Array 1 element in $t4
	beq	$t9, $zero, Exit	# if equal to zero then end
	j	L1			# otherwise loop
Exit:	ori	$v0, $zero, 10		# Perform graceful exit
	syscall


	.data
array1:		.word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -15

