简体   繁体   English

FASM 汇编程序在退出前等待

[英]FASM assembly program waits before exiting

This is the code that I use with FASM:这是我与 FASM 一起使用的代码:

format PE console
entry main

include '..\MACRO\import32.inc'

section '.data' data readable writeable
msg db "привіт!",0dh,0ah,0 ;hi
lcl_set db ?

section '.code' code readable executable
main:
;fail without set locale
push    msg
call    [printf]
 pop    ecx

;succeed with set locale
push msg
call _liapnuty
pop ecx

push 0
call [ExitProcess]

_liapnuty:
push    ebp
 mov    ebp, esp
;sub    esp, 0
 mov    ebx,[ebp+8]  ; 1st arg addr

 mov    al, [lcl_set]
  or    al, al
 jnz    _liapnuty_rest
call    __set_locale

_liapnuty_rest:
push    ebx
call    [printf]
 pop    ebx

 mov    esp, ebp
 pop    ebp
 ret    0

__set_locale:
 mov    al, [lcl_set]
  or    al, al
 jnz    __set_locale_rest

push    1251
call    SetConsoleCP
call    SetConsoleOutputCP
 pop    ecx
 mov    [lcl_set], 1

;push   lcl
;call   [system]
; pop   ecx
; mov   [lcl_set], 1

;push   cls
;call   [printf]
; pop   ecx

__set_locale_rest:
 ret    0


section '.idata' import data readable
library kernel,'kernel32.dll',\
        msvcrt,'msvcrt.dll'

import  kernel,\
        SetConsoleCP,'SetConsoleCP',\
        SetConsoleOutputCP,'SetConsoleOutputCP',\
        ExitProcess,'ExitProcess'

import  msvcrt,\
        printf,'printf'

It works almost perfectly, except that before exiting it waits for like a second for some reason.它几乎完美地工作,除了在退出之前它出于某种原因等待一秒钟。 It outputs data almost instantly, yet it fails to shut down quickly.它几乎立即输出数据,但无法快速关闭。 If the reason is using these libraries or not clearing the stack after calling ExitProcess (which obviously can't be done), then let me know and I will mostly gladly accept this answer, but I want to be 100% sure I'm doing everything correctly.如果原因是使用这些库或在调用 ExitProcess 后没有清除堆栈(这显然无法完成),那么请告诉我,我很乐意接受这个答案,但我想 100% 确定我在做什么一切正确。

The reason for all of it was because kernel32 functions pop their parameters themselves on return.所有这一切的原因是因为 kernel32 函数在返回时自己弹出它们的参数。 If I remove unnecessary pops it starts working fast again.如果我删除不必要的弹出窗口,它会再次开始快速工作。 Of course, the program still runs with damaged stack but it does a lot of damage control at the end.当然,程序仍然在损坏的堆栈下运行,但它在最后做了很多损坏控制。 That's why it was slow, but still worked.这就是为什么它很慢,但仍然有效。 For everyone facing this issue, make sure to be careful with the calling convention.对于面临此问题的每个人,请务必注意调用约定。

To debug the application and find the error I used OLLYDBG .为了调试应用程序并找到错误,我使用了OLLYDBG It's free and it works.它是免费的,而且有效。 It helps you debug EXEs and DLLs, allowing to step one command at a time.它可以帮助您调试 EXE 和 DLL,允许一次执行一个命令。 Also it shows the memory, the stack and all of the registers and flags.它还显示了内存、堆栈以及所有寄存器和标志。

Using the stack I was able to find out that it gets corrupted.使用堆栈我能够发现它已损坏。 OLLYDBG 屏幕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM