Assembly Learning Lab
MODULE 00 · Development Environment and Toolchain

Your first Linux program

Assemble, link, and single-step a NASM program that returns an exit status to Linux.

25 min

Learning objectives

  • Explain what the assembler, linker, Linux loader, and debugger each do.
  • Trace two register writes and identify the values Linux reads when a program exits.
  • Assemble and inspect a 64-bit Linux executable and verify its exit status.

You need a Linux x86-64 shell, including Arch Linux under WSL2 on an x86-64 host. Familiarity with a simple C main function is helpful.

Background and motivation

When a shell launches a program, it can read the program’s exit status. A status of zero conventionally means success. We will return 42 so that we can verify a specific value traveled from our source file to the shell.

This is a Linux userspace program. It runs under an existing kernel and uses that kernel to terminate. A bare-metal bootloader begins in a different environment and has no Linux process to exit from.

Fundamental concepts

Assembly is a human-readable notation for machine instructions. An assembler translates assembly into an object file containing encoded instructions, symbols, and other information. A linker lays out one or more object files and produces an executable. On Linux, ELF (Executable and Linkable Format) describes how the linker and operating system interpret these files.

A register is a named storage location exposed by the CPU. RAX and RDI are 64-bit general-purpose registers. EAX and EDI refer to their low 32 bits. In 64-bit mode, writing a 32-bit general-purpose register also clears the upper 32 bits of its 64-bit parent.

Hardware-level explanation

Linux maps the executable into the new process and starts it at the executable’s entry address. The value in RIP, the instruction pointer, identifies the location of the next instruction to execute. The CPU decodes the bytes at that address and applies the instruction’s defined effects. The control unit is a conceptual name for the circuitry that coordinates these actions. The ALU, or arithmetic logic unit, performs operations such as integer addition and bitwise AND.

Calling syscall transfers control to a kernel entry point configured by Linux. The kernel interprets register values according to a system call convention, a set of rules for passing a service number and arguments. The CPU defines the register and control-flow effects of instructions. The techniques a particular CPU uses internally to perform those operations are its microarchitecture.

Glossary

English term Meaning
Mnemonic The readable name of an instruction, such as mov.
Operand A value or location used by an instruction, such as a register or a literal.
Immediate A literal value encoded in the instruction.
Opcode The part of a machine instruction’s encoding that selects the operation.
Entry point The address where program execution begins.
RFLAGS The x86-64 register containing condition and control bits.
ABI Application binary interface, the rules compiled code follows at a binary boundary.

Illustrated explanation

x86-64 CPUCONTROL UNITREGISTERSALUMEMORY控制單元暫存器算術邏輯單元記憶體
A conceptual CPU data path. 控制單元解碼指令,協調暫存器、算術邏輯單元與記憶體之間的資料流。

Our two mov instructions set registers. Linux then reads them after syscall transfers execution to the kernel. The diagram shows architectural roles at a conceptual level. Individual CPUs organize their internal hardware differently.

Instruction syntax

NASM uses Intel operand order. mov destination, source copies a source value into its destination. syscall has no explicit operands.

In mov eax, 60, the encoding is B8 3C 00 00 00. Here B8 selects the form that loads a 32-bit immediate into EAX. The bytes 3C 00 00 00 encode decimal 60 with the least significant byte first. That byte ordering is called little-endian.

Operand and register analysis

Instruction Data and effects
mov eax, 60 Copies the 32-bit immediate 60 into EAX and zeroes the upper half of RAX. Leaves RFLAGS unchanged.
mov edi, 42 Copies the 32-bit immediate 42 into EDI and zeroes the upper half of RDI. Leaves RFLAGS unchanged.
syscall Saves the following instruction’s address in RCX and user flags in R11, transfers control to the configured kernel entry address, and applies the configured flag mask. Linux reads the service number from RAX and the first argument from RDI.

The immediate forms of mov here have no explicit data-memory operands. The CPU still fetches the instruction bytes from memory. On Linux x86-64, syscall 60 terminates the calling thread. This program has one thread, so the process exits.

Step-by-step execution trace

Step RAX RDI RIP
Entry Do not assume a value Do not assume a value Points to the first mov
First mov 60 Unchanged Advances five bytes
Second mov 60 42 Advances another five bytes to syscall
syscall Read by Linux Read by Linux Transfers to kernel code and never returns to this program

C comparison

int main(void) {
    return 42;
}

The shell observes the same status. In the C program, startup code calls main, then the C runtime handles process termination when main returns. Our assembly program starts directly at _start and asks Linux to terminate.

Practical NASM experiment

On Arch Linux, install the tools with sudo pacman -Syu --needed nasm binutils gdb. Confirm uname -m prints x86_64. Save the following as exit.asm.

global _start

section .text
_start:
    mov eax, 60
    mov edi, 42
    syscall

section .note.GNU-stack noalloc noexec nowrite progbits

global exposes the _start symbol to the linker. A symbol gives an address or value a name. .text holds executable code. The empty .note.GNU-stack section tells the linker this object does not require an executable stack.

Compilation and linking

nasm -f elf64 -g -F dwarf exit.asm -o exit.o
ld -m elf_x86_64 -e _start -o exit exit.o
objdump -d -Mintel exit
./exit
printf '%s\n' "$?"

Expect 42 from printf. The program itself writes nothing. Read $?, the status of the last command, immediately after running the program. NASM’s -g -F dwarf options emit DWARF debugging information, giving GDB source-level context for the instructions.

GDB debugging exercise

gdb -q ./exit
(gdb) set disassembly-flavor intel
(gdb) starti
(gdb) x/3i $rip
(gdb) si
(gdb) info registers eax rip
(gdb) si
(gdb) info registers edi rip
(gdb) continue

starti stops at the first executed instruction. x/3i $rip displays three decoded instructions starting at RIP. Each si runs one machine instruction. After the first step EAX is 0x3c (60). After the second EDI is 0x2a (42). GDB may print the exit status in octal as 052, which is also 42.

Common mistakes

  • Using elf32 generates a 32-bit object. Use elf64 for this example.
  • Reversing the operands makes 60 the destination. An immediate is an encoded input and cannot be written.
  • Putting 42 in EAX changes the syscall number. The exit status belongs in EDI.
  • Running another command before checking $? replaces the value you want to inspect.

Exercises

  1. Change the program to return 7. Reassemble and relink, then verify the status.
  2. Before the second mov, insert mov rdi, 0x1234567800000000. Step through both writes to RDI and predict the final value.
Which register supplies the exit status in this program?

Answer explanations

Show exercise answers
  1. Replace mov edi, 42 with mov edi, 7. The shell should report 7 after rebuilding and running.
  2. The second write sets EDI to 42 (0x2a) and clears RDI’s high 32 bits. RDI ends at 0x000000000000002a.

References

Search curriculum