0
我试图写用于复制零终止的字符串终止0复印零包括x86Assembly(MASM)终止0结尾的字符串
后我请stringCopy(STR1,STR2)另一个字符串包括函数;输出应该好,bye0ld0但再见 :(我错过什么? 如何打印正确的结果?
;------------------ in my .cpp file
extern "C" void __stdcall stringCopy(char[], char[]);
int main()
{
char str1[] = { 'h','e','l','l','o',' ','w','o','r','l','d',0 };
cout << str1 << endl;
char str2[] = { 'G','o','o','d','-','b','y','e',0};
cout << str2 << endl;
stringCopy(str1, str2);
cout << str1 << endl; ;should be Good-bye0ld0
;but is Good-bye
}
;------------------ in my .asm file
; Copy zero terminated string2 (including terminating 0)
stringCopy PROC uses ecx eax esi edi, ;save registers used
string1:DWORD, ;address of string1
string2:DWORD ;address of string2
cld ;forward direction - clear direction flag
push string2 ;address of str2 arg to StrlenAsm
call getStringLength ;get length of str2
;called function responsible for stack cleanup
mov ecx,eax ;length of string in ecx for rep
mov edi,string1 ;edi gets destination address for copy
mov esi,string2 ;esi gets source address for copy
rep movsb ;copy byte from source to desintation ecx times
mov byte ptr[edi],0 ;null terminate copied string
ret
stringCopy ENDP
getStringLength PROC uses edi, ;save edi
strAdd:DWORD ;address of string to find length of
mov edi, strAdd ;edi = address of string to get length of
xor eax,eax ;eax to hold length so 0 it out
looptop:
cmp byte ptr [edi],0 ;have we reached the end of the string yet?
je done ;Yes, done
inc edi ;no, increment to next character
inc eax ;and increment length
jmp looptop ;repeat
done:
ret
getStringLength ENDP
为什么不简化成一个循环,以便同时复制和查找终止0?这是实现'strcpy(3)'的“正常”方式。另外,MASM不会让你在'push' /'call'之后修复堆栈吗? 'getStringLength'返回'ret',而不是'ret 4',所以它不会将操作数弹出堆栈。无论如何,**在调试器**中逐步完成,或者查看反汇编输出以查看MASM指令产生的代码。 –