# ------------------------------------------------------------------
# copyString.s
#	copy string at source address, to destination address
# ..................................................................
# calling sequence:
#	push int max number of chars
#	push destination base address
#	push source base address
#	call	copyString	
#	add 12 to stack pointer
# ..................................................................
# author: David Bozarth - 10 May 2004
# modified 10 May 2004
# ------------------------------------------------------------------
TRUE = 1
FALSE = 0
WORD = 4
#
# Stack frame
stkTop = basePtr		# top-of-stack marker
basePtr = 0
rtnAddr = basePtr + WORD
strSource = rtnAddr + WORD	# source string base address argument
strDestin = strSource + WORD	# destination string base address argument
intMaxLen = strDestin + WORD	# maximum n_chars to copy
#
	.text
	.align	WORD
	.globl	copyString

copyString:
	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, %ecx		# reset char count
	movl	strSource(%ebp), %ebx	# base address of source string
	movl	strDestin(%ebp), %edx	# base address of destination string
	
writeLoop:		
	cmpl	intMaxLen(%ebp), %ecx	# see if max n_chars has been reached
	jae	allDone
	
	movb	(%ebx), %al		# get a char
	cmpb	$0, %al			# test for null terminator
	je	allDone

	movb	%al, (%edx)		# place char in destination
	incl	%ebx			# increment source pointer
	incl	%edx			# increment destination pointer
	incl	%ecx			# increment char count
	jmp	writeLoop

allDone:
	movb	$0, (%edx)		# The Terminator
	movl	%ecx, %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
