# ---------------------------------------------
# writeStr.s
#	send a string to the display
# .............................................
# calling sequence:
#	push address of string to write
#	call	writeStr
#	add 4 to stack pointer
# .............................................
# adapted from Robert G. Plantz - 20 March 2000
# adapted by David Bozarth - 14 April 2004
# modified 27 April 2004
# ---------------------------------------------
STDIN = 0
STDOUT = 1

# Stack frame
stkTop = -8
intCount = -8
strLoc = -4	# local string address variable
basePtr = 0
rtnAddr = 4
strIn = 8	# string address passed as argument

	.text
	.align 4
	.globl writeStr

writeStr:
	pushl	%ebp			# save caller's base pointer
	movl	%esp, %ebp		# establish our base pointer
	addl	$stkTop, %esp		# allocate local variable
	pushl	%ebx			# save registers - using write()
	pushl	%ecx
	pushl	%edx

	movl	strIn(%ebp), %ebx	# address of string -> local varb
	movl	%ebx, strLoc(%ebp)	# .. this will get ++
	movl	$0, intCount(%ebp)	# reset char count
	
writeLoop:		
	movl	strLoc(%ebp), %ebx	# address of next char in string
	cmpb	$0, (%ebx)		# test for null terminator
	je	allDone

	pushl	$1			# send one byte
	pushl	%ebx			# .. at this location
	pushl	$STDOUT			# .. to screen
	call	write
	addl	$12, %esp
	incl	strLoc(%ebp)		# increment pointer
	incl	intCount(%ebp)		# increment char count
	jmp	writeLoop

allDone:
	movl	intCount(%ebp), %eax	# return # chars written	
	popl	%edx			# restore registers
	popl	%ecx
	popl	%ebx
	movl	%ebp, %esp		# restore stack pointer
	popl	%ebp			# restore caller's base pointer
	ret
