flat assembler
Message board for the users of flat assembler.

Index > Windows > Questions about running the executable console after compila

Author
Thread Post new topic Reply to topic
MR.Z



Joined: 12 Mar 2025
Posts: 18
MR.Z 27 Mar 2025, 09:59
Ask a question, with fasm compiled into.exe executable file and run, why the console just flash? How do I make the console stay and wait for it to close or click any key to close it again?
The code is as follows:
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
 
    ; 调用 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',\   ; 获取标准设备句柄的函数
    WriteConsoleA, 'WriteConsoleA',\; 向控制台写入字符串的函数
    ExitProcess, 'ExitProcess'      ; 结束进程的函数
    
Post 27 Mar 2025, 09:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20580
Location: In your JS exploiting you and your system
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
;...    
If instead you need to wait for for a mouse click or key press then you need some more complicated code that opens handles to the two input sources and then waits on those handles for an event.
Post 27 Mar 2025, 10:24
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1104
Location: Russia
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'      ; 结束进程的函数
        
Post 27 Mar 2025, 11:39
View user's profile Send private message Reply with quote
Mat-Quasar



Joined: 02 Mar 2025
Posts: 81
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.
Post 27 Mar 2025, 13:28
View user's profile Send private message Reply with quote
MR.Z



Joined: 12 Mar 2025
Posts: 18
MR.Z 28 Mar 2025, 07:17
revolution wrote:
The simplest modification would be to call Sleep before exiting.
Code:
;...
 invoke Sleep,6000 ; wait 6 seconds
 invoke ExitProcess,0
;...    
If instead you need to wait for for a mouse click or key press then you need some more complicated code that opens handles to the two input sources and then waits on those handles for an event.


Add "invoke sleep,6000" before "invoke ExitProcess,0" and the compiler will prompt an error on "invoke sleep,6000". Why?
Post 28 Mar 2025, 07:17
View user's profile Send private message Reply with quote
MR.Z



Joined: 12 Mar 2025
Posts: 18
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.
Post 28 Mar 2025, 07:21
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1104
Location: Russia
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?
You also need to add the import line for the Sleep function in the import section below.

In my example, I added the ReadConsoleA function there so that a similar error would not occur.
Post 28 Mar 2025, 07:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20580
Location: In your JS exploiting you and your system
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?
It needs to be spelled correctly, with a capital S. Sleep, not sleep.
Post 28 Mar 2025, 07:48
View user's profile Send private message Visit poster's website Reply with quote
Core i7



Joined: 14 Nov 2024
Posts: 47
Location: Socket on motherboard
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 Smile

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'
    
Post 28 Mar 2025, 08:11
View user's profile Send private message Reply with quote
Mat-Quasar



Joined: 02 Mar 2025
Posts: 81
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!
Post 28 Mar 2025, 09:55
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.