Skip to content

Latest commit

 

History

History
246 lines (192 loc) · 4.01 KB

gas.md

File metadata and controls

246 lines (192 loc) · 4.01 KB

GAS (GNU Assembler) Comprehensive Cheatsheet

Installation Instructions

Windows

# Using MSYS2
pacman -S mingw-w64-x86_64-gcc
# Or install via MinGW

Linux (Ubuntu/Debian)

sudo apt-get update
sudo apt-get install gcc-multilib binutils

macOS

# Using Homebrew
brew install gcc
brew install binutils

Essential GAS Assembly Directives and Instructions

Basic Syntax and Directives

  1. Basic Instruction Syntax
instruction source, destination
  1. Define Byte
.byte 42         # 8-bit value
.byte 'A'        # Character literal
  1. Define Word (16-bit)
.word 1000       # 16-bit integer
  1. Define Long (32-bit)
.long 65536      # 32-bit integer
  1. Define Quad (64-bit)
.quad 1000000    # 64-bit integer
  1. Reserving Memory Space
.comm buffer, 100  # Reserve 100 bytes of uninitialized memory
  1. Global Symbol Declaration
.globl main      # Make symbol visible globally
  1. External Symbol Import
.extern printf   # Import external function

Register Operations

  1. 64-bit Register Move
movq $42, %rax   # Move immediate to 64-bit register
  1. 32-bit Register Move
movl $100, %eax  # Move immediate to 32-bit register
  1. Register to Register Move
movq %rbx, %rax  # Copy value from rbx to rax
  1. Memory to Register Move
movq (address), %rax  # Move from memory to register

Arithmetic Operations

  1. Addition
addq $10, %rax   # Add 10 to rax
  1. Subtraction
subq $5, %rbx    # Subtract 5 from rbx
  1. Multiplication
imulq %rcx       # Multiply rax by rcx
  1. Division
idivq %rdx       # Divide rax by rdx

Comparison and Branching

  1. Compare Instructions
cmpq %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
pushq %rax       # Push rax onto stack
  1. Pop from Stack
popq %rbx        # Pop top of stack to rbx

System Calls (Linux x86_64)

  1. Exit System Call
movq $60, %rax   # Exit syscall number
movq $0, %rdi    # Exit status
syscall          # Invoke syscall
  1. Write System Call
movq $1, %rax    # Write syscall
movq $1, %rdi    # Stdout
movq $message, %rsi  # Buffer
movq $14, %rdx   # Length
syscall

Macro Definitions

  1. Simple Macro
.macro print_msg
    movq $1, %rax
    movq $1, %rdi
    movq $message, %rsi
    movq $14, %rdx
    syscall
.endm

Conditional Assembly

  1. Preprocessor Conditionals
#ifdef DEBUG
    # Debug-specific code
#endif

Advanced Memory Operations

  1. Indirect Addressing
movq (%rax), %rbx  # Move value pointed by rax to rbx
  1. Base + Displacement Addressing
movq 8(%rsp), %rax  # Move value 8 bytes above stack pointer

Floating Point Operations

  1. SSE Floating Point Move
movsd x(%rip), %xmm0  # Move double precision float

String Operations

  1. String Copy
rep movsb        # Repeat move string byte

Bit Manipulation

  1. Shift Left
shlq $2, %rax    # Shift left by 2 bits
  1. Shift Right
shrq $1, %rbx    # Shift right by 1 bit

Compilation and Linking

32-bit Compilation

as -o output.o input.s
ld -o program output.o

64-bit Compilation

gcc -c input.s -o output.o
gcc output.o -o program

Best Practices

  • Use AT&T syntax consistently
  • Be mindful of register sizes
  • Comment your code thoroughly
  • Handle potential overflow scenarios
  • Use appropriate addressing modes

Common Debugging Tips

  • Use -g flag for debugging symbols
  • Leverage gdb for step-by-step execution
  • Check register states during debugging
  • Understand memory layout and alignment