Nasm ReadConsoleInput

时间:2015-12-14 21:24:13

标签: winapi assembly crash nasm

我目前正在开发一个从nasm编译的简单I / O控制台应用程序,但即使它编译和链接,它在运行时也会崩溃。这是代码:

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进行链接

1 个答案:

答案 0 :(得分:2)

我假设你的目标是32位Windows可执行文件。您可以拨打ReadConsoleInputA,但如果您只是对从键盘输入的字符感兴趣,可以更简单地拨打ReadConsoleA。你问题的标题是ReadConsole Input(两者之间的空间让我困惑)。你的代码是:

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位调用约定会在寄存器中而不是堆栈中传递许多参数。