Hello World in Assembly Language

Our first program will print the classic “hello world” message. Here’s the full source code.

section .data
    hello db 'hello world', 0    ; null-terminated string

section .text
    global _start

_start:
    ; write our string to stdout
    mov edx, 12                  ; message length
    mov ecx, hello               ; message to write
    mov ebx, 1                   ; file descriptor (stdout)
    mov eax, 4                   ; syscall number (sys_write)
    int 0x80                     ; call kernel

    ; exit program
    mov eax, 1                   ; syscall number (sys_exit)
    xor ebx, ebx                 ; exit code 0
    int 0x80                     ; call kernel

To run the program, save the code in a file called hello-world.asm and use nasm and ld to assemble and link it.

$ nasm -f elf32 hello-world.asm -o hello-world.o
$ ld -m elf_i386 hello-world.o -o hello-world
$ ./hello-world
hello world

Sometimes we’ll want to debug our programs to see what they’re doing step-by-step. For this, we can use a tool like gdb.

First, compile the code with debugging symbols and then run it with gdb:

$ nasm -f elf32 -g -F dwarf hello-world.asm -o hello-world.o
$ ld -m elf_i386 hello-world.o -o hello-world
$ gdb ./hello-world

Now that we can run, build, and debug basic assembly programs, let’s learn more about the language.