TExcelTAD
Home › x86-64 Assembly › CPU & Memory Model

CPU & Memory Model

Before writing real programs you need a mental picture of the machine. For an assembly programmer, a computer is two things: a CPU that executes instructions one after another, and a big array of bytes called memory. Everything else — variables, strings, objects, the stack — is a convention about how to use those bytes.

The fetch–decode–execute cycle

The CPU keeps the address of the next instruction in a special register called rip (the instruction pointer). Conceptually it repeats three steps forever:

  1. Fetch the instruction bytes at address rip.
  2. Decode them: which operation, which registers, which memory address?
  3. Execute the operation and advance rip to the following instruction (or, for a jump, to the jump target).

Real processors overlap dozens of instructions in a pipeline and even execute them out of order, but they guarantee that the result looks exactly as if the steps happened one at a time. That is the model you program against.

Registers vs memory

Storage Size Speed Named by
Registers 16 general-purpose × 8 bytes (plus vector registers) instant (part of the CPU) name: rax, rbx, …
Cache (L1/L2/L3) KB to MB a few to ~40 cycles invisible to you, holds recently used memory
Main memory (RAM) GB ~100+ cycles numeric address

Most instructions work on registers. Values are loaded from memory into registers, processed, and stored back. x86-64 also lets many instructions use one memory operand directly (add rax, [count]), but never two memory operands in the same instruction.

Memory is an array of bytes

Every byte in memory has an address, a 64-bit number. A label in your program, like count:, is simply a name for an address. Square brackets mean "the memory at this address":

nasm
mov rax, count      ; rax = the ADDRESS of count
mov rax, [count]    ; rax = the 8 bytes stored AT that address

Larger values occupy several consecutive bytes. The sizes have traditional names on x86:

Name Bytes Bits NASM data directive NASM size keyword
byte 1 8 db byte
word 2 16 dw word
doubleword 4 32 dd dword
quadword 8 64 dq qword

Little-endian byte order

When a multi-byte number is stored, x86 puts the least significant byte first (at the lowest address). This is called little-endian. The 32-bit value 0x11223344 is stored as the bytes 44 33 22 11.

nasm · try it yourself
section .data
value:  dd 0x11223344       ; one 4-byte number

section .text
global _start
_start:
    movzx edi, byte [value]     ; the byte at the lowest address
    call print_uint             ; 0x44 = 68
    movzx edi, byte [value + 3] ; the byte at the highest address
    call print_uint             ; 0x11 = 17

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
68
17

movzx ("move with zero-extension") loads a small value into a bigger register and fills the remaining bits with zeros. You will meet it again in the mov lesson.

The same bytes, different meanings

Memory has no types. The same eight bytes can be read as one quadword, two doublewords, four words or eight bytes — the instruction decides how many bytes to read and how to interpret them:

nasm · try it yourself
section .data
bytes:  db 1, 0, 2, 0, 3, 0, 4, 0     ; eight individual bytes

section .text
global _start
_start:
    movzx edi, byte [bytes]     ; 1 byte
    call print_uint
    movzx edi, word [bytes]     ; 2 bytes: 01 00 -> 1
    call print_uint
    mov edi, [bytes + 2]        ; 4 bytes: 02 00 03 00 -> 0x00030002
    call print_uint
    mov rdi, [bytes]            ; 8 bytes: 0x0004000300020001
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
1
1
196610
1125912791875585

0x00030002 is 3 × 65536 + 2 = 196610. This flexibility is powerful and dangerous: nothing stops you from reading a string as a number by accident.

The layout of a running program

When Linux starts your program it gives it its own virtual address space and maps several regions into it:

text
high addresses   +---------------------+
                 |  stack              |  grows DOWN (push, call)
                 |         ...         |
                 |  heap (if any)      |  grows UP (malloc, brk)
                 +---------------------+
                 |  .bss               |  zero-filled variables (resb)
                 |  .data              |  initialized variables (db, dq)
                 |  .rodata            |  read-only constants
                 |  .text              |  your instructions
low addresses    +---------------------+

The sections you declare with section .text, section .data and section .bss end up in these regions. Each region has permissions: .text can be executed but not written, .data can be written but not executed. Breaking the rules, as in writing into your own code, ends in a crash:

nasm · try it yourself
section .text
global _start
_start:
    mov byte [_start], 0x90     ; try to overwrite our own first instruction
    mov eax, 60
    xor edi, edi
    syscall
Program terminated with signal SIGSEGV (11)

Note: Addresses in the stack region change every time a program runs (address space layout randomization, ASLR), which makes attacks harder. That is why no example in this course prints a stack address.

Test yourself

How is the 16-bit value 0xABCD stored in memory on x86?

  1. AB CD
  2. CD AB
  3. DC BA
  4. BA DC

What is the difference between mov rax, count and mov rax, [count]?

  1. None
  2. The first loads the address of count, the second loads the value stored there
  3. The first loads 1 byte, the second 8 bytes
  4. The second is invalid

Which register holds the address of the next instruction?

  1. rsp
  2. rax
  3. rip
  4. rbp