Writing Hello World in Assembly Using NASM on Windows

Nov 23, 2025 · Programming · 9 views · 7.8

Keywords: NASM | Assembly Language | Windows Programming | Hello World | System Calls

Abstract: This article provides a comprehensive guide to writing Hello World programs in assembly language using NASM on Windows. It covers multiple implementation approaches including direct Windows API calls and C standard library linking, with complete code examples, compilation commands, and technical explanations. The discussion extends to architectural differences and provides essential guidance for assembly language beginners.

Introduction

Assembly language, being the programming language closest to hardware, holds an irreplaceable position in system programming and performance optimization. Writing a Hello World program using NASM (Netwide Assembler) in the Windows environment serves as a crucial starting point for learning assembly language. This article delves into several different implementation methods to help readers understand the basic structure of assembly programs and programming characteristics in the Windows environment.

Implementation Using C Standard Library

For beginners, implementing a Hello World program by linking the C standard library is the simplest and most straightforward approach. This method leverages C language's standard input-output functions, significantly reducing the complexity of writing assembly programs.

Complete code example:

global  _main
extern  _printf

section .text
_main:
    push    message
    call    _printf
    add     esp, 4
    ret

message:
    db  'Hello, World', 10, 0

Code Analysis:

Compilation and Linking Process

Use the following commands for compilation and linking:

nasm -fwin32 helloworld.asm
gcc helloworld.obj

Compilation parameter explanation:

Direct Windows API Implementation

To gain deeper understanding of Windows system programming, we can bypass the C standard library and directly call Windows API functions. Although this approach is more complex, it better demonstrates the direct interaction between assembly language and the operating system.

Core API Functions:

16-bit DOS Environment Implementation

For historical compatibility considerations, understanding implementation in the 16-bit DOS environment is also valuable:

org 100h
mov dx, msg
mov ah, 9
int 21h
mov ah, 4Ch
int 21h
msg db 'Hello, World!', 0Dh, 0Ah, '$'

This implementation uses DOS interrupt calls and is suitable for DOS environments or Windows NTVDM compatibility mode.

Technical Summary

By comparing the different implementation approaches above, we can summarize several important technical points:

Understanding Calling Conventions: In the Windows environment, it's essential to correctly understand the stdcall calling convention, including parameter passing order and stack balancing responsibility.

Memory Model Selection: Choose appropriate memory models based on the target platform, such as 32-bit protected mode or 16-bit real mode.

System Call Hierarchy: From high-level C library calls to low-level system calls, different levels provide varying abstraction levels and performance characteristics.

Practical Recommendations

For assembly language beginners, we recommend following these learning steps:

  1. First master the simple implementation using C standard library
  2. Understand basic assembly syntax and program structure
  3. Gradually delve into direct Windows API calls
  4. Practice different compilation and linking options
  5. Use debugging tools to analyze program execution processes

Through systematic learning and practice, readers will be able to master the core skills of assembly programming using NASM in the Windows environment, laying a solid foundation for subsequent more complex system programming tasks.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.