汇编:处理windows nasm中的用户input

我是一个新手,试图做一个简单的问候世界,等待用户按一个键结束。 现在,你好,世界是好的,但我从这个刚刚closures的.exe控制台程序,而我希望它留在屏幕上,直到用户按下一个键。 现在我遇到的问题是,由于某种原因,程序不断循环,search用户input,但是当我强制closures程序(^ C)我可以看到我按下的所有键被写在下一个控制台线,如if它使用了错误的缓冲区(?)

我一直在整个互联网寻找一个修复了几天,最后我寻求帮助,因为这是让我疯狂的^^我发现的一切都是基于int系统或linux下,而我必须处理窗户api …

非常感谢你,任何帮助或暗示,欢迎!

代码:

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 resd 2 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 consoleInHandle push dword[buffer2] push 1 push NULL call ReadConsoleInputA cmp eax,1 jge exit jmp read exit: push NULL call ExitProcess 

关于Windowsfunction的莫阿尔信息可以在这里find:

  • ReadConsoleInput
  • WriteConsole

push consoleInHandle推地址,而不是句柄。 你想push dword [consoleInHandle] 。 相反,对于想要传递地址的缓冲区,则需要push buffer2那里缓冲push buffer2 。 此外,这个缓冲区应该是一个INPUT_RECORD结构的大小,我相信这是32个字节。

更新 :弗兰克评论说,论证顺序也是错误的。 这个代码适用于我(请注意,由于我的环境设置了,我必须添加@xx stdcall装饰 – 显然你不需要这些):

 STD_OUTPUT_HANDLE equ -11 STD_INPUT_HANDLE equ -10 NULL equ 0 global start extern ExitProcess@4, GetStdHandle@4, WriteConsoleA@20, ReadConsoleInputA@16 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@4 push NULL push buffer push msg.len push msg push eax call WriteConsoleA@20 read: push STD_INPUT_HANDLE call GetStdHandle@4 mov [consoleInHandle],eax push NULL push 1 push buffer2 push dword [consoleInHandle] call ReadConsoleInputA@16 exit: push NULL call ExitProcess@4