Your first program

The source file

Create hello.asm with the two system calls you saw in lesson 1.1: sys_write (1) prints bytes, and sys_exit (60) ends the process:

section .data msg db "Hello from assembly", 10 len equ $ - msg section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, len syscall mov rax, 60 xor rdi, rdi syscall

Assemble and link

Assemble to an ELF object, then link it into an executable. The -f elf64 flag selects the 64-bit ELF output format:

nasm -f elf64 -o hello.o hello.asm
ld -o hello hello.o

Run it

Run the executable and check the exit status:

./hello
echo $?

Hello from assembly
0

Anatomy

global _start makes the entry point visible to the linker. Each mov loads the call number or one argument of the system call into a register: rax holds the call number, rdi the first argument, rsi the second, rdx the third. The final syscall never returns, because sys_exit ends the process.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does nasm -f elf64 -o hello.o hello.asm produce?

    Answer

    An ELF64 object file

    NASM writes an object file; the linker is the tool that makes it executable.

  2. What does ld -o hello hello.o do?

    Answer

    It links hello.o into the executable hello

    ld takes the object file and writes the executable named after -o.

  3. Which flag makes NASM write a 64-bit ELF object?

    Answer

    -f elf64

    -f selects the output format, and elf64 is the 64-bit ELF one.

  4. What exit status does the hello program on this page return?

    Answer

    0

    rdi is cleared before sys_exit, so the status is 0.

  5. What does echo $? print right after ./hello?

    Answer

    The exit status of the last command

    The shell keeps the exit status of the last command in $?.

  6. What does this program print? section .data msg db "Hi", 10 section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, 3 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    Hi

    The buffer is Hi plus a newline, and rdx asks for three bytes.

  7. Which register gives the exit status to sys_exit?

    Answer

    rdi

    rdi is the first argument of the call, and for sys_exit that argument is the status.

  8. Which register gives sys_write the number of bytes to print?

    Answer

    rdx

    rdi is the file descriptor, rsi the buffer and rdx the count.

  9. The program writes Hello, a newline and world, but rdx is 5. What does the reader see?

    Answer

    Hello

    write prints exactly rdx bytes, so only the first five letters reach the screen.

  10. Which label does ld use as the entry point of hello?

    Answer

    _start

    A program with no C library starts at _start, the label the source exports with global.

The code on this page was assembled with NASM 3.01 and linked with GNU ld 2.45.1, and the program was run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev