Initial project.
[marionette.git] / kernel / libc / memmove.S
blobc7d124d6df6d6bbc0de6d50e2df08d9317e5d728
1 /*
2  *    Copyright (c) 2008 Joshua Phillips. All rights reserved.
3  *  
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions are
6  *  met:
7  *  
8  *    1. Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *    2. Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in
12  *       the documentation and/or other materials provided with the
13  *       distribution.
14  *  
15  *  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
16  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  *  DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
19  *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  *  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  *  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 .global memcpy
29 .global memmove
31 .section .text.memmove
33 memcpy:
34 memmove:
35     pushl %ebp
36     movl %esp,%ebp
37     pushl %esi
38     pushl %edi
39     movl 8(%ebp),%eax // dest
40     movl 12(%ebp),%ecx // src
41     movl %eax,%edi
42     movl %ecx,%esi
43     cmpl %eax,%ecx
44     jb 5f
45     // copy forwards
46     cld
47     movl 16(%ebp),%ecx // count
48     movl %ecx,%edx
49     shrl $2,%ecx // count / 4
50     rep
51     movsl
52     movl %edx,%ecx
53     andl $3,%ecx // count % 4
54     rep  // copy residual bytes
55     movsb
56     jmp 6f
58     // copy backwards
59     std
60     movl 16(%ebp),%ecx // count
61     movl %ecx,%edx
62     addl %ecx,%esi // esi/edi point at byte *after* last byte
63     addl %ecx,%edi
64     shrl $2,%ecx // count / 4
65     testl %ecx,%ecx
66     jz 1f
67     subl $4,%esi // point at last dword
68     subl $4,%edi
69     rep
70     movsl
71     addl $4,%esi
72     addl $4,%edi
74     movl %edx,%ecx
75     andl $3,%ecx // count % 4
76     subl $1,%esi // point at last byte
77     subl $1,%edi
78     rep
79     movsb
80     cld
82     movl 8(%ebp),%eax
83     popl %edi
84     popl %esi
85     movl %ebp,%esp
86     popl %ebp
87     ret