	.file	"memccpy.s"
/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

/*	Copyright (c) 1986 AT&T	*/
/*	All Rights Reserved	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any	*/
/*	actual or intended publication of such source code.	*/

	.ident	"@(#)$Header: memccpy.s 1.2 89/10/10 $"

/*
 * Copy s2 to s1, stopping if character c is copied. Copy no more than n bytes.
 * Return a pointer to the byte after character c in the copy,
 * or NULL if c is not found in the first n bytes.
 */

/*
 *	char *
 *	memccpy(s1, s2, c, n)
 *	register char *s1, *s2;
 *	register int c, n;
 *	{
 *		while (--n >= 0)
 *			if ((*s1++ = *s2++) == c)
 *				return (s1);
 *		return (0);
 *	}
 */

#include "sys.s"

#define	MEMCCPY \
;	decl	%ecx		/* decrement bytes to go		*/ \
;	jl	.notfound	/* --n < 0 ?				*/ \
;	slodb ; sstob		/* move a byte				*/ \
;	cmpb	%dl,%al		/* is this the byte that stop us?	*/ \
;	je	.found		/* yup					*/

ENTRY(memccpy)
	pushl	%edi		/ register variables
	pushl	%esi
	movl	8+SPARG0,%edi	/ %edi = destination
	movl	8+SPARG1,%esi	/ %esi = source
	movb	8+SPARG2,%dl	/  %dl = character to stop on
	movl	8+SPARG3,%ecx	/ %ecx = max length

.loop:
	MEMCCPY
	MEMCCPY
	MEMCCPY
	MEMCCPY
	MEMCCPY
	MEMCCPY
	MEMCCPY
	MEMCCPY
	jmp	.loop

.found:
	movl	%edi,%eax	/* yes, return byte after in dest	*/
	popl	%esi		/* restore register variables		*/
	popl	%edi
	ret

.notfound:
	xorl	%eax,%eax	/* search fails			*/
	popl	%esi		/* restore register variables	*/
	popl	%edi
	ret
