Search code examples
masm

Calling printf by MASM


I am trying to explore world of assembly :) But for some reason, it does not work :( It compiles, but at running, it prints out nothing :(

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\msvcrt.lib ; Some default includes :P

printf proto C :vararg

.data
Text       db "Hello World",0

.code
start:
mov eax, offset Text
push eax ; Push text to the stack
call printf 
pop eax ; Pop text from the stack
push 0 ; Exit code 0 (Success)
call ExitProcess
end start

Solution

  • You need to link with /SUBSYSTEM:CONSOLE for your current code to print anything.

    From the documentation for SUBSYSTEM:

    WINDOWS
    Application does not require a console, probably because it creates its own windows for interaction with the user.

    CONSOLE
    Win32 character-mode application. The operating system provides a console for console applications.