简体   繁体   中英

x86 assembly - GetStdHandle & WriteConsole

In response to my question about Windows API's, I have successfully gotten it to work. My question is in regards to this code:

push STD_OUTPUT_HANDLE
    call GetStdHandle
    push NULL
    push offset other
    push mlen
    push offset msg
    push eax
    call WriteConsole
push    0
call ExitProcess

This code is supposed to print the value of msg . Why does one need to do:

a)

push STD_OUTPUT_HANDLE
    call GetStdHandle
    push NULL

And:

b)

push offset other
    push mlen
    push offset msg
    push eax

I am just wondering what the need is for getting a StdHandle and pushing offsets.

Thanks in advance,

Progrmr

Look at the definition of WriteConsole . The NULL is the last argument of the function, the lpReserved argument. Arguments are pushed in right-to-left order. The first function argument is the console handle, the one you got from GetStdHandle and you pass by pushing eax.

So properly commenting the assembly code:

push STD_OUTPUT_HANDLE          ; GetStdHandle nStdHandle argument
call GetStdHandle               ; eax = Console handle
push NULL                       ; lpReserved = null
push offset other               ; lpNumberOfCharsWritten = pointer to "other"
push mlen                       ; nNumberOfCharsToWrite = length of "msg"
push offset msg                 ; lpBuffer = pointer to "msg"
push eax                        ; hConsoleOutput = console handle from GetStdHandle
call WriteConsole               ; Write string
push    0                       ; exit code = 0
call ExitProcess                ; terminate program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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