双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

如何用汇编实现strcpy+strlen的功能(拷贝一个字符串并返回拷贝的长度)?

masterz masterz 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
需要实现一个函数拷贝字符串并且返回拷贝的长度,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;
}
...全文
评论 15
收藏 已收藏
分享
返回顶部
masterz
汇编语言 21,500 成员 41,619 内容