Assembly Playfield: Build, Run & Debug (NASM, LD, GDB)

From MediaWiki
Jump to navigation Jump to search

Assembly Playfield: Build, Run & Debug (NASM, LD, GDB)

This page provides a practical starting point for experimenting with assembly programming under Linux, using NASM for assembling, LD for linking, and GDB for debugging.

The playfield program

To learn assembly, we use a minimal “playfield” — a small skeleton program that runs correctly but does nothing by default. It provides a clean structure that you can modify freely.

Basic structure of `playfield.asm`:

SECTION .data
SECTION .bss
SECTION .text
global _start

_start:
  nop               ; no operation - placeholder for experiments

  ; properly end program
  mov rax, 60       ; syscall number for sys_exit
  mov rdi, 0        ; exit code 0
  syscall           ; invoke kernel

This structure defines:

  • `.data` – initialized variables
  • `.bss` – uninitialized variables
  • `.text` – program instructions
  • `_start` – the program’s entry point

Assembling and linking

To build and link the playfield program manually:

nasm -f elf64 -g -F dwarf playfield.asm -l playfield.lst
ld -o playfield playfield.o

Or with a Makefile:

playfield: playfield.o
	ld -o playfield playfield.o

playfield.o: playfield.asm
	nasm -f elf64 -g -F dwarf playfield.asm -l playfield.lst

This creates an executable called `playfield`.

Running the program

Execute directly:

./playfield

Since it only runs a `nop` and then exits, nothing visible happens — but the environment is ready for experimentation.

Debugging with GDB

Use the GNU Debugger (GDB) to step through instructions and inspect registers.

Start with the text interface:

gdb --tui playfield

Useful commands inside GDB:

Command | Description


| ------------

l | List source code b N | Set a breakpoint at line N r | Run the program s | Step one instruction i r | Show register contents layout regs | Display and update all registers q | Quit GDB

Optional setup: create a `.gdbinit` file in your home directory with:

tui e
layout regs

Experiment ideas

Try editing `playfield.asm` and adding simple operations:

mov rax, 2
mov rbx, 3
add rax, rbx     ; result: 5 in RAX

Assemble, link, and debug again to observe register changes step-by-step.

Summary

The assembly playfield is your testbed for learning CPU instructions. With NASM, LD, and GDB, you can assemble, link, and inspect every part of the program execution cycle directly at the hardware level.