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

masterz 2007-09-15 04:32:02
需要实现一个函数拷贝字符串并且返回拷贝的长度,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;
}
...全文
688 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahjoe 2007-09-19
  • 打赏
  • 举报
回复
先调用 StrCpy 再调用 StrLen,速度不能满足需要?

先 StrLen, 再用 CopyMemory, 可能快一点点儿
masterz 2007-09-17
  • 打赏
  • 举报
回复
抛砖引玉,下面的代码比C语言还慢。
size_t copy_string3(char* destination, const char* source)
{
__asm{
xor eax, eax
mov esi, source
test esi, esi ; check if source is valid
jz proc_end
mov edi, destination
test edi, edi ; check if destination is valid
jz proc_end
cld
copy_lable:
lodsb
stosb
test al, al
jnz copy_lable
mov ecx, [destination]
sub edi, ecx
mov eax, edi
proc_end:
}
}
masterz 2007-09-17
  • 打赏
  • 举报
回复
下面这个版本速度还可以
size_t asm_copy_string(char* destination, const char* source)
{
__asm
{
//int 3
xor eax, eax
mov edi, destination
test edi, edi
je quit
mov ecx, source
test ecx, ecx
je quit
; check if source is aligned ON 32 bits
test ecx, 3
je 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,destination ; return in eax pointer to dest string
sub edi, eax
mov eax, edi
add eax, 3
jmp quit;ret
byte_2:
mov [edi],dx
mov eax,destination ; return in eax pointer to dest string
mov byte ptr [edi+2],0
sub edi, eax
mov eax, edi
add eax, 2
jmp quit;ret
byte_1:
mov [edi],dx
mov eax,destination ; return in eax pointer to dest string
sub edi, eax
mov eax, edi
add eax, 1
jmp quit;ret
byte_0:
mov [edi],dl
mov eax,destination ; return in eax pointer to dest string
sub edi, eax
mov eax, edi
jmp quit
quit:
};

}
masterz 2007-09-17
  • 打赏
  • 举报
回复
To jvphxys: 我也是这么想的,就是不知道怎么写:)库函数似乎考虑到字节对齐的问题而进行优化了。
jvphxys 2007-09-17
  • 打赏
  • 举报
回复
代码不知道怎么写啊..
不过有点想法,错了别见怪..
直接一个字符一个字符地复制,设置一个计数器,进行循环复制,遇到结束标志结束.
复制完成,计数器的值应该就是长度了.
这样感觉对于整个字符串来说,只进行了一次扫描哈.
sunlin7 2007-09-16
  • 打赏
  • 举报
回复
呵呵,不知道什么原因,直接打开这个贴子后,只能看到楼主的问题,而看不到跟贴.
sunlin7 2007-09-16
  • 打赏
  • 举报
回复
up
masterz 2007-09-16
  • 打赏
  • 举报
回复
To hkbyest:谢谢你的热心,不过sizeof是不能得到字符串长度的,它只能得到编译时静态分配的数组大小。
hkbyest 2007-09-15
  • 打赏
  • 举报
回复
当然,你用跳转逐字节比较结尾的0,里面设置一个值递增来记录字符串长度也是可以的。
hkbyest 2007-09-15
  • 打赏
  • 举报
回复
我上面说的只是一种方法,

以前做过类似的东西,
伪码如下:
mov ecx,sizeof sourcestr
mov length,ecx
dec length
push ds
pop es
mov esi,sourcestr
mov edi,targetstr
rep movsb
masterz 2007-09-15
  • 打赏
  • 举报
回复
To hkbyest:你自己试一试:)
masterz 2007-09-15
  • 打赏
  • 举报
回复
To lisunlin0:2遍扫描速度慢,我测试过了,movsb这个指令我也知道,只是不善于写汇编语言。你能帮我试试用汇编实现一遍扫描吗^_^
hkbyest 2007-09-15
  • 打赏
  • 举报
回复
字符串长度吗?
直接sizeof不就行了嘛,注意处理一下结尾的字符。
sunlin7 2007-09-15
  • 打赏
  • 举报
回复
不好意思:
size_t copy_string(char* destination, const char* source)
{
int i;
if (source && destination)
return 0;
for(i = 0; destination[i] = source[i]; i++);
return i;
}
sunlin7 2007-09-15
  • 打赏
  • 举报
回复
汇编中可以用repnz movsb实现字符串拷贝,rep scasb实现字符串求长.因此也得扫描两遍.
直接用
size_t copy_string(char* destination, const char* source)
{
int i;
if (source | destination)
return 0;
for(i = 0; destination[i] = source[i]; i++);
return i;
}
也可.

21,496

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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