xref: /titanic_41/usr/src/lib/libc/i386/gen/memccpy.s (revision 989f28072d20c73ae0955d6a1e3e2fc74831cb39)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26	.file	"memccpy.s"
27
28#include <sys/asm_linkage.h>
29
30	ANSI_PRAGMA_WEAK(memccpy,function)
31
32#include "SYS.h"
33
34	ENTRY(memccpy)
35	pushl	%esi		/ save register variable
36	movl	8(%esp),%eax	/ %eax = address of dest string
37	movl	12(%esp),%esi	/ %esi = address of source string
38	movb	16(%esp),%dh	/ %dh = character to search for
39	movl	20(%esp),%ecx	/ %ecx = length to go still
40.loop:
41	decl	%ecx		/ decrement bytes to go
42	jl	.notfound
43	movb	(%esi),%dl
44	movb	%dl,(%eax)	/ move byte
45	cmpb	%dh,%dl		/ is it the byte sought?
46	je	.found		/ yes
47
48	decl	%ecx		/ decrement bytes to go
49	jl	.notfound
50	movb	1(%esi),%dl
51	movb	%dl,1(%eax)	/ move byte
52	cmpb	%dh,%dl		/ is it the byte sought?
53	je	.found1		/ yes
54
55	decl	%ecx		/ decrement bytes to go
56	jl	.notfound
57	movb	2(%esi),%dl
58	movb	%dl,2(%eax)	/ move byte
59	cmpb	%dh,%dl		/ is it the byte sought?
60	je	.found2		/ yes
61
62	decl	%ecx		/ decrement bytes to go
63	jl	.notfound
64	movb	3(%esi),%dl
65	movb	%dl,3(%eax)	/ move byte
66	addl	$4,%esi
67	addl	$4,%eax
68	cmpb	%dh,%dl		/ is it the byte sought?
69	jne	.loop		/ no
70	decl	%eax
71
72.found:
73	popl	%esi		/ restore register variable
74	incl	%eax		/ return pointer to next byte in dest
75	ret
76
77	.align	4
78.found2:
79	incl	%eax
80.found1:
81	popl	%esi		/ restore register variable
82	addl	$2,%eax		/ return pointer to next byte in dest
83	ret
84
85	.align	4
86.notfound:
87	popl	%esi		/ restore register variable
88	xorl	%eax,%eax	/ search fails
89	ret
90	SET_SIZE(memccpy)
91