MASM32 汇编 - 从控制台读取数字
抱歉,如果这个问题真的很简单,但我尝试了我所知道的一切,但无法弄清楚。
我正在尝试创建一个简单的过程,该过程从控制台获取字符串和计数,并打印计数指定的字符串次数。
一切都很好,但是当我将 Count 移动到 eax 进行循环时,该值会变得混乱,最终会出现无限循环的打印。
我尝试使用 atodw 将计数更改为 DWORD,但没有成功。
这是代码:
PrintString PROTO :DWORD, :DWORD
.data
String db 100 DUP(0)
Count db 10 DUP(0)
.code
start:
;1- get user input
invoke StdIn, addr String, 99
invoke StdIn, addr Count, 10
;2- Remove the CRLF from count
invoke StripLF, addr Count
;3- Convert the count to DWORD
invoke atodw, addr InputCount
mov Counter, eax
;4- Call the Printer function
invoke Printer, addr String, addr Count
Printer PROC StringToPrint:DWORD, count:DWORD
mov eax,count ;;;;;; This is the problem I think
Looppp:
push eax
invoke StdOut, StringToPrint
pop eax
dec eax
jnz Looppp
ret
Printer endp
Sorry if this question is really simple, but I tried all that I know and coudn't figure it out.
I'm trying to make a simple procedure which takes a string and a Count from the console and print the string number of times specified by the Count.
Everything is fine, but when I mov the Count to eax for a loop, the value get's messed up and I end up with an infinite loop of print.
I tried to change the Count to DWORD with atodw, but didn't work.
here's the code :
PrintString PROTO :DWORD, :DWORD
.data
String db 100 DUP(0)
Count db 10 DUP(0)
.code
start:
;1- get user input
invoke StdIn, addr String, 99
invoke StdIn, addr Count, 10
;2- Remove the CRLF from count
invoke StripLF, addr Count
;3- Convert the count to DWORD
invoke atodw, addr InputCount
mov Counter, eax
;4- Call the Printer function
invoke Printer, addr String, addr Count
Printer PROC StringToPrint:DWORD, count:DWORD
mov eax,count ;;;;;; This is the problem I think
Looppp:
push eax
invoke StdOut, StringToPrint
pop eax
dec eax
jnz Looppp
ret
Printer endp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
addr Count
(字符串的地址)作为第二个参数传递给Printer
。但它需要一个整数,因此您需要传递Counter
。由于您使用的语言没有类型检查,因此请采用命名约定,例如 匈牙利表示法标识符可以帮助您发现并避免此类问题。例如,如果此处的变量名为
strCount
和dwCount
,则更明显的是您使用了错误的变量。顺便说一句,
eax
最终必须达到零,这样你的打印循环就不会无限——只是比你预期的要长......You’re passing
addr Count
– the address of the string – as the second argument toPrinter
. But it expects an integer, so you want to passCounter
instead.Since you’re using a language without type checking, adopting a naming convention such as Hungarian notation for your identifiers could help you see and avoid this kind of problem. With the variables here named
strCount
anddwCount
, for example, it would be more obvious that you were using the wrong one.As an aside,
eax
must eventually reach zero so your printing loop won’t be infinite – just rather longer than you intended…