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 在 .data 中定义为一个字节的位置?

编辑:

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
4

1 回答 1

3

不,它(至少通常)没有那么简单。

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

具体看ReadConsole电话,有两件事要记住。首先,您需要检索控制台的句柄,通常使用GetStdHandle. 然后,您需要提供ReadConsole它所期望的所有六个左右的参数。

于 2012-04-05T17:56:07.760 回答