2010-09-27 02:57:18 +04:00
|
|
|
/* _memmove() Author: Kees J. Bot */
|
|
|
|
/* 2 Jan 1994 */
|
|
|
|
|
|
|
|
/* void *_memmove(void *s1, const void *s2, size_t n) */
|
|
|
|
/* Copy a chunk of memory. Handle overlap. */
|
|
|
|
/* */
|
|
|
|
|
|
|
|
#include "asm.h"
|
|
|
|
|
|
|
|
ENTRY(_memmove)
|
|
|
|
push %ebp
|
|
|
|
movl %esp, %ebp
|
|
|
|
push %esi
|
|
|
|
push %edi
|
|
|
|
movl 8(%ebp), %edi /* String s1 */
|
|
|
|
movl 12(%ebp), %esi /* String s2 */
|
|
|
|
movl 16(%ebp), %ecx /* Length */
|
|
|
|
movl %edi, %eax
|
|
|
|
subl %esi, %eax
|
|
|
|
cmpl %ecx, %eax
|
|
|
|
jb downwards /* if (s2 - s1) < n then copy downwards */
|
|
|
|
LABEL(_memcpy)
|
|
|
|
cld /* Clear direction bit: upwards */
|
|
|
|
cmpl $16, %ecx
|
|
|
|
jb upbyte /* Don't bother being smart with short arrays */
|
|
|
|
movl %esi, %eax
|
|
|
|
orl %edi, %eax
|
|
|
|
testb $1, %al
|
|
|
|
jne upbyte /* Bit 0 set, use byte copy */
|
|
|
|
testb $2, %al
|
|
|
|
jne upword /* Bit 1 set, use word copy */
|
2010-02-12 23:11:35 +03:00
|
|
|
uplword:
|
2010-09-27 02:57:18 +04:00
|
|
|
shrdl $2, %ecx, %eax /* Save low 2 bits of ecx in eax */
|
|
|
|
shrl $2, %ecx
|
|
|
|
|
|
|
|
rep movsl /* Copy longwords. */
|
|
|
|
shldl $2, %eax, %ecx /* Restore excess count */
|
2010-02-12 23:11:35 +03:00
|
|
|
upword:
|
2010-09-27 02:57:18 +04:00
|
|
|
shrl $1, %ecx
|
|
|
|
|
|
|
|
rep movsw /* Copy words */
|
|
|
|
adcl %ecx, %ecx /* One more byte? */
|
2010-02-12 23:11:35 +03:00
|
|
|
upbyte:
|
2010-09-27 02:57:18 +04:00
|
|
|
rep movsb /* Copy bytes */
|
2010-02-12 23:11:35 +03:00
|
|
|
done:
|
2010-09-27 02:57:18 +04:00
|
|
|
movl 8(%ebp), %eax /* Absolutely noone cares about this value */
|
|
|
|
pop %edi
|
|
|
|
pop %esi
|
|
|
|
pop %ebp
|
|
|
|
ret
|
2010-02-12 23:11:35 +03:00
|
|
|
|
2010-09-27 02:57:18 +04:00
|
|
|
/* Handle bad overlap by copying downwards, don't bother to do word copies. */
|
2010-02-12 23:11:35 +03:00
|
|
|
downwards:
|
2010-09-27 02:57:18 +04:00
|
|
|
std /* Set direction bit: downwards */
|
|
|
|
leal -1(%esi,%ecx,1), %esi
|
|
|
|
leal -1(%edi,%ecx,1), %edi
|
|
|
|
|
|
|
|
rep movsb /* Copy bytes */
|
|
|
|
cld
|
|
|
|
jmp done
|