# ------------------------------------------------------------------
# getList.s
#	Obtain & store a list of integers, from keyboard input.
# Takes 2 arguments: destination base address of int array, and
# max number ints to store. Prompts the user for each individual
# integer. Ends when the user enters nil, or when the max number of
# ints has been stored. Returns number of integers stored.
# ..................................................................
# calling sequence:
#	push max n_ints to store
#	push base address of int array
#	call	getList
#	add 8 to stack pointer
# ..................................................................
# author: David Bozarth - 28 April 2004
# modified 30 April 2004
# ------------------------------------------------------------------
#
TRUE = -1
FALSE = 0
WORD = -4
#
# Stack frame
stkTop = basePtr		# top-of-stack marker
basePtr = 0
rtnAddr = basePtr - WORD
intPtrIn = rtnAddr - WORD	# array base address argument
intMax = intPtrIn - WORD	# max number of ints to store

	.data
strPrompt:
	.string "Integer <Enter>: "
strNewl:
	.string "\n"
		
	.text
	.align	-WORD
	.globl	getList

getList:
	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	$strNewl		# send newline to display
	call	writeStr
	subl	$WORD, %esp
	
	# obtain input until done
	movl	intPtrIn(%ebp), %ebx	# int array base address
	xorl	%ecx, %ecx		# reset index register
	decl	%ecx
	movl	$TRUE, %eax		 
getLoop:
	cmpl	$FALSE, %eax		# while Entry is not nil
	je	allDone

	incl	%ecx			# increment index register
	cmpl	%ecx, intMax(%ebp)	# while count < intMax
	jbe	allDone

	pushl	$strPrompt		# prompt for keyboard input
	call	writeStr
	subl	$WORD, %esp
	leal	(%ebx, %ecx, 4), %eax	# next address for int
	pushl	%eax
	call	getDec			# n_chars converted -> eax
	subl	$WORD, %esp
	jmp	getLoop

allDone:
	movl	%ecx, %eax		# return number of integers stored
	popl	%ecx			# restore registers
	popl	%ebx
	movl	%ebp, %esp		# restore stack pointer
	popl	%ebp			# restore caller's base pointer
	ret
