Fenying

Angus’ Home.


28 Oct 2010

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

该文章迁移自作者的旧博客站点。
源地址:http://fenying.blog.163.com/blog/static/10205599320109281544219/

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

StrLen    PROC stdcall, USES edi ecx , pStrEOZ: DWORD

    mov edi, pStrEOZ

    mov ecx, -1

    xor al, al

    cld

    repnz scasb

    mov eax, ecx

    not eax

    ret

StrLen ENDP

StrLenW    PROC stdcall, USES edi ecx ,    pWStrEOZ: DWORD

    mov edi, pWStrEOZ

    mov ecx, -1

    xor ax, ax

    cld

    repnz scasw

    mov eax, ecx

    not eax

    ret

StrLenW ENDP

MemCpy    PROC stdcall, USES edi esi ecx ,    pDst: DWORD, pSrc: DWORD, nBytes: DWORD

    cld

    mov esi, pSrc

    mov edi, pDst

    mov ecx, nBytes

    rep movsb

    mov eax, pDst

    ret

MemCpy ENDP

MemCpyW    PROC stdcall, USES edi esi ecx ,    pDst: DWORD, pSrc: DWORD, nWords: DWORD

    cld

    mov esi, pSrc

    mov edi, pDst

    mov ecx, nWords

    rep movsw

    mov eax, pDst

    ret

MemCpyW ENDP

MemSet    PROC stdcall, USES edi ecx ,    pMem: DWORD, cFill: BYTE, nBytes: DWORD

    cld

    mov edi, pMem

    mov al, cFill

    mov ecx, nBytes

    rep stosb

    ret

MemSet ENDP

MemSetW    PROC stdcall, USES edi ecx ,    pMem: DWORD, wFill: WORD, nWords: DWORD

    cld

    mov edi, pMem

    mov ax, wFill

    mov ecx, nWords

    rep stosw

    ret

MemSetW ENDP

MemCmp    PROC stdcall, USES edi esi ecx ,    pMem1: DWORD, pMem2: DWORD, nBytes: DWORD

    cld

    mov esi, pMem1

    mov edi, pMem2

    mov ecx, nBytes

    repz cmpsb

    jz    l__iszero

    mov eax, esi

    sub eax, pMem1

    jmp finished

l__iszero:

    xor eax, eax

finished:

    ret

MemCmp ENDP

MemCmpW    PROC stdcall, USES edi esi ecx ,    pMem1: DWORD, pMem2: DWORD, nWords: DWORD

    cld

    mov esi, pMem1

    mov edi, pMem2

    mov ecx, nWords

    repz cmpsw

    jz    l__iszero

    mov eax, esi

    sub eax, pMem1

    jmp finished

l__iszero:

    xor eax, eax

finished:

    ret

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

comments powered by Disqus