mohitmishra786/low-level-dev-skills

assembly-x86

x86-64 assembly skill for reading, writing, and debugging assembly code.

Ver código fuente
Documento original del Skill

Contenido del repositorio de origen con títulos, ejemplos, código, tablas, enlaces e imágenes preservados.

x86-64 Assembly

Purpose

Guide agents through x86-64 assembly: reading compiler output, understanding the ABI, writing inline asm, and common patterns.

Triggers

  • "How do I read the assembly GCC generated?"
  • "What are the x86-64 registers?"
  • "What is the calling convention on Linux/macOS?"
  • "How do I write inline assembly in C?"
  • "How do I use SSE/AVX intrinsics?"
  • "This assembly uses %rsp / %rbp — what does it mean?"

Workflow

1. Generate and read assembly

bash
# AT&T syntax (GCC default)
gcc -S -O2 -fverbose-asm foo.c -o foo.s

# Intel syntax
gcc -S -masm=intel -O2 foo.c -o foo.s

# From GDB
(gdb) disassemble /s main    # with source
(gdb) x/20i $rip

# From objdump
objdump -d -M intel -S prog  # Intel + source (needs -g)

2. x86-64 registers

64-bit32-bit16-bit8-bit high8-bit lowPurpose
%rax%eax%ax%ah%alReturn value / accumulator
%rbx%ebx%bx%bh%blCallee-saved
%rcx%ecx%cx%ch%cl4th arg / count
%rdx%edx%dx%dh%dl3rd arg / 2nd return
%rsi%esi%si—%sil2nd arg
%rdi%edi%di—%dil1st arg
%rbp%ebp%bp—%bplFrame pointer (callee-saved)
%rsp%esp%sp—%splStack pointer
%r8–%r11%r8d–%r11d%r8w–%r11w—%r8b–%r11b5th–8th args / caller-saved
%r12–%r15%r12d–%r15d%r12w–%r15w—%r12b–%r15bCallee-saved
%ripInstruction pointer
%rflags%eflagsStatus flags
%xmm0–%xmm7FP/SIMD args and return
%xmm8–%xmm15Caller-saved SIMD
%ymm0–%ymm15AVX 256-bit
%zmm0–%zmm31AVX-512 512-bit

3. System V AMD64 ABI (Linux, macOS, FreeBSD)

Integer/pointer argument registers (in order): %rdi, %rsi, %rdx, %rcx, %r8, %r9

Floating-point argument registers: %xmm0–%xmm7

Return values:

  • Integer: %rax (low), %rdx (high if 128-bit)
  • Float: %xmm0 (low), %xmm1 (high)

Caller-saved (scratch): %rax, %rcx, %rdx, %rsi, %rdi, %r8–%r11, %xmm0–%xmm15

Callee-saved (must preserve): %rbx, %rbp, %r12–%r15

Stack: 16-byte aligned before call; call pushes 8 bytes → 16-byte aligned at function entry after prologue.

Red zone: 128 bytes below %rsp may be used by leaf functions without adjusting %rsp. Not available in kernel/signal handlers.

4. Common instruction patterns

PatternMeaning
mov %rdi, %raxCopy rdi to rax
mov (%rdi), %raxLoad 8 bytes from address in rdi
mov %rax, 8(%rdi)Store rax to rdi+8
lea 8(%rdi), %raxLoad effective address rdi+8 into rax (no memory access)
push %rbxPush rbx; rsp -= 8
pop %rbxPop into rbx; rsp += 8
call fooPush return addr; jmp foo
retPop return addr; jmp to it
xor %eax, %eaxZero rax (smaller encoding than mov $0, %rax)
test %rax, %raxSet ZF if rax == 0 (cheaper than cmp $0, %rax)
cmp $5, %rdiSet flags for rdi - 5
jl labelJump if signed less than

5. AT&T vs Intel syntax

FeatureAT&TIntel
Operand ordersource, destdest, source
Register prefix%raxrax
Immediate prefix$4242
Memory operand8(%rdi)[rdi+8]
Size suffixmovl, movq— (inferred)

