【发布时间】:2017-06-13 16:18:40
【问题描述】:
我正在搜索连接两个字符串(由 C 传递),将结果放在第三个字符串中。我做到了,但我知道我想在字符串之间放置一个空格,但是......不可能......! 这是C的部分
#include <stdio.h>
#include <string.h>
void concatena(char *stringa, char *stringa2, char *dest);
int main(void)
{
char stringa[17] = { "stringa numero 1" };
char stringa2[17] = { "stringa numero 2" };
char dest[34] = { "" };
concatena(stringa, stringa2, dest);
printf("%s", dest);
getchar();
}
调用masm32的部分:
.586
.model flat
.code
_concatena proc
;pre
push ebp
mov ebp,esp
push ebx
push edi
push esi
;clean
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
xor esi, esi
xor edi, edi
mov eax, dword ptr[ebp+8] ;source=stringa
mov ebx, dword ptr[ebp+12] ;target=stringa2
mov ecx, dword ptr[ebp+16] ;buffer=dest
inizio:
mov dl,byte ptr[eax+esi*1]
cmp dl,0
je space ;first string finished
mov byte ptr[ecx+esi*1], dl
inc esi
jmp inizio
space:
inc esi
mov byte ptr[ecx+esi*1],32
;if i put a 'inc esi' here the result is the same
jmp fine1
fine1:
mov dl, byte ptr[ebx+edi*1]
cmp dl, 0
je fine2 ;second string finished
mov byte ptr[ecx+esi*1], dl
inc edi
inc esi
jmp fine1
fine2:
;post
pop esi
pop edi
pop ebx
pop ebp
ret
;chiusura della procedura
_concatena endp
end
当我在输出中运行它时,我看到:
您如何看到 concatena() 仅将第一个字符串放入目标数组... 非常感谢您的每一个回答!
【问题讨论】:
-
如果你想要一个空间,添加它。如果你不添加它,C 就不会。这一切都在你的控制之下。权力带来责任。
-
从用高级语言编写函数开始,然后将它翻译成汇编。错误是你的代码在没有混淆的情况下会很明显。
-
更好:
void concatena(const char *stringa, const char *stringa2, char *dest);和printf("%s\n", dest);并且可能在main()的右大括号之前添加return 0; -
char stringa[17] = { "stringa numero 1" };是无效的初始化程序。取下大括号。
标签: assembly x86 masm string-concatenation