Skip to content
Pwnversity
PWN-ASMbasic · 6 min · upd. 2026-10

x86-64 Assembly

To exploit binaries and do reversing you need to read (and sometimes write) assembly: it’s the language code compiles to and the level you work at when manipulating execution. You don’t need to master it like a systems developer, but you do need to understand registers, basic instructions, how the stack works, and calling conventions. This card is the practical base for pwn-reversing.

Assembly is the textual representation of the machine code the CPU runs. Each instruction (mov, call, ret…) maps to a few bytes (opcodes). When a compiler translates C into a binary, it produces these instructions; when you disassemble with objdump or Ghidra, you walk the reverse path. In pwn you work on x86-64 (AMD64) most of the time, with forays into ARM/MIPS on specific challenges.

General-purpose registers are 64-bit, with 32/16/8-bit sub-registers:

  • RAX RBX RCX RDX RSI RDI RBP RSP and R8–R15.
  • Size equivalences on RAX: RAX (64) → EAX (32) → AX (16) → AL (8).

The special ones that drive the exploit:

  • RIP — instruction pointer: what runs. It’s the ultimate target of the exploit.
  • RSP — stack pointer: top of the stack.
  • RBP — base pointer: anchor of the function frame.
  • RFLAGS — flags from comparison results (ZF zero, CF carry, SF sign…).
  • mov dst, src — copy; lea dst, [addr] — load the address (not the contents).
  • add/sub dst, src — arithmetic; xor/and/or — logical (xor rax, rax zeroes RAX).
  • push/pop — push/pop the stack (adjust RSP).
  • call func — pushes the return address and jumps; ret — pops RIP.
  • cmp a, b — compare (sets flags); test a, a — check for zero.
  • jmp addr — unconditional jump; je/jne/jg/jl… — jumps based on flags.
  • nop — no operation (0x90), useful as a NOP sled in shellcode.
  • syscall — transition into the kernel (on 64-bit it replaces int 0x80).

The stack grows toward low addresses. On entering a function a prologue runs, and on exit an epilogue:

; typical prologue
push rbp ; save the caller's RBP
mov rbp, rsp ; new frame
sub rsp, 0x40 ; reserve 64 bytes for locals
; ... body ...
; epilogue
leave ; equivalent to: mov rsp, rbp ; pop rbp
ret ; pop rip -> returns to the saved return address

ret pops the address to return to off the top of the stack and loads it into RIP. If you control what’s at that stack position, you control RIP —the basis of the stack overflow (see Stack Buffer Overflow).

flowchart TD
    subgraph Stack["Stack inside a function (grows downward)"]
        A["local buffer (char buf[64])"]
        B["saved RBP"]
        C["return address ← overflow target"]
        D["caller arguments / frame"]
    end
    A --> B --> C --> D
    C -.->|"ret loads this into RIP"| E["RIP"]

Calling convention (System V, Linux x86-64)

Section titled “Calling convention (System V, Linux x86-64)”

Key for exploits: knowing which registers hold the arguments.

  • The first 6 arguments go, in order, in RDI, RSI, RDX, RCX, R8, R9.
  • The return value goes in RAX.
  • Arguments 7+ go on the stack.
  • Example: system("/bin/sh") → RDI = pointer to "/bin/sh", then call system.

That’s why in ROP you look for gadgets like pop rdi ; ret: to place the argument in RDI before calling a function.

; Intel (what most pwn tools use, more readable)
mov rax, rbx ; destination, source

In AT&T (GDB and GCC default) the order is reversed and prefixed: mov %rbx, %rax. To work comfortably in GDB: set disassembly-flavor intel (or use pwndbg/GEF, which already do).

Exploitation (lab): reading and writing assembly

Section titled “Exploitation (lab): reading and writing assembly”

Disassembling and assembling are everyday operations:

Ventana de terminal
objdump -d -M intel ./binary # disassemble in Intel syntax
gdb -q ./binary -ex 'disassemble main'

With pwntools you can assemble your own instructions and generate shellcode:

from pwn import *
context.arch = 'amd64'
sc = asm('xor rsi, rsi; xor rdx, rdx') # assemble to bytes
sh = asm(shellcraft.amd64.linux.sh()) # execve("/bin/sh") shellcode
print(disasm(sc)) # disassemble back to verify

Understanding assembly isn’t a defense in itself, but it’s the basis for auditing your own binaries, analyzing malware, and writing precise detections. On the development side, compilers insert the mitigation checks at the assembly level:

  • The stack canary shows up as a mov from fs:0x28 in the prologue and a cmp before the ret.
  • CFI (-fcf-protection) and Intel CET shadow stacks add endbr64 at function entry and validate returns.
  • FORTIFY_SOURCE replaces memcpy/strcpy with size-checked variants (__memcpy_chk).

Recognizing these patterns in the disassembly tells you which defenses are active without running anything.

  • I recognize the registers and their sizes (RAX/EAX/AX/AL)
  • I understand RIP/RSP/RBP and their roles
  • I read the basic instructions (mov, call, ret, jmp, cmp…)
  • I explain the prologue/epilogue and how ret controls RIP
  • I know the calling convention (RDI, RSI, RDX…)
  • I tell Intel syntax from AT&T
  • I disassemble with objdump/GDB/Ghidra
  • I understand how syscall makes kernel calls