K
K
Konstantin Malyarov2016-05-02 22:42:55
assembler
Konstantin Malyarov, 2016-05-02 22:42:55

What does this piece of code do?

format PE console 4.0
 
include 'win32ax.inc'
ENABLE_PROCESSED_OUTPUT  = 00000001h
 
    invoke  AllocConsole
    invoke  GetStdHandle, STD_INPUT_HANDLE
        push    eax
        push    eax
    invoke  GetStdHandle, STD_OUTPUT_HANDLE
        push    eax
    invoke  SetConsoleMode, eax, ENABLE_PROCESSED_OUTPUT
        pop eax
    invoke  WriteConsole, eax, hello, msgsz, NULL, NULL
    invoke  FlushConsoleInputBuffer
        pop eax
    invoke  ReadConsole, eax, buf, bufsz, cnt, NULL
    invoke  FreeConsole
    invoke  ExitProcess,0
 
cnt dd  ?
buf db  10 dup(?)
bufsz   =   $ - buf
hello   db  'Hello World!',0
msgsz   =   $ - hello
 
data import
 
 library kernel32,'KERNEL32.DLL'
 
 import kernel32,\
    ExitProcess,'ExitProcess',\
    SetConsoleCP,'SetConsoleCP',\
    SetConsoleOutputCP,'SetConsoleOutputCP',\
    GetStdHandle,'GetStdHandle',\
    SetConsoleMode,'SetConsoleMode',\
    ReadConsole,'ReadFile',\
    WriteConsole,'WriteConsoleA',\
    FreeConsole,'FreeConsole',\
    FlushConsoleInputBuffer,'FlushConsoleInputBuffer',\
    AllocConsole,'AllocConsole'
end data

Interested in this area.
invoke  GetStdHandle, STD_INPUT_HANDLE
        push    eax
        push    eax
    invoke  GetStdHandle, STD_OUTPUT_HANDLE
        push    eax
    invoke  SetConsoleMode, eax, ENABLE_PROCESSED_OUTPUT
        pop eax
    invoke  WriteConsole, eax, hello, msgsz, NULL, NULL
    invoke  FlushConsoleInputBuffer
        pop eax
    invoke  ReadConsole, eax, buf, bufsz, cnt, NULL

What are we pushing onto the stack, and what are we pulling from there?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
csar, 2016-05-02
@csar

1. GetStdHandle Retrieves a handle to the device's standard input, standard output, or standard error.
2. SetConsoleMode sets the input mode of the console input buffer or the output mode of the console screen buffer.
3. WriteConsole prints Hello, World!
4. FlushConsoleInputBuffer disables the console input buffer.
5. ReadConsole reads a character from the console.

N
none7, 2016-05-03
@none7

What the functions return is what we push, that is:

invoke  AllocConsole
    invoke  GetStdHandle, STD_INPUT_HANDLE
        push    eax ;InputHandle
        push    eax ;InputHandle
    invoke  GetStdHandle, STD_OUTPUT_HANDLE
        push    eax ; OutputHandle
    invoke  SetConsoleMode, eax, ENABLE_PROCESSED_OUTPUT ; OutputHandle
        pop eax
    invoke  WriteConsole, eax, hello, msgsz, NULL, NULL ; OutputHandle
    invoke  FlushConsoleInputBuffer ; функция сама забирает 1 аргумент, InputHandle
        pop eax
    invoke  ReadConsole, eax, buf, bufsz, cnt, NULL ; InputHandle
    invoke  FreeConsole
    invoke  ExitProcess,0

Compilers don't do that, by the way. They will either store the values ​​in ebx, esi, edi or they will store them on the stack via [ebp-x].

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question