Home | History | Annotate | Download | only in Ia32
      1 ;------------------------------------------------------------------------------
      2 ;
      3 ; Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
      4 ; This program and the accompanying materials
      5 ; are licensed and made available under the terms and conditions of the BSD License
      6 ; which accompanies this distribution.  The full text of the license may be found at
      7 ; http://opensource.org/licenses/bsd-license.php
      8 ;
      9 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     10 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     11 ;
     12 ; Module Name:
     13 ;
     14 ;   CopyMem.asm
     15 ;
     16 ; Abstract:
     17 ;
     18 ;   memcpy function
     19 ;
     20 ; Notes:
     21 ;
     22 ;------------------------------------------------------------------------------
     23 
     24     .686
     25     .model  flat,C
     26     .xmm
     27     .code
     28 
     29 ;------------------------------------------------------------------------------
     30 ;  VOID *
     31 ;  memcpy (
     32 ;    IN VOID   *Destination,
     33 ;    IN VOID   *Source,
     34 ;    IN UINTN  Count
     35 ;    );
     36 ;------------------------------------------------------------------------------
     37 memcpy  PROC    USES    esi edi
     38     mov     esi, [esp + 16]             ; esi <- Source
     39     mov     edi, [esp + 12]             ; edi <- Destination
     40     mov     edx, [esp + 20]             ; edx <- Count
     41     lea     eax, [esi + edx - 1]        ; eax <- End of Source
     42     cmp     esi, edi
     43     je      @CopyMemDone
     44     cmp     edx, 0
     45     je      @CopyMemDone
     46     cmp     esi, edi
     47     jae     @F
     48     cmp     eax, edi                    ; Overlapped?
     49     jae     @CopyBackward               ; Copy backward if overlapped
     50 @@:
     51     xor     ecx, ecx
     52     sub     ecx, edi
     53     and     ecx, 15                     ; ecx + edi aligns on 16-byte boundary
     54     jz      @F
     55     cmp     ecx, edx
     56     cmova   ecx, edx
     57     sub     edx, ecx                    ; edx <- remaining bytes to copy
     58     rep     movsb
     59 @@:
     60     mov     ecx, edx
     61     and     edx, 15
     62     shr     ecx, 4                      ; ecx <- # of DQwords to copy
     63     jz      @CopyBytes
     64     add     esp, -16
     65 @@:
     66     movdqu  xmm0, [esi]                 ; esi may not be 16-bytes aligned
     67     movdqa  [edi], xmm0                 ; edi should be 16-bytes aligned
     68     add     esi, 16
     69     add     edi, 16
     70     loop    @B
     71     add     esp, 16                     ; stack cleanup
     72     jmp     @CopyBytes
     73 @CopyBackward:
     74     mov     esi, eax                    ; esi <- Last byte in Source
     75     lea     edi, [edi + edx - 1]        ; edi <- Last byte in Destination
     76     std
     77 @CopyBytes:
     78     mov     ecx, edx
     79     rep     movsb
     80     cld
     81 @CopyMemDone:    
     82     mov     eax, [esp + 12]
     83     ret
     84 memcpy  ENDP
     85 
     86     END
     87