1

I'd like to write super simple Hello World Win32 program in assembly using the printf to compile with ml and link provided with my MSVC.

Here is the code I've written so far:

.386
.model flat, stdcall
option casemap :none

EXTERN printf :PROC ; declare printf

.data
    HelloWorld db "Hello World!\n"

.code
main:
  push offset HelloWorld
  call printf
  ret
end main

I'm compiling it like that:

ml.exe /c HelloWorld.asm

And then linking:

link HelloWorld.obj libcmt.lib

without any problems BUT when i run it (under the PowerShell) the program doesn't display ANYTHING!

What's wrong whit the code and how to correct it to work?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
darek_911
  • 81
  • 1
  • 7
  • Maybe something like [this](https://stackoverflow.com/a/33725587/2189500)? – David Wohlferd Oct 20 '21 at 19:45
  • 1
    Also you are missing the null terminator in your string. – CherryDT Oct 20 '21 at 22:50
  • Resolved by https://stackoverflow.com/questions/69643447/how-to-compile-a-hello-world-assembly-on-windows/69654244#69654244 – vengy Oct 20 '21 at 23:54
  • @CherryDT Sorry for a such an obvious question, but can You please tell me how does the string should be terminated in that case to work? – darek_911 Oct 21 '21 at 09:22
  • https://stackoverflow.com/a/19038040/1871033 - `HelloWorld db "Hello World!\n",0` - Without the null byte, `printf` wouldn't know where the string ends and would continue reading ad infinitum, into unrelated and possibly non-existing memory areas, which is undefined behavior. I think MASM should also support [`.asciiz`](https://chortle.ccsu.edu/assemblytutorial/Chapter-20/ass20_2.html), in which case `HelloWorld: .asciiz "Hello World!\n"` would work too and do the same. – CherryDT Oct 21 '21 at 09:56

0 Answers0