summaryrefslogtreecommitdiff
path: root/Core/CPU/x64/x64AsmLib/MemCpy32.asm
blob: a5886b3a2d41a15b10266ac5d6de3d3f6620c48c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
;*************************************************************************
;*************************************************************************
;**                                                                     **
;**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
;**                                                                     **
;**                       All Rights Reserved.                          **
;**                                                                     **
;**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
;**                                                                     **
;**                       Phone: (770)-246-8600                         **
;**                                                                     **
;*************************************************************************
;*************************************************************************

;*************************************************************************
; $Header: /Alaska/SOURCE/Core/Modules/x64Core/x64AsmLib/MemCpy32.asm 1     10/17/11 1:03p Yakovlevs $
;
; $Revision: 1 $
;
; $Date: 10/17/11 1:03p $
;*************************************************************************
; Revision History
; ----------------
; $Log: /Alaska/SOURCE/Core/Modules/x64Core/x64AsmLib/MemCpy32.asm $
; 
; 1     10/17/11 1:03p Yakovlevs
; [TAG]  		EIP71694 
; 
; 1     10/01/10 5:07p Felixp
; 
; 1     8/24/06 12:57p Felixp
; 
;*************************************************************************
;<AMI_FHDR_START>
;
; Name:
;
; Description:
;
;<AMI_FHDR_END>
;*************************************************************************
.code

;*************************************************************************
;<AMI_PHDR_START>
;
; Name: MemCpy32
;
; Description:
;  VOID MemCpy32(OUT VOID *pDestination, IN VOID *pSource, IN UINTN Count)
; copies Count bytes of memory from Source to Destination.
;
; Input:
;  OUT VOID *pDestination
; Memory address where data shall be copied.  User is responsible for
; allocating the necessary memory resources.
;
;  IN VOID *pSource
; Memory address from where data shall be copied.
;
;  IN UINTN Count
; Number of bytes to copy from pSource.
;
; Output:
;  VOID.
;
; Modified:
;
; Referrals:
;
; Notes:
;  This function checks for overlapping of source and destination and
; selects copy direction that prevents memory corruption.
;
;<AMI_PHDR_END>
;*************************************************************************
public memcpy32
memcpy32:
MemCpy32 proc
		push rdi
		push rsi
        push rbx
		pushf
		mov rsi, rdx ; pSource
		mov rdi, rcx ; pDestination
		mov rcx, r8 ; Count
		mov dl, 0
        ; if pSource > pDestination CopyForward
		mov rax, rsi
		sub rax, rdi ; rax = pSource-pDestination
		jnb CopyForward; if pSource-pDestination > 0 CopyForward
        ; if pSource+Count < pDestination then CopyForward
		lea rbx, [rsi+rcx] ; rbx = pSource + Count
		neg rax ; rax = pDestination - pSource
		cmp rbx, rdi
		jb CopyForward ; if (pSource + Count < pDestination ) CopyForward
        ; Copy Backward
		mov rsi, rbx; rsi = pSource + Count
		lea rdi, [rdi+rcx]; rdi = pDestination + Count
		mov dl, 1; Flag to indicate that we are copying backward
		std; set direction flag to copy backward
CopyForward:
		cmp rcx, 4 ; if (Counter<4) copy byte by byte
		jb m8
		cmp rax, 4 ; if (pDestination - pSource < 4) copy byte by byte
		jb m8
        ; if pSource and pDestination are not 4 byte aligned
		; Calculate 4-(Buffer%4), which is a number of bytes we have to copy to align the buffer
        ; if this number if the same for source and destinations
        ; copy several bytes to align them
		; otherwise proceed to DWORD copy
		mov rax, rsi
		mov rbx, rdi
		and rax, 3
		and rbx, 3
		test dl, dl
		jz skip1
		dec rsi
		dec rdi
skip1:
		cmp rax, rbx
		jne m32
		test rax, rax
		jz m32
		test dl, dl
		jnz skip_nz1
		neg rax
		add rax, 4
skip_nz1:
		xchg rax, rcx
		sub rax, rcx
		rep movsb
		mov rcx, rax
m32:
		test dl, dl
		jz skip2
		sub rsi, 3
		sub rdi, 3
skip2:
		mov rax, rcx
		shr rcx, 2
		rep movsd
		and rax, 3
		jz MemCpuEnd
		test dl, dl
		jz skip3
		add rsi, 4
		add rdi, 4
skip3:
		mov rcx, rax
m8:
		test dl, dl
		jz skip4
		dec rsi
		dec rdi
skip4:
		rep movsb
MemCpuEnd:
		popf
        pop rbx
		pop rsi
		pop rdi
        ret
MemCpy32 endp

END
;*************************************************************************
;*************************************************************************
;**                                                                     **
;**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
;**                                                                     **
;**                       All Rights Reserved.                          **
;**                                                                     **
;**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
;**                                                                     **
;**                       Phone: (770)-246-8600                         **
;**                                                                     **
;*************************************************************************