strcpy的内部机制

 strcpy proc \	 ;strcpy 函数开始!!!
  dst:ptr byte, \	 ;表明两个参数是char *
  src:ptr byte

  OPTION PROLOGUE:NONE, EPILOGUE:NONE

  push edi ; preserve edi 保存目的串指针要用到的edi寄存器
  mov edi,[esp+8] ; edi points to dest string 把目的串指针赋给edi寄存器
  jmp short copy_start ; 跳到开始复制的代码

strcpy endp




copy_start::
  mov ecx,[esp+0ch] ; ecx -> sorc string 把源串指针赋给ecx寄存器
  test ecx,3 ; test if string is aligned on 32 bits 测试源串地址是否安
  ;4字节对齐
  je short main_loop_entrance ;如果源串地址是按4字节对齐就跳到main_loop_entrance


src_misaligned: ; simple byte loop until string is aligned 如果源串地址不是
  ;4字节对齐
  mov dl,byte ptr [ecx]
  add ecx,1
  test dl,dl	 ;测试是不是字符串结束
  je short byte_0 ;是字符串结束标志则跳到后面的byte_0   
  mov [edi],dl	 ;不是字符串结束标志就把dl中的字符复制到目的串
  add edi,1	 ;目的串指针加1
  test ecx,3	 ;再次测试源串指针加1后是不是4字节对齐了
  jne short src_misaligned ;不是,就又返回进行单字节复制
  jmp short main_loop_entrance ;是4字节对齐了,跳到main_loop_entrance


main_loop: ; edx contains first dword of sorc string
  mov [edi],edx ; store one more dword复制不含结束标志的4字节数据到目的串
  add edi,4 ; kick dest pointer目的串指针加4


main_loop_entrance:	 ;在此开始了4字节的快速内存读取
  mov edx,7efefeffh
  mov eax,dword ptr [ecx] ; read 4 bytes ;从源串读4字节数据

  add edx,eax
  xor eax,-1

  xor eax,edx
  mov edx,[ecx] ; it's in cache now

  add ecx,4 ; kick dest pointer源串指针加4
  test eax,81010100h	 ; 判断这4字节里是否含有字符串结束标志

  je short main_loop	 ; 没有结束标志,跳到main_loop
  ; found zero byte in the loop

; main_loop_end:
    
test dl,dl ; is it byte 0 ;开始判断在读入的4字节是不是第1个字节含有结束
  ;标志,把该字节中的'\0'复制到目的串尾部
  je short byte_0

  test dh,dh ; is it byte 1 ;开始判断在读入的4字节是不是第2个字节含有结束
  ;标志,把该字节中的'\0'复制到目的串尾部
  je short byte_1

  test edx,00ff0000h ; is it byte 2 ;开始判断在读入的4字节是不是第3个字节含有结束标
  ;志, 把该字节中的'\0'复制到目的串尾部
  je short byte_2

  test edx,0ff000000h ; is it byte 3 ;开始判断在读入的4字节是不是第4个字节含有结束
  ;标志,然后跳到相应的函数出口并把该字节中的'\0'复制到目的串尾部
  je short byte_3
  jmp short main_loop ; taken if bits 24-30 are clear and bit
  ; 31 is set

byte_3:
  mov [edi],edx	 ;复制前3字节有效数据和'\0'到目的串尾部
  mov eax,[esp+8] ; return in eax pointer to dest string 返回指向目的串开始的指针
  pop edi ;平衡堆栈
  ret	 ;弹出返回地址,传入参数由主调程序负责平衡堆栈

byte_2:
  mov [edi],dx	 ;复制前两字节有效数据到目的串尾部
  mov eax,[esp+8] ; return in eax pointer to dest string 返回指向目的串开始的指针
  mov byte ptr [edi+2],0 ;复制'\0'到目的串尾部
  pop edi ;平衡堆栈
  ret	 ;弹出返回地址,传入参数由主调程序负责平衡堆栈

byte_1:
  mov [edi],dx	 ;复制前一1字节有效数据和'\0'到目的串尾部
  mov eax,[esp+8] ; return in eax pointer to dest string 返回指向目的串开始的指针
  pop edi	 ;平衡堆栈	
  ret	 ;弹出返回地址,传入参数由主调程序负责平衡堆栈

byte_0:
  mov [edi],dl	 ;复制'\0'到目的串尾部
  mov eax,[esp+8] ; return in eax pointer to dest string 返回指向目的串开始的指针
  pop edi	 ;平衡堆栈
  ret	 ;弹出返回地址,传入参数由主调程序负责平衡堆栈 
strcpy是以4个字节为一组复制
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值