# -------------------------------------------------------------------
# myList.s
#	main program for Project 3 - CS 250 - Sonoma State University
# Take list of integers as input.
# Echo list, display sum & average.
# ...................................................................
# calling sequence:
#	(command only)
# ...................................................................
# author: David Bozarth - 26 April 2004
# modified 30 April 2004
# -------------------------------------------------------------------
#
WORD = -4
LIST = 10
	
# Stack frame
stkTop = intList		# top-of-stack marker
intList = intMax + WORD * LIST	# list of integers by value
intMax = intCount + WORD	# max number of integers to store
intCount = basePtr + WORD	# number of integers actually stored
basePtr = 0
rtnAddr = basePtr - WORD
	
	.data
strNewl:
	.string "\n"
	
	.text
	.align	4
	.globl	main

main:	pushl	%ebp			# save caller's base pointer
	movl	%esp, %ebp		# establish our base pointer
	addl	$stkTop, %esp		# allocate local variables
	pushl	%ebx			# save register(s)
	pushl	%ecx
	pushl	%edx

	# DEBUG ONLY
	#leal	intList(%ebp), %edx
	
	# initialize local variables
	movl	$0, intCount(%ebp)	# this will get n_ints stored
	movl	$LIST, intMax(%ebp)	# set max n_ints to ask for
	# int array <- zeros (did this for debugging the stack & left it in)
	leal	intList(%ebp), %ebx	
	movl	$(WORD * LIST), %ecx
	negl	%ecx
zeroLoop:
	cmpl	$0, %ecx
	je	doConvert
	
	movb	$0, (%ebx)
	incl	%ebx			# .. array address
	decl	%ecx			# .. array index
	jmp	zeroLoop

doConvert:
	movl	intMax(%ebp), %ecx	# max n_ints to store
	pushl	%ecx
	leal	intList(%ebp), %ebx	# destination base address
	pushl	%ebx
	call	getList			# n_chars read -> eax
	addl	$8, %esp
	cmpl	$0, %eax		# test for nil input
	je	allDone

#inputFound:
	# a list was entered - display the list
	movl	%eax, intCount(%ebp)	# store local char count
	pushl	$strNewl	
	call	writeStr		# send newline to display
	addl	$4, %esp
	pushl	intCount(%ebp)		# char count to display function
	leal	intList(%ebp), %ebx	# base_address of int_array
	pushl	%ebx
	call	showList		# display list of integers
	addl	$8, %esp	
	pushl	$strNewl	
	call	writeStr		# send newline to display
	addl	$4, %esp

	# display sum & average
	pushl	intCount(%ebp)		# char count to display function
	leal	intList(%ebp), %ebx	# base_address of int_array
	pushl	%ebx
	call	showStats		# display statistics
	addl	$8, %esp	
	pushl	$strNewl	
	call	writeStr		# send newline to display
	addl	$4, %esp
	
allDone:
	movl	$0, %eax		# return value goes here
	popl	%edx			# restore register(s)
	popl	%ecx			# restore register(s)
	popl	%ebx
	movl	%ebp, %esp		# restore stack pointer
	popl	%ebp			# restore caller's base pointer
	ret

