小言_互联网的博客

x86汇编实现strcpy

396人阅读  评论(0)
.386
.model flat,stdcall
option casemap:none

include msvcrt.inc
includelib msvcrt.lib;system("pause")


.data
szPause db "pause",0
szFormat db "%s",0
str1 db "hello world",0
str2 db 20 dup(0),0

.code

	
main:

	;求字符串长度
	;al与edi中数据进行比较
	lea edi,str1
	lea esi,str1
	mov al,0
	repne scasb;循环从edi中比较al的值,直到相等退出循环
	sub edi,esi;终止位置-起始位置=长度
	mov ecx,edi;保存长度
	
	
	;movsb 字符拷贝
	lea edi,str2
	lea esi,str1
	cld ;清空方向
	rep movsb
	
	push offset str2
	push offset szFormat
	call crt_printf
	add esp,8
	
	push offset szPause
	call crt_system
	add esp,4
	ret
end main
end

把str1的“hello world”拷贝到str2里

方法二:

.386
.model flat,stdcall
option casemap:none

include msvcrt.inc
includelib msvcrt.lib;system("pause")


.data
szPause db "pause",0
szFormat db "%s",0
str1 db "hello world",0
str2 db 20 dup(0),0

.code
mstrcpy proc
	push ebp
	mov ebp ,esp;打开栈帧
	
	
	xor eax,eax
	mov edi, [ebp+0ch] 
	mov ecx,-1
	repne scasb
	not ecx;ecx保存数组长度
	
	mov esi,dword ptr[ebp+0Ch]
	mov edi,dword ptr[ebp+08h]
	rep movsb
	
	mov esp,ebp
	pop ebp
	
	ret 8

mstrcpy endp
	
main:
	push offset str1
	push offset str2
	call mstrcpy
	
	push offset str2
	push offset szFormat
	call crt_printf
	add esp,8
	
	push offset szPause
	call crt_system
	add esp,4

	ret
end main
end

 


转载:https://blog.csdn.net/plokday/article/details/101389106
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场