Nasm 读取控制台输入
Nasm ReadConsoleInput
我目前正在开发一个简单的 I/O 控制台应用程序,它是从 nasm 编译而来的,但即使它编译和链接,当我 运行 它时它也会崩溃。这是代码:
STD_OUTPUT_HANDLE equ -11
STD_INPUT_HANDLE equ -10
NULL equ 0
global start
extern ExitProcess, GetStdHandle, WriteConsoleA, ReadConsoleInputA
section .data
msg db "Hello World!", 13, 10, 0
msg.len equ $ - msg
consoleInHandle dd 1
section .bss
buffer resd 2
buffer2 resb 32
section .text
start:
push STD_OUTPUT_HANDLE
call GetStdHandle
push NULL
push buffer
push msg.len
push msg
push eax
call WriteConsoleA
read:
push STD_INPUT_HANDLE
call GetStdHandle
mov [consoleInHandle],eax
push NULL
push 1
push buffer2
push dword [consoleInHandle]
call ReadConsoleInputA
exit:
push NULL
call ExitProcess
有什么线索吗?顺便说一句,我 运行 正在使用 64 位 windows 10 机器,我使用 Nasm 进行编译,使用 GoLink 进行链接
我假设您的目标是 32 位 Windows 可执行文件。您调用 ReadConsoleInputA
,但如果您只对从键盘输入的字符感兴趣,则调用 ReadConsoleA
可能更简单。您问题的标题是 ReadConsole Input
(两者之间的 space 让我感到困惑)。您的代码是:
push STD_INPUT_HANDLE
call GetStdHandle
mov [consoleInHandle],eax
push NULL
push 1
push buffer2
push dword [consoleInHandle]
call ReadConsoleInputA
ReadConsoleA 本质上类似,但只处理键盘数据。代码可能如下所示:
push STD_INPUT_HANDLE
call GetStdHandle
mov [consoleInHandle],eax
push NULL
push buffer ; Pointer to a DWORD for number of characters read to be returned
push 1
push buffer2
push dword [consoleInHandle]
call ReadConsoleA
尽管 ReadConsoleInputA
从控制台读取字符数据,它会处理您必须正确处理(或忽略)的大量其他事件(包括鼠标、菜单、焦点和键盘)。
我假设它是用生成 32 位可执行文件的命令构建的,如下所示:
nasm -f win32 test.asm -o test.obj
GoLink.exe /console test.obj kernel32.dll
如果您想要以 64 位可执行文件为目标,那么您的所有代码都必须更改,因为 64 位调用约定在寄存器中而不是在堆栈中传递许多参数。
我目前正在开发一个简单的 I/O 控制台应用程序,它是从 nasm 编译而来的,但即使它编译和链接,当我 运行 它时它也会崩溃。这是代码:
STD_OUTPUT_HANDLE equ -11
STD_INPUT_HANDLE equ -10
NULL equ 0
global start
extern ExitProcess, GetStdHandle, WriteConsoleA, ReadConsoleInputA
section .data
msg db "Hello World!", 13, 10, 0
msg.len equ $ - msg
consoleInHandle dd 1
section .bss
buffer resd 2
buffer2 resb 32
section .text
start:
push STD_OUTPUT_HANDLE
call GetStdHandle
push NULL
push buffer
push msg.len
push msg
push eax
call WriteConsoleA
read:
push STD_INPUT_HANDLE
call GetStdHandle
mov [consoleInHandle],eax
push NULL
push 1
push buffer2
push dword [consoleInHandle]
call ReadConsoleInputA
exit:
push NULL
call ExitProcess
有什么线索吗?顺便说一句,我 运行 正在使用 64 位 windows 10 机器,我使用 Nasm 进行编译,使用 GoLink 进行链接
我假设您的目标是 32 位 Windows 可执行文件。您调用 ReadConsoleInputA
,但如果您只对从键盘输入的字符感兴趣,则调用 ReadConsoleA
可能更简单。您问题的标题是 ReadConsole Input
(两者之间的 space 让我感到困惑)。您的代码是:
push STD_INPUT_HANDLE
call GetStdHandle
mov [consoleInHandle],eax
push NULL
push 1
push buffer2
push dword [consoleInHandle]
call ReadConsoleInputA
ReadConsoleA 本质上类似,但只处理键盘数据。代码可能如下所示:
push STD_INPUT_HANDLE
call GetStdHandle
mov [consoleInHandle],eax
push NULL
push buffer ; Pointer to a DWORD for number of characters read to be returned
push 1
push buffer2
push dword [consoleInHandle]
call ReadConsoleA
尽管 ReadConsoleInputA
从控制台读取字符数据,它会处理您必须正确处理(或忽略)的大量其他事件(包括鼠标、菜单、焦点和键盘)。
我假设它是用生成 32 位可执行文件的命令构建的,如下所示:
nasm -f win32 test.asm -o test.obj
GoLink.exe /console test.obj kernel32.dll
如果您想要以 64 位可执行文件为目标,那么您的所有代码都必须更改,因为 64 位调用约定在寄存器中而不是在堆栈中传递许多参数。