【问题标题】:Basic user input基本用户输入
【发布时间】:2012-04-05 17:40:29
【问题描述】:

我正在学习一些汇编语言 (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

【问题讨论】:

  • 这应该可以帮助你stackoverflow.com/questions/523185/…
  • @PavanManjunath:可能帮助不大——这是为 DOS 编写的,但这是为 Windows 编写的。从汇编语言级编程的角度来看,两者并没有太多共同点。

标签: winapi assembly x86 masm irvine32


【解决方案1】:

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

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

具体查看ReadConsole 调用,有两点需要牢记。首先,您需要检索控制台的句柄,通常使用GetStdHandle。然后,您需要向ReadConsole 提供它所期望的所有六个左右的参数。`

【讨论】:

  • 谢谢,我让它工作了。我会编辑我的代码,但我的另一个问题是,如果我在写一个字符串,我该如何将这个人输入的变量放入字符串中?我必须输出2个字符串吗? 1 个字符串说“请输入”,然后是变量“5”,然后是另一个字符串“数字”所以当它一起出现时,它是“请输入 5 个数字”
  • @Nogg:是的,差不多就是这样。
猜你喜欢
  • 2019-07-21
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
相关资源
最近更新 更多