2012-04-05 58 views
1

我正在学习一些汇编语言(x86 Irvine.32 windows7),并有一个关于如何从用户输入的问题。我所写的这本书并没有深入讨论它。我想提示用户:基本的用户输入

myfirst BYTE "Welcome! This program calculates the sum of a list of numbers.", 0dh, 0ah, 0dh, 0ah ; greeting 
     BYTE "How many integers will be added? : " 

那么用户会输入X.我怎样读取用户输入的内容并将其放入一个变量?

是化繁为简:

INVOKE ReadConsole, SomeVairable 

凡SomeVairable中。数据定义了一个字节?

编辑:

INCLUDE Irvine32.inc 

BufSize = 80 

.data 
buffer BYTE BufSize DUP(?) 
stdInHandle HANDLE ? 
bytesRead DWORD ? 
myfirst BYTE "Welcome! This program calculates the sum of a list of numbers.", 0dh, 0ah, 0dh, 0ah ; greeting 
     BYTE "How many integers will be added? : " 
mysecond BYTE "Please enter the " 

.code 
main PROC 

    mov edx, OFFSET myfirst       ;move the location of myfirst into edx 
    call WriteString  

    ; Get handle to standard input 
    INVOKE GetStdHandle, STD_INPUT_HANDLE 
    mov stdInHandle,eax 

    ; Wait for user input 
    INVOKE ReadConsole, stdInHandle, ADDR buffer, 
     BufSize, ADDR bytesRead, 0 


    exit 
main ENDP 
END main 
+0

什么平台/操作系统? – kuba 2012-04-05 17:44:05

+0

刚编辑原稿。 – Nogg 2012-04-05 17:47:34

+0

这应该可以帮助你http://stackoverflow.com/questions/523185/a-simple-assembly-input-question – 2012-04-05 17:55:35

回答

3

不,这不是(至少通常)这么简单。

什么用户输入将被解读为一个字符串,不是一个数字。您通常必须读取字符串(通常会多于一个字节),然后将其转换为整数。在进行转换之前,您可能需要验证字符串中的所有字符都是数字,或者您可能希望将转换与验证结合起来。

特别注意ReadConsole呼叫,有两点需要注意。首先,您需要检索控制台的句柄,通常使用GetStdHandle。然后,你需要提供ReadConsole所需的所有参数。[

+0

谢谢,我得到它的工作。生病编辑我的代码,但我有另一个问题是如果即时通讯写入一个字符串如何可以把人进入字符串的变量?我需要输出2个字符串吗? 1字符串说“请输入”,然后变量“5”,然后另一个字符串“数字”因此,当它一起出现时,它是“请输入5个数字” – Nogg 2012-04-05 18:07:28

+0

@Nogg:哟,这就是它。 – 2012-04-05 18:13:18