strcat strcpy函数源码
以下程序段取自glibc,做了简单修改.
/* Copy SRC to DEST. */
char *
strcpy (dest, src)
char *dest;
const char *src;
{
char c;
char *s = (char *)src;
const ptrdiff_t off = dest - s - 1;
do
{
c = *s++;
s[off] = c;
}
while (c != '\0');
return dest;
}
以下程序段取自VC6.0
/*
下面是strcpy库函数的实现,因为库函数讲究的就是精练、简洁。所以没有其他的异常处理代码。主要的异常处理还是交给了函数的使用者,在调用前请确认目的和源指针是否都存在(不能为Null),请确认目标指针空间是否大于源字符串的空间。
Copies the string src into the spot specified by dest;
assumes enough room.
目标指针空间必须大于源字符串空间。
*/
char * __cdecl strcpy(char * dst, const char * src)
{
char * cp = dst;
while( *cp++ = *src++ )
; /* Copy src over dst */
return( dst );
}
glibc中取出的那段,其实我还删了一些,主要是范围检查,而取自VC的这段却是十分简洁.
这并不能说明哪个更好,看看这些源代码,使自己能提高编码水平吧.
===========================================================
Assembly code
page ,132
title strcat - concatenate (append) one string to another
;***
;strcat.asm - contains strcat() and strcpy() routines
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;Purpose:
; STRCAT concatenates (appends) a copy of the source string to the
; end of the destination string, returning the destination string.
;
;*******************************************************************************
.xlist
include cruntime.inc
.list
page
;***
;char *strcat(dst, src) - concatenate (append) one string to another
;
;Purpose:
; Concatenates src onto the end of dest. Assumes enough
; space in dest.
;
; Algorithm:
; char * strcat (char * dst, char * src)
; {
; char * cp = dst;
;
; while( *cp )
; ++cp; /* Find end of dst */
; while( *cp++ = *src++ )
; ; /* Copy src to end of dst */
; return( dst );
; }
;
;Entry:
; char *dst - string to which "src" is to be appended
; const char *src - string to be appended to the end of "dst"
;
;Exit:
; The address of "dst" in EAX
;
;Uses:
; EAX, ECX
;
;Exceptions:
;
;*******************************************************************************
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 \
dst:ptr byte, \
src:ptr byte
OPTION PROLOGUE:NONE, EPILOGUE:NONE
push edi ; preserve edi
mov edi,[esp+8] ; edi points to dest string
jmp short copy_start
strcpy endp
align 16
strcat proc \
dst:ptr byte, \
src:ptr byte
OPTION PROLOGUE:NONE, EPILOGUE:NONE
.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]
add ecx,1
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]
add ecx,1
test dl,dl
je short byte_0
mov [edi],dl
add edi,1
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
分享到:
相关推荐
资源名称:C语言基础知识视频教程(21集)资源目录:【】C语言基础知识视频(01-03)【】C语言基础知识视频(04-06)【】C语言基础知识视频(07-09)【】C语言基础知识视频(10-12)【】C语言基础知识视频(13-15)【】C语言...
适合有C语言基础的同学,其中包含搜集整理的c与c语言的对比。
关于C语言基础方面的一些练习题,也有一点面试题,带一点嵌入式C基础方面的应用题。
C语言基础知识 C语言基础知识 C语言基础知识 C语言基础知识 C语言基础知识
C语言基础 C语言基础 C语言基础 C语言基础
C语言基础知识大全 C语言基础知识大全 C语言基础知识大全 C语言基础知识大全
C语言基础教程---电子教案 C语言基础教程---电子教案 C语言基础教程---电子教案
C语言基础知识,完整的PPT课件,更容易对C语言进行系统的学习
c语言基础教程 txt版 怎样学习C语言 很多人对学习C语言感到无从下手,经常问我同一个问题:究竟怎样学习C语言?我是一个教师,已经开发了很多年的程序,和很多刚刚起步的人一样,学习的第一个计算机语言就是C语言...
有相当多的c语言的基础例子对于想学好c语言的人 这是一个很好的基础参考例子,一切从基础开始
C语言爱好者网友搜集的学习资料。包括C语言基础、经典C程序100例,以及一些C语言的高级应用。exe格式电子书
C语言基础 C语言基础 C语言基础 C语言基础.zip
C语言基础学习心得总结文档 C语言基础学习心得总结文档 C语言基础学习心得总结文档 C语言基础学习心得总结文档 C语言基础学习心得总结文档 C语言基础学习心得总结文档 C语言基础学习心得总结文档 C语言基础学习心得...
C语言基础源代码.其最基础的基本数据类型到指针,基本可以达到学习iOS的基础.
C语言基础知识大全.doc
c语言基础知识大全,C资料大全
C语言教程之C语言基础.pdf
C语言是在70年代初问世的。一九七八年由美国电话电报公司(AT&T)...但是,在《K&R》中并没有定义一个完整的标准C语言,后来由美国国家标准学会在此基础上制定了一个C 语言标准,于一九八三年发表。通常称之为ANSI C。
标准C语言基础教程 5The programs examined so far have been useful in illustrating the correct structure of C programs and in introducing fundamental C input, output, assignment, and selection ...