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

.386
.MODEL Flat,STDCALL
option casemap:none 
include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib 

STD_OUTPUT_HANDLE EQU -11

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

.DATA?

consoleOutHandle dd ? 
bytesWritten dd ?

.code
start:
INVOKE GetStdHandle, STD_OUTPUT_HANDLE
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

+3


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.



+1


source


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,              \
       0

      

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.

0


source







All Articles