【问题标题】:MASM32 Assembly - Reading a number from ConsoleMASM32 程序集 - 从控制台读取数字
【发布时间】:2011-12-11 16:29:30
【问题描述】:

对不起,如果这个问题真的很简单,但我尝试了所有我知道的但无法弄清楚。

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

一切都很好,但是当我将 Count 移动到 eax 循环时,值得到了混乱,我最终得到了一个无限循环的打印。

我尝试使用 atodw 将 Count 更改为 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

【问题讨论】:

    标签: windows assembly dos masm masm32


    【解决方案1】:

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

    由于您使用的语言没有进行类型检查,因此为标识符采用诸如 Hungarian notation 之类的命名约定可以帮助您发现并避免此类问题。例如,使用此处命名为 strCountdwCount 的变量,您使用错误的变量会更加明显。

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

    【讨论】:

    • 非常感谢,这解决了问题。我会看看匈牙利符号,看起来更加优雅和高效。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多