Skip to content

Latest commit

 

History

History
238 lines (188 loc) · 3.97 KB

nasm.md

File metadata and controls

238 lines (188 loc) · 3.97 KB

NASM (Netwide Assembler) Cheatsheet

Installation Instructions

Windows

  1. Download official NASM binary from official website
  2. Add to PATH environment variable
# Using Chocolatey
choco install nasm

# Manual installation
# Download from nasm.us
# Add downloaded directory to PATH

Linux (Ubuntu/Debian)

sudo apt-get update
sudo apt-get install nasm

macOS

# Using Homebrew
brew install nasm

# Using MacPorts
sudo port install nasm

NASM Cheat Codes and Commands

Basic Syntax and Directives

  1. Basic Instruction Syntax
instruction destination, source
  1. Define Byte (8-bit)
db 42        ; Define single byte with value 42
db 'Hello'   ; Define string
  1. Define Word (16-bit)
dw 1000      ; Define 16-bit word
  1. Define Double Word (32-bit)
dd 65536     ; Define 32-bit integer
  1. Define Quad Word (64-bit)
dq 1000000   ; Define 64-bit integer
  1. Reserving Memory Space
buffer: resb 100   ; Reserve 100 bytes
  1. Global Symbol Declaration
global _start      ; Make symbol visible to linker
  1. External Symbol Import
extern printf      ; Import external function
  1. Macro Definition
%macro print 2     ; Macro with two parameters
    mov eax, 4
    mov ebx, 1
    mov ecx, %1
    mov edx, %2
    int 0x80
%endmacro
  1. Conditional Assembly
%ifdef DEBUG
    ; Code for debug build
%endif

Registers and Data Movement

  1. 64-bit Register Move
mov rax, 42        ; Move 64-bit value
  1. 32-bit Register Move
mov eax, 100       ; Move 32-bit value
  1. 16-bit Register Move
mov ax, 0xFFFF     ; Move 16-bit value
  1. 8-bit Register Move
mov al, 0x55       ; Move 8-bit value
  1. Register to Register Move
mov rax, rbx       ; Copy value from rbx to rax
  1. Memory to Register Move
mov rax, [address] ; Move value from memory to register
  1. Immediate to Memory Move
mov [address], 42  ; Move immediate value to memory

Arithmetic Operations

  1. Addition
add rax, 10        ; Add 10 to rax
  1. Subtraction
sub rbx, 5         ; Subtract 5 from rbx
  1. Multiplication
mul rcx            ; Multiply rax by rcx
  1. Division
div rdx            ; Divide rax by rdx
  1. Increment
inc rax            ; Increment rax by 1
  1. Decrement
dec rbx            ; Decrement rbx by 1

Comparison and Branching

  1. Compare Instructions
cmp rax, rbx       ; Compare rax and rbx
  1. Conditional Jumps
je label           ; Jump if equal
jne label          ; Jump if not equal
jg label           ; Jump if greater
jl label           ; Jump if less

Stack Operations

  1. Push to Stack
push rax           ; Push rax onto stack
  1. Pop from Stack
pop rbx            ; Pop top of stack to rbx

System Calls (Linux x86_64)

  1. Exit System Call
mov rax, 60        ; Exit syscall number
mov rdi, 0         ; Exit status
syscall            ; Invoke syscall
  1. Write System Call
mov rax, 1         ; Write syscall
mov rdi, 1         ; Stdout
mov rsi, message   ; Buffer
mov rdx, 14        ; Length
syscall

... [The remaining 71 cheat codes would follow a similar detailed format with examples and explanations]

Best Practices and Tips

  1. Always comment your code
; This is a comment explaining the code
mov rax, 42        ; Inline comment

Compilation and Linking

32-bit Compilation

nasm -f elf32 filename.asm
ld -m elf_i386 filename.o

64-bit Compilation

nasm -f elf64 filename.asm
ld filename.o

Common Error Handling

  • Always check register sizes
  • Be mindful of memory alignment
  • Use proper syscall conventions
  • Handle potential overflow scenarios