Writing a simple line for console using masm (assembly code)

I would like to write a line in console release using writeconsole api but it doesn't work i link and create it using console in masm

here is the code

option casemap:none 
include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib 


Msg  db "Hello World",13,10,0
lmessage dd 13


consoleOutHandle dd ? 
bytesWritten dd ?

mov [consoleOutHandle],eax

invoke WriteConsole, consoleOutHandle,offset Msg,offset lmessage,offset bytesWritten,0
INVOKE ExitProcess,0 
end start


when i run the exe output i got the following

C: \ masm32> 18.exe

C: \ masm32>

empty output

so any advice


source to share

2 answers

The third parameter is the number of characters to write, not the address of the number of characters to be written. Luckily for you, the address turned out to be over 64K, which caused the call to fail with error code ERROR_NOT_ENOUGH_MEMORY.



One obvious problem is that you haven't defined the stack:

.stack 8192


This needs to be done after the directive .MODEL

, but otherwise the location doesn't really matter. As little stack space as you use, you could make it as little as 4096 bytes, but that won't matter much anyway.

When you call WriteFile, you also want to pass in the actual size of the data being written. I usually figure it out, something like:

message db "Hello World!", 13, 10
msg_size equ $ - offset message

; ...

invoke WriteFile,                   \
       eax,                         \
       offset message,              \
       msg_size,                    \
       offset written,              \


Note that instead of storing the standard output descriptor in memory, I simply passed it directly from EAX, where it GetStdHandle

returns it. For a non-trivial program, keeping it in memory is usually the right solution.



All Articles