xref: /openbsd-src/lib/libc/arch/i386/string/memmove.S (revision 447c6881e52f329a5283ae895130c5e2377d00c6)
1*447c6881Stb/*	$OpenBSD: memmove.S,v 1.8 2022/12/04 08:22:13 tb Exp $	*/
25b859c19Sderaadt
35b859c19Sderaadt/*-
4c298c173Stedu * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.  All rights reserved.
55b859c19Sderaadt * Copyright (c) 1990 The Regents of the University of California.
65b859c19Sderaadt * All rights reserved.
75b859c19Sderaadt *
8c298c173Stedu * This code is derived from software contributed to Berkeley by
9c298c173Stedu * William Jolitz.
105b859c19Sderaadt *
115b859c19Sderaadt * Redistribution and use in source and binary forms, with or without
125b859c19Sderaadt * modification, are permitted provided that the following conditions
135b859c19Sderaadt * are met:
145b859c19Sderaadt * 1. Redistributions of source code must retain the above copyright
155b859c19Sderaadt *    notice, this list of conditions and the following disclaimer.
165b859c19Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
175b859c19Sderaadt *    notice, this list of conditions and the following disclaimer in the
185b859c19Sderaadt *    documentation and/or other materials provided with the distribution.
195b859c19Sderaadt * 3. Neither the name of the University nor the names of its contributors
205b859c19Sderaadt *    may be used to endorse or promote products derived from this software
215b859c19Sderaadt *    without specific prior written permission.
225b859c19Sderaadt *
235b859c19Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
245b859c19Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
255b859c19Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
265b859c19Sderaadt * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
275b859c19Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
285b859c19Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
295b859c19Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
305b859c19Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
315b859c19Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
325b859c19Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
335b859c19Sderaadt * SUCH DAMAGE.
345b859c19Sderaadt */
355b859c19Sderaadt
36ea6088e7Sguenther#include "DEFS.h"
375b859c19Sderaadt
385b859c19Sderaadt/*
39c298c173Stedu * Emulate bcopy() by swapping the first two arguments, and jumping
40c298c173Stedu * into memmove(), which handles overlapping regions.
415b859c19Sderaadt */
42*447c6881StbENTRY_NB(bcopy)
43c298c173Stedu	pushl	%esi
44c298c173Stedu	pushl	%edi
45c298c173Stedu	movl	12(%esp),%esi
46c298c173Stedu	movl	16(%esp),%edi
47c298c173Stedu	jmp	docopy
485b859c19Sderaadt
49c298c173Stedu/*
50c298c173Stedu * memmove(caddr_t dst, caddr_t src, size_t len);
51c298c173Stedu * Copy len bytes, coping with overlapping space.
52c298c173Stedu */
535b859c19SderaadtENTRY(memmove)
545b859c19Sderaadt	pushl	%esi
555b859c19Sderaadt	pushl	%edi
565b859c19Sderaadt	movl	12(%esp),%edi
575b859c19Sderaadt	movl	16(%esp),%esi
58c298c173Stedudocopy:
595b859c19Sderaadt	movl	20(%esp),%ecx
60c298c173Stedu	movl	%edi,%eax
61c298c173Stedu	subl	%esi,%eax
62c298c173Stedu	cmpl	%ecx,%eax		# overlapping?
63c298c173Stedu	jb	1f
64ea6088e7Sguenther#ifdef memcpy_in_asm
65c298c173Stedu	jmp	docopyf			# nope
66ea6088e7Sguenther
67c298c173Stedu/*
68c298c173Stedu * memcpy() doesn't worry about overlap and always copies forward
69c298c173Stedu */
70ea6088e7SguentherENTRY(memcpy)
71c298c173Stedu	pushl	%esi
72c298c173Stedu	pushl	%edi
73c298c173Stedu	movl	12(%esp),%edi
74c298c173Stedu	movl	16(%esp),%esi
75c298c173Stedu	movl	20(%esp),%ecx
76c298c173Stedudocopyf:
77ea6088e7Sguenther#endif
78c298c173Stedu	movl	%edi,%eax		# setup return value for memcpy/memmove
79c298c173Stedu	shrl	$2,%ecx			# copy by 32-bit words
805b859c19Sderaadt	rep
815b859c19Sderaadt	movsl
82c298c173Stedu	movl	20(%esp),%ecx
83c298c173Stedu	andl	$3,%ecx			# any bytes left?
845b859c19Sderaadt	rep
855b859c19Sderaadt	movsb
865b859c19Sderaadt	popl	%edi
875b859c19Sderaadt	popl	%esi
885b859c19Sderaadt	ret
89c298c173Stedu
90c298c173Stedu	_ALIGN_TEXT
91c298c173Stedu1:	movl	%edi,%eax		# setup return value for memmove
92c298c173Stedu	addl	%ecx,%edi		# copy backward
935b859c19Sderaadt	addl	%ecx,%esi
945b859c19Sderaadt	std
95c298c173Stedu	andl	$3,%ecx			# any fractional bytes?
965b859c19Sderaadt	decl	%edi
975b859c19Sderaadt	decl	%esi
985b859c19Sderaadt	rep
995b859c19Sderaadt	movsb
100c298c173Stedu	movl	20(%esp),%ecx		# copy remainder by 32-bit words
1015b859c19Sderaadt	shrl	$2,%ecx
1025b859c19Sderaadt	subl	$3,%esi
1035b859c19Sderaadt	subl	$3,%edi
1045b859c19Sderaadt	rep
1055b859c19Sderaadt	movsl
1065b859c19Sderaadt	popl	%edi
1075b859c19Sderaadt	popl	%esi
1085b859c19Sderaadt	cld
1095b859c19Sderaadt	ret
110ea6088e7Sguenther#ifdef memcpy_in_asm
111ea6088e7SguentherEND_BUILTIN(memcpy)
112ea6088e7Sguenther#endif
113ea6088e7SguentherEND_BUILTIN(memmove)
1149b9d2a55SguentherEND_WEAK(bcopy)
115