拷贝字符串函数小问题

giant7 2013-06-01 06:41:16
本人写了个拷贝字符串函数(即实现函数strcpy()),欢迎各位拍砖。


char *strcpy(char *strDest, const char *strSrc)  
{
assert((strDest!=NULL)&&(strSrc!=NULL));
char *address=strDest;
while((*strDest++=*strSrc++)!='\0');
return address;
}


请大家帮忙看下,代码有什么地方需要改进,谢谢!
...全文
199 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-06-03
  • 打赏
  • 举报
回复
http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express 点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装 再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\intel\strcat.asm
        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

giant7 2013-06-02
  • 打赏
  • 举报
回复
引用 11 楼 worldy 的回复:
没改完就发出了,晕 int strcpy(char *strDest, const char *strSrc) //返回地址没有意思,返回拷贝的长度更有用 { //assert((strDest!=NULL)&&(strSrc!=NULL)); int n=0; if((strDest!=NULL)&&(strSrc!=NULL)) while((*strDest++=*strSrc++)!='\0') n++; return n; }
受教了,谢谢!
worldy 2013-06-01
  • 打赏
  • 举报
回复
没改完就发出了,晕 int strcpy(char *strDest, const char *strSrc) //返回地址没有意思,返回拷贝的长度更有用 { //assert((strDest!=NULL)&&(strSrc!=NULL)); int n=0; if((strDest!=NULL)&&(strSrc!=NULL)) while((*strDest++=*strSrc++)!='\0') n++; return n; }
worldy 2013-06-01
  • 打赏
  • 举报
回复
int strcpy(char *strDest, const char *strSrc) //返回地址没有意思,返回拷贝的长度更有用 { //assert((strDest!=NULL)&&(strSrc!=NULL)); int n=0; if((strDest!=NULL)&&(strSrc!=NULL)) while((*strDest++=*strSrc++)!='\0') ; return address; }
AnYidan 2013-06-01
  • 打赏
  • 举报
回复
assert 只在 debug 版本才有效,用 if 更好
AnYidan 2013-06-01
  • 打赏
  • 举报
回复
引用 3 楼 ggxxkkll 的回复:
[quote=引用 2 楼 Idle_Cloud 的回复:] 没问题,也不需要改进了。 我到是还见过一个写法。 while((*strDest++=*strSrc++)); 也可以。
你这个没有字符串终止判断标识'\0', 是不是语句(*strDest++=*strSrc++)会自动判断?[/quote] '\0' = 0 --> while(0)
绯红女王 2013-06-01
  • 打赏
  • 举报
回复
额,这个没什么问题。LZ看看还能不能精简下代码。
void copy(char to[],char from[])
{
	int i;
	while((to[i]=from[i])!='\0')
		++i;
}
zcronline 2013-06-01
  • 打赏
  • 举报
回复
引用 5 楼 ggxxkkll 的回复:
[quote=引用 4 楼 zcronline 的回复:] 没什么问题 K&R C中实现的strcpy这样写(The C programming language P105)

void strcpy (char *s ,char *t)
{
    while((*s++=*t++)!='\0')
         ;
}
就是跟我的一样了呗[/quote]恩
giant7 2013-06-01
  • 打赏
  • 举报
回复
引用 4 楼 zcronline 的回复:
没什么问题 K&R C中实现的strcpy这样写(The C programming language P105)

void strcpy (char *s ,char *t)
{
    while((*s++=*t++)!='\0')
         ;
}
就是跟我的一样了呗
zcronline 2013-06-01
  • 打赏
  • 举报
回复
没什么问题 K&R C中实现的strcpy这样写(The C programming language P105)

void strcpy (char *s ,char *t)
{
    while((*s++=*t++)!='\0')
         ;
}
giant7 2013-06-01
  • 打赏
  • 举报
回复
引用 2 楼 Idle_Cloud 的回复:
没问题,也不需要改进了。 我到是还见过一个写法。 while((*strDest++=*strSrc++)); 也可以。
你这个没有字符串终止判断标识'\0', 是不是语句(*strDest++=*strSrc++)会自动判断?
Carl_CCC 2013-06-01
  • 打赏
  • 举报
回复
没问题,也不需要改进了。 我到是还见过一个写法。 while((*strDest++=*strSrc++)); 也可以。
giant7 2013-06-01
  • 打赏
  • 举报
回复
或者有什么错误的地方

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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