+;------------------------------------------------------------------------------\r
+;\r
+; Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>\r
+; This program and the accompanying materials\r
+; are licensed and made available under the terms and conditions of the BSD License\r
+; which accompanies this distribution. The full text of the license may be found at\r
+; http://opensource.org/licenses/bsd-license.php.\r
+;\r
+; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+;\r
+; Module Name:\r
+;\r
+; CopyMem.nasm\r
+;\r
+; Abstract:\r
+;\r
+; CopyMem function\r
+;\r
+; Notes:\r
+;\r
+;------------------------------------------------------------------------------\r
+\r
+ DEFAULT REL\r
+ SECTION .text\r
+\r
+;------------------------------------------------------------------------------\r
+; VOID *\r
+; EFIAPI\r
+; InternalMemCopyMem (\r
+; OUT VOID *DestinationBuffer,\r
+; IN CONST VOID *SourceBuffer,\r
+; IN UINTN Length\r
+; );\r
+;------------------------------------------------------------------------------\r
+global ASM_PFX(InternalMemCopyMem)\r
+ASM_PFX(InternalMemCopyMem):\r
+ push rsi\r
+ push rdi\r
+ mov rsi, rdx ; rsi <- Source\r
+ mov rdi, rcx ; rdi <- Destination\r
+ lea r9, [rsi + r8 - 1] ; r9 <- End of Source\r
+ cmp rsi, rdi\r
+ mov rax, rdi ; rax <- Destination as return value\r
+ jae .0\r
+ cmp r9, rdi\r
+ jae @CopyBackward ; Copy backward if overlapped\r
+.0:\r
+ mov rcx, r8\r
+ and r8, 7\r
+ shr rcx, 3 ; rcx <- # of Qwords to copy\r
+ jz @CopyBytes\r
+ DB 0x49, 0xf, 0x7e, 0xc2 ; movd r10, mm0 (Save mm0 in r10)\r
+.1:\r
+ DB 0xf, 0x6f, 0x6 ; movd mm0, [rsi]\r
+ DB 0xf, 0xe7, 0x7 ; movntq [rdi], mm0\r
+ add rsi, 8\r
+ add rdi, 8\r
+ loop .1\r
+ mfence\r
+ DB 0x49, 0xf, 0x6e, 0xc2 ; movd mm0, r10 (Restore mm0)\r
+ jmp @CopyBytes\r
+@CopyBackward:\r
+ mov rsi, r9 ; rsi <- End of Source\r
+ lea rdi, [rdi + r8 - 1] ; rdi <- End of Destination\r
+ std ; set direction flag\r
+@CopyBytes:\r
+ mov rcx, r8\r
+ rep movsb ; Copy bytes backward\r
+ cld\r
+ pop rdi\r
+ pop rsi\r
+ ret\r
+\r