[英]How to concatenate two strings?
The program is supposed to take two inputted strings, concatenate them, then print.该程序应该接受两个输入的字符串,将它们连接起来,然后打印。 This is the code I have right now and I am wondering how to go about this.这是我现在拥有的代码,我想知道如何 go 关于这个。 I'm still new, so bear with me.我还是新手,所以请多多包涵。 Thanks in advance.提前致谢。
.586
.MODEL FLAT
.STACK 4096
INCLUDE io.h
.DATA
Inputstr BYTE 100 DUP (?)
Inputstr2 BYTE 100 DUP (?)
Outputstr BYTE 100 DUP (?)
prompt BYTE "Enter a string", 0
displayLbl BYTE "Concatinated string", 0
.CODE
_MainProc PROC
input prompt, Inputstr, 100
lea esi, Inputstr
lea edi, Outputstr
push esi
push edi
cld
input prompt, Inputstr2, 100
lea esi, Inputstr2
lea edi, Outputstr
push esi
push edi
cld
whileNoNul:
cmp BYTE PTR [esi], 0
je endWhileNoNul
movsb
loop whileNoNul
endWhileNoNul:
mov BYTE PTR [edi], 0
pop esi
pop edi
output displayLbl, Outputstr
mov eax, 0
ret
_MainProc ENDP
END
My code is only printing my second input which is Inputstr2 .我的代码只打印我的第二个输入,即Inputstr2 。 It is supposed to print out both Inputstr and Inputstr2 together.它应该同时打印出Inputstr和Inputstr2 。
I'm no assembly expert, but it looks like you're pushing data on register esi and edi twice, first for InputStr and then for InputStr2 - so won't the pop esi
and pop edi
have only the InputStr2 in it when the code executes the While?我不是汇编专家,但看起来你在寄存器 esi 和 edi 上推送数据两次,首先是 InputStr ,然后是 InputStr2 - 所以当代码时pop esi
和pop edi
不会只有 InputStr2 吗执行 While?
If you are going to allow the user to input 2 strings of up to 99 characters each, then your program should define an Outputstr that is bigger than 100.如果您要允许用户输入 2 个字符串,每个字符串最多 99 个字符,那么您的程序应该定义一个大于 100 的Outputstr 。
For a concatenation between string1 and string2 , you would normally place the string1 before the string2 .对于string1和string2之间的串联,您通常会将string1放在string2之前。 Your code currently begins by copying string2 (and forgets to deal with string1 ).您的代码当前从复制string2开始(并忘记处理string1 )。
The stack needs balancing: what you push
also needs to come off, And just as important, you need to pop
the registers in reverse order.堆栈需要平衡:你push
的东西也需要脱落,同样重要的是,你需要以相反的顺序pop
寄存器。
The loop
instruction in loop whileNoNul
depends on the ECX register. loop whileNoNul
中的loop
指令取决于 ECX 寄存器。 Your code did not prepare that register beforehand, so results will not be correct.您的代码没有事先准备好该寄存器,因此结果将不正确。
There's nothing that needs pushing or popping here.这里没有什么需要推动或弹出的。 First copy the Inputstr1 without its terminating zero, then copy the Inputstr2 with its terminating zero.首先复制不带终止零的Inputstr1 ,然后复制带终止零的Inputstr2 。 And of course, in between you don't tamper with the EDI register!当然,在此期间您不能篡改 EDI 登记册!
Inputstr1 BYTE 100 DUP (?)
Inputstr2 BYTE 100 DUP (?)
Outputstr BYTE 200 DUP (?)
...
input prompt, Inputstr1, 100
input prompt, Inputstr2, 100
mov esi, OFFSET Inputstr1
mov edi, OFFSET Outputstr
cld
jmp while
contWhile:
stosb
while:
lodsb
cmp al, 0
jne contWhile
mov esi, OFFSET Inputstr2
until:
lodsb
stosb
cmp al, 0
jne until
output displayLbl, Outputstr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.