# ------------------------------------------------------------------
# sumList.s
#	Find the sum of a list of integers.
# Takes 2 arguments: address of int array, number of ints in array.
# Returns number of integers displayed.
# ..................................................................
# calling sequence:
#	push number_of_ints
#	push array_base_address
#	call	sumList	
#	add 8 to stack pointer
# ..................................................................
# author: David Bozarth - 30 April 2004
# modified 30 April 2004
# ------------------------------------------------------------------
STDIN = 0
STDOUT = 1
#
# Stack frame
stkTop = 0	# top-of-stack marker	
#intCount = -4	# ints_displayed counter variable
basePtr = 0
rtnAddr = 4
intPtrIn = 8	# int_array_base_address argument
intSize = 12	# number_of_integers (array size) argument
	
	.data
strNewl:
	.string "\n"
	
	.text
	.align 4
	.globl sumList

sumList:
	pushl	%ebp			# save caller's base pointer
	movl	%esp, %ebp		# establish our base pointer
	addl	$stkTop, %esp		# allocate local variables
	pushl	%ebx			# save registers
	pushl	%ecx
	pushl	%edx
	#movl	$0, intCount(%ebp)	# initialize local variable(s)

	xorl	%eax, %eax		# reset accumulator
	xorl	%ecx, %ecx		# reset counter
	movl	intPtrIn(%ebp), %ebx	# array base address
sendLoop:		
	cmpl	intSize(%ebp), %ecx	# while count <= size
	jae	allDone

	movl	(%ebx, %ecx, 4), %edx	# fetch the next integer
	addl	%edx, %eax
	incl	%ecx			# counter++
	jmp	sendLoop

allDone:
	# return sum of integers - already in eax
	#movl	%ecx, %eax		# return number of ints displayed
	popl	%edx			# restore registers
	popl	%ecx			# restore registers
	popl	%ebx
	movl	%ebp, %esp		# restore stack pointer
	popl	%ebp			# restore caller's base pointer
	ret

