TechBuzzPro

Hello World in Assembly on Windows

· 5 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Writing a "Hello, World!" program in Assembly on Windows means creating a minimal program that displays text using low-level instructions, demonstrating direct interaction with the operating system and CPU. This guide walks you through writing, assembling, and running such a program using NASM (Netwide Assembler) and linking it for 32-bit Windows. While Assembly is complex, this example focuses only on the essentials to get output working.

What You'll Need

  • A Windows PC (any modern version)
  • NASM (Netwide Assembler) installed
  • A linker (GoLink or MinGW's ld)
  • A plain text editor (Notepad, VS Code, etc.)
  • Command Prompt or PowerShell

Step-by-Step

  1. Install NASM: Download the latest NASM installer from the official site. Run it and follow the prompts. During installation, ensure the option to add NASM to the system PATH is selected. If not, you’ll need to add the NASM directory to your PATH manually via System Properties > Environment Variables.
  2. Install a linker: Download GoLink (a lightweight linker for Windows). Extract GoLink.exe and place it in a convenient folder, such as C:\asm\. Alternatively, if you have MinGW or MinGW-w64 installed, use its ld linker. Make sure the linker is accessible from the command line by placing it in a PATH directory or calling it with a full path.
  3. Create your Assembly source file: Open a text editor and create a new file named hello.asm. Save it in a working directory like C:\asm\. Enter the following code exactly:
section .data
    msg db 'Hello, World!', 0

section .text
    global _main
    extern _printf

_main:
    push    msg
    call    _printf
    add     esp, 4
    ret
  1. Assemble the code: Open Command Prompt or PowerShell, navigate to your working directory using cd C:\asm\, then run:
    nasm -f win32 hello.asm -o hello.obj
    This tells NASM to generate a 32-bit Windows object file. If you see no output, the assembly succeeded. If there’s an error, double-check your code and NASM installation.
  2. Link the object file: Now link hello.obj into an executable. If using GoLink, run:
    GoLink /console hello.obj
    If using MinGW’s ld, the command is more complex and requires linking against the C runtime. GoLink is simpler for beginners.
  3. Run the program: After linking, you should have hello.exe. Run it by typing:
    hello.exe
    If everything worked, you’ll see "Hello, World!" printed in the console. The program exits immediately, so ensure you’re running it in a console window that stays open.
  4. Verify output: If no text appears or the program crashes, check the error messages. Common issues include missing runtime libraries, incorrect calling conventions, or mismatched function names (e.g., _printf vs printf). The C runtime must be available; GoLink automatically links against MSVCRT.

Common Mistakes

  • Forgetting to use _printf instead of printf: Windows C functions are prefixed with an underscore in Assembly.
  • Not using -f win32 flag: This is required for proper object format on Windows.
  • Mixing 32-bit and 64-bit tools: NASM defaults to 32-bit output, but your linker or runtime must match. Use 32-bit tools throughout.
  • Missing the add esp, 4 after call: This cleans up the stack after pushing the argument. Without it, the stack becomes corrupted.
  • Running in an environment without a console: Double-clicking hello.exe may cause a flash and close. Always run from Command Prompt.

Tips

  • Use GoLink for simplicity: It automatically resolves C library dependencies and creates console executables with minimal flags.
  • Keep your working directory organized: Place NASM, GoLink, and your source files in the same folder to avoid path issues.
  • Use db for strings: Define your message with db (define byte) and null-terminate it with , 0.
  • Test incrementally: Start with a working template and modify one part at a time.
  • Check NASM syntax: NASM uses Intel syntax; do not use AT&T syntax directives.

Troubleshooting

If the program fails to assemble, check the NASM error output. Common syntax errors include missing colons after labels or incorrect section names. If linking fails, ensure GoLink or ld is in your PATH or specify the full path. If the program compiles but crashes, verify that you’re using _printf and that the stack is balanced (push and pop counts match). If "Hello, World!" doesn’t appear, run the executable from Command Prompt to see output. If you get "not found" errors for _printf, your linker isn’t finding MSVCRT; GoLink handles this automatically, but ld may require explicit library specification.

For 64-bit systems, ensure you’re using 32-bit tooling consistently. NASM can generate 64-bit code with -f win64, but that requires a different calling convention and entry point (e.g., main instead of _main). Stick to 32-bit unless you’re ready to handle those complexities.

Frequently Asked Questions

Can I write Hello World in Assembly without a C library?

Yes, but it requires calling Windows API functions like WriteConsole directly, which involves more setup: getting handles, defining function prototypes, and managing data structures. Using printf is simpler for beginners.

Why do I need a linker?

Assembly produces object code, not a runnable executable. The linker combines object files and resolves external references (like _printf) to create a final executable.

Can I use MASM instead of NASM?

Yes, but MASM uses different syntax and tooling (ML.EXE). This guide uses NASM because it’s free, widely available, and uses familiar Intel syntax.

Does this work on 64-bit Windows?

Yes, but you’re creating a 32-bit executable. It runs via WoW64 compatibility layer. For native 64-bit, you’d need to target win64 and use 64-bit calling conventions.

Why is the message followed by 0?

In C-style strings, a null byte (0) marks the end. printf reads until it finds this terminator to know where the string ends.

Can I make a GUI window instead?

Yes, but it requires calling Windows GUI functions like CreateWindow and handling messages. That’s significantly more complex than a console output and beyond this basic example.