MASM32字符串比较
我编写了以下代码来比较两个字符串,一个是预定义的,另一个作为用户的输入。但每次节目都显示他们不平等。请帮助我。我正在使用 MASM32 汇编器。
.data
msg1 db '***Welcome to My Program***',13,10,0
msg2 db 'Please Enter a Product: ',0
msg3 db 'You Entered Shoes: ',0
p1 db 'Shoes',0
.data?
product db 100 dup(?)
.code
start:
invoke StdOut,ADDR msg1
invoke StdOut,ADDR msg2
invoke StdIn,ADDR product,100 ; receive text input
lea esi, p1 ; Load the buffer start address
lea edi, product ; Load the save buffer start address
mov ecx, 10 ; Load the operation count
repe cmpsb ; Compare the byte into the buffer
jne Terminate
invoke StdOut,ADDR msg3
Terminate:
invoke ExitProcess,0
END start
I have written following code to compare two strings, one is predefined and other is taken as input from user. But everytime the program shows them as unequal. please assist me. I am using MASM32 assembler.
.data
msg1 db '***Welcome to My Program***',13,10,0
msg2 db 'Please Enter a Product: ',0
msg3 db 'You Entered Shoes: ',0
p1 db 'Shoes',0
.data?
product db 100 dup(?)
.code
start:
invoke StdOut,ADDR msg1
invoke StdOut,ADDR msg2
invoke StdIn,ADDR product,100 ; receive text input
lea esi, p1 ; Load the buffer start address
lea edi, product ; Load the save buffer start address
mov ecx, 10 ; Load the operation count
repe cmpsb ; Compare the byte into the buffer
jne Terminate
invoke StdOut,ADDR msg3
Terminate:
invoke ExitProcess,0
END start
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有 MASM32 参考,但从内存中 StdIn 也会通过在控制台中按 Enter 键获取回车+换行,这将反映在您读取的变量中。
MASM32 有一个名为 StripLF 或类似函数的内置函数来处理这个问题。之后比较应该通过。
对于这样的问题,我强烈推荐 OllyDbg,它允许您单步执行代码并查看内存转储和堆栈。
编辑:刚刚在 MASM32 论坛上找到了一个帖子,准确地演示了我所描述的内容(忽略这是一个错误报告这一事实,但对 StdIn 的指定行为进行了评论): http://www.masm32.com/board/index.php?PHPSESSID=b98a1a56c52fdc4c07a2bca3553302e2&topic=51.0
I don't have the MASM32 reference to hand but from memory StdIn will also take the carriage return + line feed from hitting enter in the console and that will be reflected in the variable you read to.
MASM32 has a built in function called StripLF or something like that to deal with this. The comparison should pass after that.
For problems like this I highly recommend OllyDbg which will allow you to step your code and see the memory dump and stack.
Edit : Just found a thread on MASM32 forums demonstrating exactly what I'm describing (ignore the fact that's a bug report but hutch comments on the designated behaviour of StdIn) : http://www.masm32.com/board/index.php?PHPSESSID=b98a1a56c52fdc4c07a2bca3553302e2&topic=51.0