strcpy的内联实现
发布日期:2021-06-30 22:10:23 浏览次数:2 分类:技术文章

本文共 2136 字,大约阅读时间需要 7 分钟。

前言

strcpy在release版中都会内联实现.

为了突出strcpy内联,封装了一个fnStrcpy做试验.
strcpy算串长度和拷贝都很简洁.
拷贝时,优先按照4字节拷贝,最后不足4字节的串尾部分,再按照1字节拷贝.

IDA笔记

.text:00401000 ; =============== S U B R O U T I N E =======================================.text:00401000.text:00401000.text:00401000 ; int __cdecl fnStrcpy(char *pcDst, char *pcSrc).text:00401000 fnStrcpy        proc near               ; CODE XREF: sub_401090+24p.text:00401000.text:00401000 pcDst           = dword ptr  4.text:00401000 pcSrc           = dword ptr  8.text:00401000.text:00401000                 push    esi.text:00401001                 push    edi             ; save env.text:00401001                                         ; ;.text:00401002                 mov     edi, [esp+8+pcSrc].text:00401006                 or      ecx, 0FFFFFFFFh.text:00401009                 xor     eax, eax.text:0040100B                 repne scasb.text:0040100D                 not     ecx             ; ecx 是带\0的串长度.text:0040100D                                         ; ;.text:0040100F                 sub     edi, ecx        ; scasb 使 pSrc向右挪了ecx个字节,再将pSrc归位.text:0040100F                                         ; ;.text:00401011                 mov     eax, ecx        ; 备份串长度.text:00401011                                         ; ;.text:00401013                 mov     esi, edi.text:00401015                 mov     edi, [esp+8+pcDst].text:00401019                 shr     ecx, 2          ; 为了效率, 先按照4个字节(DWORD)拷贝.text:0040101C                 rep movsd               ; movsd : copy DWORD from esi to edi, count = ecx.text:0040101E                 mov     ecx, eax        ; 还原字符串长度.text:00401020                 and     ecx, 3          ; 不足4个字节的目标字符串,再按照字节拷贝.text:00401023                 rep movsb               ; movsb : copy byte from esi to edi, count = ecx.text:00401023                                         ; ;.text:00401025                 pop     edi             ; restore env.text:00401026                 pop     esi.text:00401027                 retn.text:00401027 fnStrcpy        endp.text:00401027.text:00401027 ; ---------------------------------------------------------------------------

转载地址:https://lostspeed.blog.csdn.net/article/details/52515872 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:'inline' is the only legal storage class for constructors
下一篇:还原自己写的冒泡排序

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月13日 06时56分14秒