๐ NASM Assembly Programming - Complete Tutorial
Welcome to your comprehensive NASM assembly programming course! This tutorial will take you from beginner to advanced assembly programmer through structured, hands-on lessons.
๐ Course Structure
This course is organized into progressive levels with 20 core topics plus advanced projects.
๐ Directory Structure
nasm-tutorial/
โโโ README.md # This file - course overview
โโโ topics/ # Individual topic lessons
โ โโโ topic-01-setup.md
โ โโโ topic-02-registers.md
โ โโโ ...
โโโ qna/ # Common questions and deep dives
โโโ push-pop-explained.md
โโโ int-vs-syscall.md
โโโ memory-addressing.md
โโโ sections-and-optimization.md
๐ฏ Complete Curriculum
LEVEL 1: Foundation (Weeks 1-2)
โ Topic 1: Setup & First Program
- Install NASM and linker
- Understand assembly workflow
- Write โHello Worldโ using syscalls
- Learn basic program structure
โ Topic 2: Registers & Data Types
- General purpose registers
- Register sizes (64, 32, 16, 8-bit)
- Data types: DB, DW, DD, DQ
- Register conventions
๐ Supplementary: Instruction Encoding
- How assembly becomes machine code
- REX prefix and 64-bit mode
- Opcode, ModR/M, SIB bytes
- Why some instructions are shorter
- Optimization techniques
โ Topic 3: Basic Instructions
- MOV and data movement
- Arithmetic: ADD, SUB, INC, DEC, NEG, MUL, DIV
- Bitwise: AND, OR, XOR, NOT, TEST
- Shifts and rotates: SHL, SHR, SAL, SAR, ROL, ROR
- LEA for address calculation and arithmetic
- CMP for comparisons
LEVEL 2: Control Flow (Weeks 3-4)
โ Topic 4: Flags & Comparisons
- The RFLAGS register structure
- Six arithmetic flags: CF, ZF, SF, OF, PF, AF
- How instructions affect flags
- CMP instruction (compare)
- TEST instruction (test bits)
- Signed vs unsigned flag interpretation
- Flag manipulation and common patterns
โ Topic 5: Conditional Jumps
- How jumps work (RIP register)
- Unconditional jumps: JMP
- Conditional jumps based on flags
- Signed comparisons: JG, JGE, JL, JLE, JE, JNE
- Unsigned comparisons: JA, JAE, JB, JBE
- If/else statement implementation
- Loop construction
- LOOP instruction and variants
- Practical examples and patterns
โ Topic 6: Loops
- Loop fundamentals and structure
- Counted loops (for equivalent)
- Condition-tested loops (while/do-while)
- LOOP instruction and variants
- Loop patterns (array iteration, search, accumulation)
- Loop control (break/continue)
- Nested loops
- Loop optimization techniques
- Practical examples (bubble sort, Fibonacci, matrix multiplication)
LEVEL 3: The Stack (Week 5)
โ Topic 7: Stack Operations
pushandpopmechanics- Stack pointer (ESP/RSP)
- Stack alignment
- Complete with C code equivalents
โ Topic 8: Stack Frames
- Base pointer (EBP/RBP)
- Function prologue/epilogue
- Local variables on stack
- Complete with C code equivalents
LEVEL 4: Functions & Procedures (Weeks 6-7)
โ Topic 9: Calling Conventions
- cdecl, stdcall, fastcall
- System V AMD64 ABI (Linux)
- Microsoft x64 (Windows)
- Complete with C code equivalents
โ Topic 10: Procedures
callandretinstructions- Return values
- Recursive functions
- Complete with C code equivalents
LEVEL 5: Memory & Addressing (Week 8)
โ Topic 11: Memory Addressing Modes
- Direct, indirect, indexed
- Scaled indexed addressing
- RIP-relative (x64)
- SIB addressing with scale factors
- Complete with C code equivalents
โ Topic 12: Arrays & Strings
- String instructions (MOVS, LODS, STOS, SCAS, CMPS)
- Direction flag
repprefix- Complete with C code equivalents
LEVEL 6: Advanced Operations (Weeks 9-10)
โ Topic 13: Multiplication & Division
mul/imul- unsigned and signed multiplicationdiv/idiv- unsigned and signed division- 64-bit results and RDX:RAX usage
- Complete with C code equivalents
โ Topic 14: Shifts & Rotates (Advanced)
- Logical shifts:
shl, shr - Arithmetic shift:
sar - Rotates:
rol, ror, rcl, rcr - Complete with C code equivalents
โ Topic 15: Macros & Directives
%define,%macro- Conditional assembly (%if, %ifdef)
%includeand file organization- Complete with practical examples
LEVEL 7: System Programming (Weeks 11-12)
โ Topic 16: Linux System Calls
syscallinstruction and mechanism- Syscall numbers and register usage (RAX, RDI, RSI, RDX, R10, R8, R9)
- File I/O: read, write, open, close
- Process management: fork, exec, wait
- Memory management: brk, mmap, munmap
- Error handling and return values
- Complete with C code equivalents
โ Topic 17: Interfacing with C
- Calling conventions (System V AMD64 ABI)
- Calling C from assembly
- Calling assembly from C
- Stack alignment requirements
- Using
printf,malloc, and libc functions - Creating reusable assembly libraries
- Inline assembly in C
- Complete with C code equivalents
LEVEL 8: Optimization & Advanced (Weeks 13-14)
โ Topic 18: SIMD Instructions
- SSE/AVX fundamentals
- XMM/YMM registers (128-bit/256-bit)
- Packed operations on multiple data elements
- Vector arithmetic, comparisons, shuffling
- Practical examples (array sum, dot product)
- Performance gains from vectorization
- Complete with C code equivalents
โ Topic 19: Performance & Optimization
- CPU architecture and pipeline fundamentals
- Instruction latency vs throughput
- Memory hierarchy and cache optimization
- Loop unrolling and multiple accumulators
- Branch prediction and branchless code
- Strength reduction and induction variables
- Data alignment and SIMD optimization
- Profiling with perf and RDTSC
- Complete optimization checklist
โ Topic 20: Debugging & Tools
- GDB comprehensive guide (breakpoints, watchpoints, TUI mode)
- Objdump for disassembly
- Strace for syscall tracing
- Valgrind for memory debugging
- Readelf for ELF analysis
- Common debugging scenarios (segfault, infinite loop, wrong results)
- Core dumps and remote debugging
- Reverse engineering basics
โ Q&A Section - Deep Dives
These files contain detailed explanations of specific questions that came up during learning:
- Push and Pop Explained
- Stack mechanics
- LIFO behavior
- Swapping values with push/pop
- int 0x80 vs syscall
- When to use each
- Register differences
- Syscall number differences
- Performance implications
- Memory Addressing with [ ] Brackets
- Dereferencing explained
- All addressing modes
- Practical examples
- Common mistakes
- Sections (.text, .data, .bss) and XOR Optimization
- Memory layout
- Section purposes
- Relation to C code
- Why
xor reg, regis better thanmov reg, 0
- CMP vs TEST - Whatโs the Difference?
- CMP does subtraction, TEST does AND
- When to use each instruction
- Performance comparison
- Common mistakes and decision tree
- Linux x86-64 Syscall Reference
- Complete syscall register usage (RAX, RDI, RSI, RDX, R10, R8, R9)
- Common syscalls with examples (read, write, open, close, exit)
- File I/O, process control, memory management
- Complete syscall number tables
- Error handling and return values
- Practical examples for each syscall
- All Types of Jumps - Complete Reference
- Unconditional jumps (JMP)
- Conditional jumps: signed (JG, JL, etc.) and unsigned (JA, JB, etc.)
- Flag-specific jumps (JZ, JS, JC, JO, JP)
- Loop instructions (LOOP, LOOPE, LOOPNE)
- Function calls (CALL/RET)
- Indirect jumps and jump tables (switch statements)
- Conditional move (CMOVcc) as branchless alternative
- Complete reference table with all 40+ jump variants
- Performance optimization patterns
- Instruction Size (Bytes) vs Execution Time (Cycles)
- Why we measure BOTH bytes and cycles
- Bytes = space in memory (code size, cache efficiency)
- Cycles = execution time (performance, speed)
- How instruction size affects I-cache performance
- Real-world examples showing both measurements matter
- When to optimize for bytes vs cycles
- Measuring techniques for each (objdump, RDTSC, perf)
- Complete comparison with benchmarks
๐ Progress Tracking
โ Foundation (Weeks 1-2):
- โ Topic 1: Setup & First Program
- โ Topic 2: Registers & Data Types
- โ Topic 3: Basic Instructions
โ Control Flow (Weeks 3-4):
- โ Topic 4: Flags & Comparisons
- โ Topic 5: Conditional Jumps
- โ Topic 6: Loops
โ The Stack (Week 5):
- โ Topic 7: Stack Operations
- โ Topic 8: Stack Frames
โ Functions & Procedures (Weeks 6-7):
- โ Topic 9: Calling Conventions
- โ Topic 10: Procedures
โ Memory & Addressing (Week 8):
- โ Topic 11: Memory Addressing Modes
- โ Topic 12: Arrays & Strings
โ Advanced Operations (Weeks 9-10):
- โ Topic 13: Multiplication & Division
- โ Topic 14: Shifts & Rotates (Advanced)
- โ Topic 15: Macros & Directives
โ System Programming (Weeks 11-12):
- โ Topic 16: Linux System Calls
- โ Topic 17: Interfacing with C
โ Optimization & Advanced (Weeks 13-14):
- โ Topic 18: SIMD Instructions
- โ Topic 19: Performance & Optimization
- โ Topic 20: Debugging & Tools
Supplementary:
- โ Instruction Encoding (Advanced)
- โ All Q&A articles with C equivalents
- โ Comprehensive syscall reference
๐ ALL 20 TOPICS COMPLETED! Over 30,000 lines of comprehensive content!
๐ How to Use This Tutorial
- Sequential Learning: Follow topics in order - each builds on previous knowledge
- Hands-On Practice: Type and run every code example
- Complete Exercises: Practice problems reinforce concepts
- Check Understanding: Review knowledge checks before moving on
- Reference Q&A: Deep dive into specific questions as needed
๐ ๏ธ Prerequisites
- Linux system (64-bit recommended)
- NASM installed:
sudo yum install nasm - GCC for linking:
sudo yum install gcc - Text editor (vim, nano, VSCode, etc.)
- Terminal access
๐ Quick Command Reference
# Assemble 64-bit
nasm -f elf64 program.asm -o program.o
# Link
ld -o program program.o
# Run
./program
# Check exit code
echo $?
# Assemble with debug info
nasm -f elf64 -g -F dwarf program.asm
# Debug with GDB
gdb ./program
๐ฏ Learning Goals
By completing this course, you will be able to:
โ Write complete assembly programs from scratch โ Understand x86-64 architecture deeply โ Read and understand compiler output โ Optimize code at the lowest level โ Debug assembly with confidence โ Interface assembly with high-level languages โ Work with system calls and OS interfaces โ Apply assembly knowledge to reverse engineering and security
๐ Additional Resources
- Official NASM Documentation: https://www.nasm.us/docs.php
- Intel Manuals: Intelยฎ 64 and IA-32 Architectures Software Developer Manuals
- Linux Syscall Table: https://filippo.io/linux-syscall-table/
- Practice: pwn.college, crackmes.one
๐ค Contributing
Found an error? Have a question? Want to add content? Feel free to modify and extend these materials!
Current Status: ๐ ALL 20 TOPICS COMPLETED! Over 30,000 lines of comprehensive NASM assembly programming content with C equivalents throughout!
Happy Assembly Programming! ๐