如何查看类似strcpy这样的函数的内部实现

minioreo 2008-11-08 11:32:01
如题
...全文
170 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jkjackstar 2012-06-28
  • 打赏
  • 举报
回复
到底怎么弄啊? 有没有完整的?
太乙 2008-11-09
  • 打赏
  • 举报
回复

page
;***
;char *strcpy(dst, src) - copy one string over another
;
;Purpose:
; Copies the string src into the spot specified by
; dest; assumes enough room.
;
; Algorithm:
; char * strcpy (char * dst, char * src)
; {
; char * cp = dst;
;
; while( *cp++ = *src++ )
; ; /* Copy src over dst */
; return( dst );
; }
;
;Entry:
; char * dst - string over which "src" is to be copied
; const char * src - string to be copied over "dst"
;
;Exit:
; The address of "dst" in EAX
;
;Uses:
; EAX, ECX
;
;Exceptions:
;*******************************************************************************


CODESEG

% public strcat, strcpy ; make both functions available
strcpy proc
push edi ; preserve edi
mov edi,[esp+8] ; edi points to dest string
jmp short copy_start

strcpy endp

align 16

strcat proc

.FPO ( 0, 2, 0, 0, 0, 0 )

mov ecx,[esp+4] ; ecx -> dest string
push edi ; preserve edi
test ecx,3 ; test if string is aligned on 32 bits
je short find_end_of_dest_string_loop

dest_misaligned: ; simple byte loop until string is aligned
mov al,byte ptr [ecx]
inc ecx
test al,al
je short start_byte_3
test ecx,3
jne short dest_misaligned

align 4

find_end_of_dest_string_loop:
mov eax,dword ptr [ecx] ; read 4 bytes
mov edx,7efefeffh
add edx,eax
xor eax,-1
xor eax,edx
add ecx,4
test eax,81010100h
je short find_end_of_dest_string_loop
; found zero byte in the loop
mov eax,[ecx - 4]
test al,al ; is it byte 0
je short start_byte_0
test ah,ah ; is it byte 1
je short start_byte_1
test eax,00ff0000h ; is it byte 2
je short start_byte_2
test eax,0ff000000h ; is it byte 3
je short start_byte_3
jmp short find_end_of_dest_string_loop
; taken if bits 24-30 are clear and bit
; 31 is set
start_byte_3:
lea edi,[ecx - 1]
jmp short copy_start
start_byte_2:
lea edi,[ecx - 2]
jmp short copy_start
start_byte_1:
lea edi,[ecx - 3]
jmp short copy_start
start_byte_0:
lea edi,[ecx - 4]
; jmp short copy_start

; edi points to the end of dest string.
copy_start::
mov ecx,[esp+0ch] ; ecx -> sorc string
test ecx,3 ; test if string is aligned on 32 bits
je short main_loop_entrance

src_misaligned: ; simple byte loop until string is aligned
mov dl,byte ptr [ecx]
inc ecx
test dl,dl
je short byte_0
mov [edi],dl
inc edi
test ecx,3
jne short src_misaligned
jmp short main_loop_entrance

main_loop: ; edx contains first dword of sorc string
mov [edi],edx ; store one more dword
add edi,4 ; kick dest pointer
main_loop_entrance:
mov edx,7efefeffh
mov eax,dword ptr [ecx] ; read 4 bytes

add edx,eax
xor eax,-1

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

add ecx,4 ; kick dest pointer
test eax,81010100h

je short main_loop
; found zero byte in the loop
; main_loop_end:
test dl,dl ; is it byte 0
je short byte_0
test dh,dh ; is it byte 1
je short byte_1
test edx,00ff0000h ; is it byte 2
je short byte_2
test edx,0ff000000h ; is it byte 3
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
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
pop edi
ret
byte_1:
mov [edi],dx
mov eax,[esp+8] ; return in eax pointer to dest string
pop edi
ret
byte_0:
mov [edi],dl
mov eax,[esp+8] ; return in eax pointer to dest string
pop edi
ret

strcat endp

end






查看方法:

在vc6.0的调试状态下,在即将执行到strcpy的时候,按f11进入函数体内部,就能看到!

LogWarning 2008-11-09
  • 打赏
  • 举报
回复
VC的crt实现在:Microsoft Visual Studio/VC/crt/src/
R9R9R9 2008-11-09
  • 打赏
  • 举报
回复
其实就是那些汇编代码的注释。
R9R9R9 2008-11-09
  • 打赏
  • 举报
回复
查看方法:

在调试状态下,在即将执行到strcpy的时候,按f11进入函数体内部,就能看到!

就在汇编代码的上面几行。向上向上
minioreo 2008-11-09
  • 打赏
  • 举报
回复
多谢4楼 我看到汇编代码了 你的C源码是在哪看到的?还是从汇编反向工程出来的?
我在C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src里只能找到strcat的定义
minioreo 2008-11-08
  • 打赏
  • 举报
回复
哦 windows平台呢?想看一些C库函数的实现
liecs 2008-11-08
  • 打赏
  • 举报
回复
linux内核源码
可以 code.google.com 之
不同的系统实现的方式也是不同的

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