xref: /illumos-gate/usr/src/lib/libc/i386/gen/strcat.S (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
1*5d9d9091SRichard Lowe/*
2*5d9d9091SRichard Lowe * CDDL HEADER START
3*5d9d9091SRichard Lowe *
4*5d9d9091SRichard Lowe * The contents of this file are subject to the terms of the
5*5d9d9091SRichard Lowe * Common Development and Distribution License (the "License").
6*5d9d9091SRichard Lowe * You may not use this file except in compliance with the License.
7*5d9d9091SRichard Lowe *
8*5d9d9091SRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5d9d9091SRichard Lowe * or http://www.opensolaris.org/os/licensing.
10*5d9d9091SRichard Lowe * See the License for the specific language governing permissions
11*5d9d9091SRichard Lowe * and limitations under the License.
12*5d9d9091SRichard Lowe *
13*5d9d9091SRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each
14*5d9d9091SRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5d9d9091SRichard Lowe * If applicable, add the following below this CDDL HEADER, with the
16*5d9d9091SRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
17*5d9d9091SRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
18*5d9d9091SRichard Lowe *
19*5d9d9091SRichard Lowe * CDDL HEADER END
20*5d9d9091SRichard Lowe */
21*5d9d9091SRichard Lowe/*
22*5d9d9091SRichard Lowe * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
23*5d9d9091SRichard Lowe * Use is subject to license terms.
24*5d9d9091SRichard Lowe */
25*5d9d9091SRichard Lowe
26*5d9d9091SRichard Lowe	.file	"strcat.s"
27*5d9d9091SRichard Lowe
28*5d9d9091SRichard Lowe/
29*5d9d9091SRichard Lowe/ strcat(s1, s2)
30*5d9d9091SRichard Lowe/
31*5d9d9091SRichard Lowe/ Concatenates s2 on the end of s1.  s1's space must be large enough.
32*5d9d9091SRichard Lowe/ Returns s1.
33*5d9d9091SRichard Lowe/
34*5d9d9091SRichard Lowe/ Fast assembly language version of the following C-program strcat
35*5d9d9091SRichard Lowe/ which represents the `standard' for the C-library.
36*5d9d9091SRichard Lowe/
37*5d9d9091SRichard Lowe/	char *
38*5d9d9091SRichard Lowe/	strcat(char *s1, const char *s2)
39*5d9d9091SRichard Lowe/	{
40*5d9d9091SRichard Lowe/		char	*os1 = s1;
41*5d9d9091SRichard Lowe/
42*5d9d9091SRichard Lowe/		while (*s1++)
43*5d9d9091SRichard Lowe/			;
44*5d9d9091SRichard Lowe/		--s1;
45*5d9d9091SRichard Lowe/		while (*s1++ = *s2++)
46*5d9d9091SRichard Lowe/			;
47*5d9d9091SRichard Lowe/		return (os1);
48*5d9d9091SRichard Lowe/	}
49*5d9d9091SRichard Lowe/
50*5d9d9091SRichard Lowe/ In this assembly language version, the following expression is used
51*5d9d9091SRichard Lowe/ to check if a 32-bit word data contains a null byte or not:
52*5d9d9091SRichard Lowe/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
53*5d9d9091SRichard Lowe/ If the above expression geneates a value other than 0x80808080,
54*5d9d9091SRichard Lowe/ that means the 32-bit word data contains a null byte.
55*5d9d9091SRichard Lowe/
56*5d9d9091SRichard Lowe
57*5d9d9091SRichard Lowe#include "SYS.h"
58*5d9d9091SRichard Lowe
59*5d9d9091SRichard Lowe	ENTRY(strcat)
60*5d9d9091SRichard Lowe	pushl	%edi			/ save register variable
61*5d9d9091SRichard Lowe	/ find a null byte in destination string
62*5d9d9091SRichard Lowe	movl	8(%esp), %edi		/ %edi = destination string address
63*5d9d9091SRichard Lowe	testl	$3, %edi		/ if %edi not word aligned
64*5d9d9091SRichard Lowe	jnz	.L1			/ goto .L1
65*5d9d9091SRichard Lowe	.align	4
66*5d9d9091SRichard Lowe.L2:
67*5d9d9091SRichard Lowe	movl	(%edi), %edx		/ move 1 word from (%edi) to %edx
68*5d9d9091SRichard Lowe	movl	$0x7f7f7f7f, %ecx
69*5d9d9091SRichard Lowe	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
70*5d9d9091SRichard Lowe	addl	$4, %edi		/ next word
71*5d9d9091SRichard Lowe	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
72*5d9d9091SRichard Lowe	orl	%edx, %ecx		/ %ecx |= %edx
73*5d9d9091SRichard Lowe	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
74*5d9d9091SRichard Lowe	cmpl	$0x80808080, %ecx	/ if no null byte in this word
75*5d9d9091SRichard Lowe	je	.L2			/ goto .L2
76*5d9d9091SRichard Lowe	subl	$4, %edi		/ post-incremented
77*5d9d9091SRichard Lowe.L1:
78*5d9d9091SRichard Lowe	cmpb	$0, (%edi)		/ if a byte in (%edi) is null
79*5d9d9091SRichard Lowe	je	.L3			/ goto .L3
80*5d9d9091SRichard Lowe	incl	%edi			/ next byte
81*5d9d9091SRichard Lowe	testl	$3, %edi		/ if %edi not word aligned
82*5d9d9091SRichard Lowe	jnz	.L1			/ goto .L1
83*5d9d9091SRichard Lowe	jmp	.L2			/ goto .L2 (%edi word aligned)
84*5d9d9091SRichard Lowe	.align	4
85*5d9d9091SRichard Lowe.L3:
86*5d9d9091SRichard Lowe	/ %edi points to a null byte in destination string
87*5d9d9091SRichard Lowe	movl	12(%esp), %eax		/ %eax = source string address
88*5d9d9091SRichard Lowe	testl	$3, %eax		/ if %eax not word aligned
89*5d9d9091SRichard Lowe	jnz	.L4			/ goto .L4
90*5d9d9091SRichard Lowe	.align	4
91*5d9d9091SRichard Lowe.L5:
92*5d9d9091SRichard Lowe	movl	(%eax), %edx		/ move 1 word from (%eax) to %edx
93*5d9d9091SRichard Lowe	movl	$0x7f7f7f7f, %ecx
94*5d9d9091SRichard Lowe	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
95*5d9d9091SRichard Lowe	addl	$4, %eax		/ next word
96*5d9d9091SRichard Lowe	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
97*5d9d9091SRichard Lowe	orl	%edx, %ecx		/ %ecx |= %edx
98*5d9d9091SRichard Lowe	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
99*5d9d9091SRichard Lowe	cmpl	$0x80808080, %ecx	/ if null byte in this word
100*5d9d9091SRichard Lowe	jne	.L7			/ goto .L7
101*5d9d9091SRichard Lowe	movl	%edx, (%edi)		/ copy this word to (%edi)
102*5d9d9091SRichard Lowe	addl	$4, %edi		/ next word
103*5d9d9091SRichard Lowe	jmp	.L5			/ goto .L5
104*5d9d9091SRichard Lowe.L7:
105*5d9d9091SRichard Lowe	subl	$4, %eax		/ post-incremented
106*5d9d9091SRichard Lowe	.align	4
107*5d9d9091SRichard Lowe.L4:
108*5d9d9091SRichard Lowe	movb	(%eax), %dl		/ %dl = a byte in (%eax)
109*5d9d9091SRichard Lowe	cmpb	$0, %dl			/ compare %dl with a null byte
110*5d9d9091SRichard Lowe	movb	%dl, (%edi)		/ copy %dl to (%edi)
111*5d9d9091SRichard Lowe	je	.L6			/ if %dl is a null, goto .L6
112*5d9d9091SRichard Lowe	incl	%eax			/ next byte
113*5d9d9091SRichard Lowe	incl	%edi			/ next byte
114*5d9d9091SRichard Lowe	testl	$3, %eax		/ if %eax not word aligned
115*5d9d9091SRichard Lowe	jnz	.L4			/ goto .L4
116*5d9d9091SRichard Lowe	jmp	.L5			/ goto .L5 (%eax word aligned)
117*5d9d9091SRichard Lowe	.align	4
118*5d9d9091SRichard Lowe.L6:
119*5d9d9091SRichard Lowe	movl	8(%esp), %eax		/ return the destination address
120*5d9d9091SRichard Lowe	popl	%edi			/ restore register variable
121*5d9d9091SRichard Lowe	ret
122*5d9d9091SRichard Lowe	SET_SIZE(strcat)
123