我目前正在开发一个从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链接
发布于 2015-12-14 23:16:34
我假设你的目标是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 ReadConsoleInputAReadConsoleA在本质上类似,但只处理键盘数据。代码可以如下所示:
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位调用约定在寄存器中而不是堆栈上传递许多参数。
https://stackoverflow.com/questions/34276880
复制相似问题