C标准库内存操作函数汇编源代码

该文章根据 CC-BY-4.0 协议发表,转载请遵循该协议。
本文地址:https://fenying.net/post/2010/10/28/c-memory-functions-source-in-assembly/

C语言标准库内,内存操作函数的汇编源代码。

  1StrLen    PROC stdcall, USES edi ecx , pStrEOZ: DWORD
  2
  3    mov edi, pStrEOZ
  4
  5    mov ecx, -1
  6
  7    xor al, al
  8
  9    cld
 10
 11    repnz scasb
 12
 13    mov eax, ecx
 14
 15    not eax
 16
 17    ret
 18
 19StrLen ENDP
 20
 21StrLenW    PROC stdcall, USES edi ecx ,    pWStrEOZ: DWORD
 22
 23    mov edi, pWStrEOZ
 24
 25    mov ecx, -1
 26
 27    xor ax, ax
 28
 29    cld
 30
 31    repnz scasw
 32
 33    mov eax, ecx
 34
 35    not eax
 36
 37    ret
 38
 39StrLenW ENDP
 40
 41MemCpy    PROC stdcall, USES edi esi ecx ,    pDst: DWORD, pSrc: DWORD, nBytes: DWORD
 42
 43    cld
 44
 45    mov esi, pSrc
 46
 47    mov edi, pDst
 48
 49    mov ecx, nBytes
 50
 51    rep movsb
 52
 53    mov eax, pDst
 54
 55    ret
 56
 57MemCpy ENDP
 58
 59MemCpyW    PROC stdcall, USES edi esi ecx ,    pDst: DWORD, pSrc: DWORD, nWords: DWORD
 60
 61    cld
 62
 63    mov esi, pSrc
 64
 65    mov edi, pDst
 66
 67    mov ecx, nWords
 68
 69    rep movsw
 70
 71    mov eax, pDst
 72
 73    ret
 74
 75MemCpyW ENDP
 76
 77MemSet    PROC stdcall, USES edi ecx ,    pMem: DWORD, cFill: BYTE, nBytes: DWORD
 78
 79    cld
 80
 81    mov edi, pMem
 82
 83    mov al, cFill
 84
 85    mov ecx, nBytes
 86
 87    rep stosb
 88
 89    ret
 90
 91MemSet ENDP
 92
 93MemSetW    PROC stdcall, USES edi ecx ,    pMem: DWORD, wFill: WORD, nWords: DWORD
 94
 95    cld
 96
 97    mov edi, pMem
 98
 99    mov ax, wFill
100
101    mov ecx, nWords
102
103    rep stosw
104
105    ret
106
107MemSetW ENDP
108
109MemCmp    PROC stdcall, USES edi esi ecx ,    pMem1: DWORD, pMem2: DWORD, nBytes: DWORD
110
111    cld
112
113    mov esi, pMem1
114
115    mov edi, pMem2
116
117    mov ecx, nBytes
118
119    repz cmpsb
120
121    jz    l__iszero
122
123    mov eax, esi
124
125    sub eax, pMem1
126
127    jmp finished
128
129l__iszero:
130
131    xor eax, eax
132
133finished:
134
135    ret
136
137MemCmp ENDP
138
139MemCmpW    PROC stdcall, USES edi esi ecx ,    pMem1: DWORD, pMem2: DWORD, nWords: DWORD
140
141    cld
142
143    mov esi, pMem1
144
145    mov edi, pMem2
146
147    mov ecx, nWords
148
149    repz cmpsw
150
151    jz    l__iszero
152
153    mov eax, esi
154
155    sub eax, pMem1
156
157    jmp finished
158
159l__iszero:
160
161    xor eax, eax
162
163finished:
164
165    ret
166
167MemCmpW ENDP
comments powered by Disqus