Assembly logo

Assembly Beginner

x86_64 Architecture — low-overhead hardware manipulation, memory registers, stack layout orientation, and bare-metal execution control.

1 Program Structure & Segments

An Assembly program (NASM syntax) maps directly into system memory blocks. Executable binaries separate binary parameters into structural subdivisions called **Segments** or **Sections**.

asm · program.asm
section .data
    ; Allocation block for initialized static variables
    msg db "Core online", 10

section .bss
    ; Allocation block for uninitialized runtime buffers
    buffer resb 64

section .text
    global _start
_start:
    ; Execution entry point (Required by the kernel linker)

2 x86_64 Processor Registers

Registers are high-speed storage locations built directly inside the CPU core. Modern x86_64 chips host 64-bit general-purpose cells that remain backward-compatible with 32-bit, 16-bit, and 8-bit historical subdivisions.

RAX / EAX / AX Primary accumulator used for math and system function return codes 64-bit / 32-bit / 16-bit
R_X (RBX, RCX, RDX) General usage nodes, loop counting parameters, and I/O tracking data Base, Counter, Data
RSI / RDI Source and Destination Index elements reserved for string arrays Memory address pointers
R8 - R15 Modern expansion registers added to the 64-bit architecture specification General purpose storage
RSP Stack Pointer tracking the current boundary address of execution metrics Crucial call stack monitor
RIP Instruction Pointer holds the memory coordinates of the upcoming command Program counter tracker

3 Data Movement Operations

The mov instruction transfers data payloads between registers, or between registers and system RAM cells. Note that Intel syntax maps arguments as mov destination, source.

asm · movement.asm
mov rax, 100        ; Load an immediate numeric constant into RAX
mov rbx, rax        ; Copy raw bit contents directly from RAX into RBX

mov rcx, [msg]      ; Square brackets dereference addresses: reads memory contents
lea rsi, [msg]      ; Load Effective Address: copies the structural pointer tracking link

4 Basic Arithmetic Instructions

Perform native mathematical mutations using dedicated CPU logic block circuits. These operations alter register allocations in place.

asm · arithmetic.asm
add rax, 50         ; RAX = RAX + 50
sub rbx, rcx        ; RBX = RBX - RCX

inc rdx             ; Increment RDX value by 1 (Fast loop step)
dec rsi             ; Decrement RSI value by 1

xor rax, rax        ; Bitwise Exclusive OR: efficiently clears register state to 0

5 Status Flags & Conditional Jumps

Branching logic evaluates data by comparing operands using the cmp instruction. This operation subtracts values without saving the result, modifying internal CPU **Status Flags** instead.

asm · control.asm
mov rax, 42
cmp rax, 50         ; Compare current RAX state against literal scalar threshold

jl  .less_target    ; Jump to local label if RAX is Less than 50
jmp .always_target  ; Unconditional jump: instantly re-routes instruction pipeline

.less_target:
    inc rbx

.always_target:

6 The Stack Pointer (Push & Pop)

The **Stack** is a Last-In, First-Out (LIFO) memory region managed directly by the CPU. The rsp register tracks the top of the stack, growing downward toward lower memory addresses.

asm · stack.asm
push rax            ; Decrements RSP by 8 bytes and copies RAX onto the Stack grid
push rbx            ; Preserves RBX state before calling destructive subroutines

pop  rbx            ; Restores value from the stack, saving it back into RBX
pop  rax            ; Restores RAX state, incrementing RSP by 8 bytes automatically
**Stack balance is critical!** Every single push instruction must map to an associated pop before exiting a function context. Imbalances cause subroutines to read corrupt addresses, resulting in instant Segmentation Fault crashes.

7 Functions & Subroutines

Subroutines organize code blocks into reusable tasks. The call command pushes the current return address onto the stack and jumps to the target label. The ret statement pops that address to resume execution.

asm · functions.asm
_start:
    mov rax, 5
    call double_value   ; Divert instruction track into subroutine block context
    ; RAX holds 10 here

double_value:
    add rax, rax        ; Double register accumulator content parameters
    ret                 ; Pops the saved IP address off the stack, jumping back

8 Linux System Calls (Syscalls)

Assembly can transition from user space to kernel space via the syscall instruction. This triggers an exception that passes execution control directly to the OS kernel to handle tasks like writing files or printing text to the console.

asm · syscall.asm
; Triggering a graceful sys_exit sequence via the Linux kernel
mov rax, 60          ; Syscall number 60 corresponds to sys_exit on x86_64 Linux
mov rdi, 0           ; Load standard error code 0 (Success exit parameter flag)
syscall              ; Kernel executes system interruption process routine
With bare-metal machine language concepts clear, you can step up to analyzing binary structures using tools like **GDB** or optimizing critical compilation loops.