MASM32 汇编 - 从控制台读取数字

发布于 2024-12-20 16:08:58 字数 995 浏览 4 评论 0原文

抱歉,如果这个问题真的很简单,但我尝试了我所知道的一切,但无法弄清楚。

我正在尝试创建一个简单的过程,该过程从控制台获取字符串和计数,并打印计数指定的字符串次数。

一切都很好,但是当我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-12-27 16:08:58

您将 addr Count(字符串的地址)作为第二个参数传递给 Printer。但它需要一个整数,因此您需要传递 Counter

由于您使用的语言没有类型检查,因此请采用命名约定,例如 匈牙利表示法标识符可以帮助您发现并避免此类问题。例如,如果此处的变量名为 strCountdwCount,则更明显的是您使用了错误的变量。

顺便说一句,eax最终必须达到零,这样你的打印循环就不会无限——只是比你预期的要长......

You’re passing addr Count – the address of the string – as the second argument to Printer. But it expects an integer, so you want to pass Counter 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 and dwCount, 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…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文