GCC emits AT&T by default. Use -masm=intel for Intel syntax.

6. Inline assembly (GCC extended asm)

c
// Basic: increment a register
int x = 5;
__asm__ volatile (
    "incl %0"
    : "=r"(x)   // outputs: =r means write-only register
    : "0"(x)    // inputs: 0 means same as output 0
    : // clobbers: none
);

// CPUID example
uint32_t eax, ebx, ecx, edx;
__asm__ volatile (
    "cpuid"
    : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
    : "a"(1)    // input: leaf 1
);

// Atomic increment
static inline int atomic_inc(volatile int *p) {
    int ret;
    __asm__ volatile (
        "lock; xaddl %0, %1"
        : "=r"(ret), "+m"(*p)
        : "0"(1)
        : "memory"
    );
    return ret + 1;
}

Constraint codes:

  • "r" — any general register
  • "m" — memory operand
  • "i" — immediate integer
  • "a", "b", "c", "d" — specific registers (%rax, %rbx, %rcx, %rdx)
  • "=" prefix — output (write-only)
  • "+" prefix — read-write
  • "memory" clobber — tells compiler memory may be modified (barrier)

7. SSE/AVX intrinsics (preferred over inline asm)

c
#include <immintrin.h>   // includes all x86 SIMD headers

// Add 8 floats at once with AVX
__m256 a = _mm256_loadu_ps(arr_a);   // load 8 floats (unaligned)
__m256 b = _mm256_loadu_ps(arr_b);
__m256 c = _mm256_add_ps(a, b);
_mm256_storeu_ps(result, c);

Check CPU support at compile time: -mavx2 or -march=native. Check at runtime: __builtin_cpu_supports("avx2").

For a full register and instruction reference, see references/reference.md.

Related skills

  • Use skills/low-level-programming/assembly-arm for AArch64/ARM assembly
  • Use skills/compilers/gcc for -S -masm=intel flag details
  • Use skills/debuggers/gdb for stepping through assembly (si, ni, x/i)
del mismo repositorio

Más Skills

Todos los Skills
mohitmishra786
Comunidad

static-analysis

Static analysis skill for C/C++ codebases. Use when hardening code quality, triaging noisy builds, running clang-tidy, cppcheck, or scan-build, interpreting check categories, suppressing false positives, or integrating static analysis into CI. Activates on queries about clang-tidy checks, cppcheck, scan-build, compilecommands.json, code hardening, or static analysis warnings.

instalaciones
1
GitHub Stars
263
Actualizado
27 jun
mohitmishra786
Comunidad

cpp-modules

C++20 modules skill for modern C++ projects. Use when working with named modules, module partitions, header units, CMake MODULESOURCES, Clang -fmodules-ts, BMI caching issues, or migrating from headers to modules. Activates on queries about C++20 modules, import statements, module interface units, header units, or BMI files.

instalaciones
1
GitHub Stars
258
Actualizado
27 jun
mohitmishra786
Comunidad

gcc

GCC compiler skill for C/C++ projects. Use when selecting optimization levels, warning flags, debug builds, LTO, sanitizer instrumentation, or diagnosing compilation errors with GCC. Covers flag selection for debug vs release, ABI concerns, preprocessor macros, profile-guided optimization, and integration with build systems. Activates on queries about gcc flags, compilation errors, performance tuning, warning suppression, or cross-standard compilation.

instalaciones
1
GitHub Stars
258
Actualizado
27 jun
mohitmishra786
Comunidad

interpreters

Bytecode interpreter and JIT compiler skill for implementing language runtimes in C/C++. Use when designing bytecode dispatch loops (switch, computed goto, threaded code), implementing stack-based or register-based VMs, adding a simple JIT using mmap/mprotect, or understanding performance trade-offs in interpreter design. Activates on queries about bytecode VMs, dispatch loops, computed goto, JIT compilation basics, tracing JITs, or implementing a scripting language runtime.

instalaciones
1
GitHub Stars
258
Actualizado
27 jun