flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
revolution 27 Mar 2025, 10:24
The simplest modification would be to call Sleep before exiting.
Code: ;... invoke Sleep,6000 ; wait 6 seconds invoke ExitProcess,0 ;... |
|||
![]() |
|
macomics 27 Mar 2025, 11:39
or just read the console input (wait for Enter).
Code: ; 设置生成 PE(Portable Executable)格式的控制台程序 format PE console ; 包含 FASM 的 Win32 宏和常量定义,简化 API 调用 include 'win32ax.inc' ; 程序入口点声明,对应下方的 start 标签 entry start ; 数据段:存储初始化的全局变量 section '.data' data readable writeable ; 定义字符串,0Dh 是回车符,0Ah 是换行符 hello db 'Hello, World!', 0Dh, 0Ah ; 计算字符串长度:当前地址 $ 减去 hello 的起始地址 hello_len = $ - hello ; 代码段:存放程序指令 section '.text' code readable executable start: ; 调用 Win32 API 获取标准输出句柄 ; STD_OUTPUT_HANDLE = -11,表示标准输出设备 invoke GetStdHandle, STD_OUTPUT_HANDLE ; 将返回的句柄保存到 hOutput 变量中 mov [hOutput], eax ; 调用 Win32 API 向控制台写入字符串 ; 参数1:标准输出句柄 [hOutput] ; 参数2:字符串地址 hello ; 参数3:字符串长度 hello_len ; 参数4:接收实际写入字节数的变量地址 addr bytesWritten ; 参数5:保留参数,设为 0 invoke WriteConsoleA, [hOutput], hello, hello_len, addr bytesWritten, 0 invoke GetStdHandle, STD_INPUT_HANDLE lea edx, [esp - 4] lea ecx, [esp - 8] invoke ReadConsoleA, eax, edx, 4, ecx, 0, 0, 0 pop ecx ; number of bytes readed pop edx ; readed bytes ; 调用 Win32 API 退出进程,返回码为 0(表示成功) invoke ExitProcess, 0 ; 未初始化数据段:存放程序运行时变量 section '.bss' readable writeable hOutput dd ? ; 存储标准输出句柄 bytesWritten dd ? ; 存储 WriteConsoleA 实际写入的字节数 ; 导入段:声明程序依赖的外部 DLL 和函数 section '.idata' import data readable library kernel32, 'kernel32.dll' ; 引入 kernel32.dll import kernel32,\ GetStdHandle, 'GetStdHandle',\ ; 获取标准设备句柄的函数 ReadConsoleA, 'ReadConsoleA',\ WriteConsoleA, 'WriteConsoleA',\; 向控制台写入字符串的函数 ExitProcess, 'ExitProcess' ; 结束进程的函数 |
|||
![]() |
|
Mat-Quasar 27 Mar 2025, 13:28
Or you can open Command Prompt / Windows PowerShell first, then type the program name and run from there.
|
|||
![]() |
|
MR.Z 28 Mar 2025, 07:17
revolution wrote: The simplest modification would be to call Sleep before exiting. Add "invoke sleep,6000" before "invoke ExitProcess,0" and the compiler will prompt an error on "invoke sleep,6000". Why? |
|||
![]() |
|
MR.Z 28 Mar 2025, 07:21
Mat-Quasar wrote: Or you can open Command Prompt / Windows PowerShell first, then type the program name and run from there. Owwww! This is amazing, but I still want to show that the console will not quit after the mouse double clicks the program, and wait for the keyboard to press any key or the mouse to click the close button to close. |
|||
![]() |
|
macomics 28 Mar 2025, 07:22
MR.Z wrote: Add "invoke sleep,6000" before "invoke ExitProcess,0" and the compiler will prompt an error on "invoke sleep,6000". Why? In my example, I added the ReadConsoleA function there so that a similar error would not occur. |
|||
![]() |
|
revolution 28 Mar 2025, 07:48
MR.Z wrote: Add "invoke sleep,6000" before "invoke ExitProcess,0" and the compiler will prompt an error on "invoke sleep,6000". Why? |
|||
![]() |
|
Core i7 28 Mar 2025, 08:11
revolution wrote: It needs to be spelled correctly, with a capital S. Sleep, not sleep. Comrade @sleepsleep will come now and tell me how to do it right ![]() MR.Z, tomorrow you will need to output to the console not only text, but also Hex\Dec values. so look at importing functions from msvcrt.dll, such as printf(), scanf(), _getch() and others. Code: format pe console include 'win32ax.inc' entry start ;//--------------------- .data value dd 12345678h buff db 0 ;//--------------------- .code start: invoke SetConsoleTitle,<'**** Test msvcrt.dll ****',0> cinvoke printf,<10,' String: ',0> cinvoke scanf, <'%s',0>,buff cinvoke printf,<10,' Value: Hex = 0x%08x. Dec = %u.',0>,[value],[value] cinvoke getch cinvoke exit,0 ;//--------------------- section '.idata' import data readable library kernel32,'kernel32.dll', msvcrt,'msvcrt.dll' import msvcrt, printf,'printf', scanf,'scanf', getch,'_getch',exit,'exit' include 'api\kernel32.inc' |
|||
![]() |
|
Mat-Quasar 28 Mar 2025, 09:55
MR.Z wrote: ... I still want to show that the console will not quit after the mouse double clicks the program, and wait for the keyboard to press any key or the mouse to click the close button to close. I have written an example before: "A Win32 console keypress example" https://board.flatassembler.net/topic.php?t=23363 Or, macomics' "Press Enter to quit..." using ReadConsoleA is enough for most use cases. Core i7's Visual C++ Runtime library function is very short and less complicated. Happy coding! |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.