如何处理 Assembly 中连接两个字符串的错误?
How to handle error on concatenation two string in Assembly?
对于我的作业,我想连接两个字符串。到目前为止,这是我的代码。结果应该是 helloworld ,但它只显示 hello :-(
如何在 destination 的 end 添加 source 字符串?
我错过了什么?
;--------------------In main.CPP
extern "C" void __stdcall StrCatAsm(char[], char[]);
;main
char str1[] = {'h','e','l','l','o',0};
char str2[] = {'w','o','r','l','d',0};
StrCatAsm(str1,str2)
string1 = 'h','e','l','l','o','w','o','r','l','d',0
;------------------In main.asm
Concat PROC uses eax edi ecx esi,
destination:DWORD,
source:DWORD
mov al,0 ;looking for zero terminated
mov ecx,100 ;number of possible loop
mov edi, source
repne scasb ;look for end of source
not ecx ;ecx is 6
mov esi, destination
rep movsb ;loop for copy ESI into EDI
ret
Concat ENDP
这里是字符串连接的伪代码:
t1 = get string length of str1 ; find length not including zero terminate
t2 = get string length of str2 ; find length not including zero terminate
ct = (t1 + t2) + 1 ; total length + 1 for zero terminate
dest = allocate memory for size ct ; get total string length
n = 0
nt = 0
while str1[n] != 0 ; Until you hit str1 zero terminate
dest[nt] = str1[n]
n = n + 1
nt = nt+1
n = 0
while str2[n] != 0 ; Until you hit str1 zero terminate
dest[nt] = str2[n]
n = n + 1
nt = nt + 1
dest[nt] = 0 ; final terminate string
How should I add source string at the end of destination ?
如果这是您所需要的,那么您应该在 destination 中查找终止零(而不是像您的代码那样在源代码中查找!)。
一旦找到终止零,您的 EDI
寄存器将超过其位置,因此您需要备份 1 个位置!
mov al, 0 ;looking for zero terminated
mov ecx, 100 ;any large number will do!
mov edi, DESTINATION
repne scasb ;look for end of DESTINATION
DEC EDI
mov ecx, 6 ;length of SOURCE {'w','o','r','l','d',0};
mov esi, SOURCE
rep movsb ;loop for copy ESI into EDI
对于我的作业,我想连接两个字符串。到目前为止,这是我的代码。结果应该是 helloworld ,但它只显示 hello :-(
如何在 destination 的 end 添加 source 字符串?
我错过了什么?
;--------------------In main.CPP
extern "C" void __stdcall StrCatAsm(char[], char[]);
;main
char str1[] = {'h','e','l','l','o',0};
char str2[] = {'w','o','r','l','d',0};
StrCatAsm(str1,str2)
string1 = 'h','e','l','l','o','w','o','r','l','d',0
;------------------In main.asm
Concat PROC uses eax edi ecx esi,
destination:DWORD,
source:DWORD
mov al,0 ;looking for zero terminated
mov ecx,100 ;number of possible loop
mov edi, source
repne scasb ;look for end of source
not ecx ;ecx is 6
mov esi, destination
rep movsb ;loop for copy ESI into EDI
ret
Concat ENDP
这里是字符串连接的伪代码:
t1 = get string length of str1 ; find length not including zero terminate
t2 = get string length of str2 ; find length not including zero terminate
ct = (t1 + t2) + 1 ; total length + 1 for zero terminate
dest = allocate memory for size ct ; get total string length
n = 0
nt = 0
while str1[n] != 0 ; Until you hit str1 zero terminate
dest[nt] = str1[n]
n = n + 1
nt = nt+1
n = 0
while str2[n] != 0 ; Until you hit str1 zero terminate
dest[nt] = str2[n]
n = n + 1
nt = nt + 1
dest[nt] = 0 ; final terminate string
How should I add source string at the end of destination ?
如果这是您所需要的,那么您应该在 destination 中查找终止零(而不是像您的代码那样在源代码中查找!)。
一旦找到终止零,您的 EDI
寄存器将超过其位置,因此您需要备份 1 个位置!
mov al, 0 ;looking for zero terminated
mov ecx, 100 ;any large number will do!
mov edi, DESTINATION
repne scasb ;look for end of DESTINATION
DEC EDI
mov ecx, 6 ;length of SOURCE {'w','o','r','l','d',0};
mov esi, SOURCE
rep movsb ;loop for copy ESI into EDI