如何用汇编实现strcpy+strlen的功能(拷贝一个字符串并返回拷贝的长度)?
需要实现一个函数拷贝字符串并且返回拷贝的长度,c语言代码如下:
size_t copy_string2(char* destination, const char* source)
{
if (!source || !destination)
return 0;
size_t length = strlen(source);
strcpy(destination, source);
return length;
}
估计上面的代码对source做了2次scan,不知道能否用汇编实现一次scan同时完成拷贝和求长度的任务。c/C++的另一种实现能做到,用汇编怎么让他更快一些?请高手赐教:)
size_t copy_string(char* destination, const char* source)
{
if (!source || !destination)
return 0;
char* start_position = destination;
do
{
*destination++ = *source++;
} while(*source);
return destination-start_position;
}
size_t copy_string2(char* destination, const char* source)
{
if (!source || !destination)
return 0;
size_t length = strlen(source);
strcpy(destination, source);
return length;
}
估计上面的代码对source做了2次scan,不知道能否用汇编实现一次scan同时完成拷贝和求长度的任务。c/C++的另一种实现能做到,用汇编怎么让他更快一些?请高手赐教:)
size_t copy_string(char* destination, const char* source)
{
if (!source || !destination)
return 0;
char* start_position = destination;
do
{
*destination++ = *source++;
} while(*source);
return destination-start_position;
}
...全文
相关推荐
c++面试题字符串拷贝函数示例
个也算是企业招工里面比较常见的一道笔试面试题了,非常简单。个人觉得考的主要是对指针使用的熟练程度,还有对字符串类内部原理的掌握程度
strcpy,sprintf,memcpy的区别,Sizeof与Strlen的区别.docx
。。。
C语言实现字符串操作函数的实例
主要介绍了C语言实现字符串操作函数的实例的相关资料,开发程序的时候经常使用到一些字符串函数,例如求字符串长度,拷贝字符串……,需要的朋友可以参考下
C语言实现字符串拼接和拷贝
主要为大家详细介绍了C语言实现字符串拼接和拷贝,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
实现字符长度,串拷贝,连接以及比较string
该文件有strlen,strcpy,strcmp,strcat函数,代码风格规范,非常有利于初学者。



