Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 40 ยท Data, Numbers and Registers

Memory, Addresses and Endianness

How assembly sees memory: byte addresses, loads and stores, little-endian byte order, alignment, and the virtual address space, with a runnable toy memory.

Registers hold a few dozen values. Everything else a program touches (arrays, strings, the stack, the code itself) lives in memory, and assembly programs reach it one address at a time. Memory looks very simple from the programmer's side: a gigantic row of numbered boxes, each holding one byte. Yet almost every confusing moment for beginners (why is my number backwards in the debugger? why did that load crash?) comes from details of this model.

In this lesson you build a mental model of memory and then a small one in JavaScript that you can poke at. You will learn how addresses work, what loads and stores are, how multi-byte numbers are laid out in little-endian order, why alignment matters, and roughly how a real program's address space is arranged.

Memory is a row of bytes

Memory is byte-addressable: every address names exactly one byte, and consecutive addresses are consecutive bytes. An address is just a number. On x86-64 a pointer is 64 bits wide, so addresses are 64-bit numbers (current CPUs implement 48 or 57 of those bits, see below).

flowchart LR
  subgraph Memory
    direction LR
    A0["0x1000: 0x2A"] --- A1["0x1001: 0x00"] --- A2["0x1002: 0xFF"] --- A3["0x1003: 0x10"]
  end
  R["rbx = 0x1000"] -. points to .-> A0

A register holding an address "points" at a byte; neighbouring bytes have neighbouring addresses.

Nothing in memory says what a byte means. The same four bytes can be an integer, a float, four letters or machine code. The instruction decides how many bytes to read and how to interpret them. That is why assembly operands carry sizes: mov eax, [rbx] reads 4 bytes, mov al, [rbx] reads 1, and mov rax, [rbx] reads 8.

Loads and stores

Moving data between registers and memory comes in two directions:

  • A load copies bytes from memory into a register: mov eax, [rbx].
  • A store copies bytes from a register (or an immediate) into memory: mov [rbx], eax.

The square brackets mean "the memory at this address", not the address itself. mov rax, rbx copies a register; mov rax, [rbx] follows the pointer. Forgetting the brackets (or adding them by mistake) is one of the most common beginner errors.

On x86-64 most instructions can use a memory operand directly (add eax, [rbx]), but never two memory operands in one instruction. To copy from memory to memory you go through a register. ARM is stricter: only dedicated load and store instructions touch memory (see ARM Load/Store).

Little-endian layout

A 32-bit number is four bytes, so which byte goes at the lowest address? There are two conventions, named endianness:

  • Little-endian: the least significant byte comes first (lowest address).
  • Big-endian: the most significant byte comes first.

x86-64 is little-endian, and so are ARM64 Linux, Android and Apple systems in practice (ARM hardware can be configured either way, but nobody ships big-endian ARM64 desktops). Network protocols traditionally use big-endian, which is why byte-swapping functions like htonl exist.

The 32-bit value 0x12345678 stored at address 0x1000 looks like this:

Address 0x1000 0x1001 0x1002 0x1003
Little-endian 78 56 34 12
Big-endian 12 34 56 78

So if you dump memory in a debugger and see 78 56 34 12, that is the number 0x12345678, and the address of that number is the address of its lowest byte. Little-endian has a handy side effect: reading the first byte of a 32-bit value at [rbx] with mov al, [rbx] gives you the number's low 8 bits, so casting down to a narrower type needs no arithmetic.

Our first toy memory makes this visible. It uses a plain Uint8Array and writes multi-byte values with shifts, the way the hardware effectively does.

JS A toy byte memory, written little-endian

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the overlapping load: reading 2 bytes at address 1 gives 0x3456, because bytes 1 and 2 are 56 and 34. Memory has no memory of how it was written. Whatever combination of address and size you ask for, you get those bytes.

Alignment

A value is aligned when its address is a multiple of its size: a 4-byte int at an address divisible by 4, an 8-byte pointer at an address divisible by 8. Why care?

Memory hardware is organised in chunks (cache lines are typically 64 bytes, and buses move several bytes at a time). An aligned access sits inside one chunk. A misaligned 8-byte access can straddle two chunks, and the CPU has to do extra work. The consequences differ by processor:

  • On x86-64, ordinary loads and stores work at any address, just slower if they cross a cache line. A few instructions, such as aligned SSE moves (movaps), fault if the address is not a multiple of 16.
  • On many other architectures (older ARM, MIPS, SPARC) a misaligned access can trap. Modern ARM64 permits it for ordinary loads and stores in most configurations, but code that depends on it is still not portable.

Compilers therefore align data automatically. C structures get padding to keep their fields aligned (Structs, Padding and Alignment shows how). The .align or align directive does the same in hand-written assembly.

The next demo checks alignment and counts how many 8-byte chunks each access touches.

JS Alignment checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

You can see that a 4-byte load at address 6 reaches into two chunks (bytes 6 to 9), while the same load at 4 or 8 stays in one. An 8-byte load at address 4 straddles two chunks too.

The virtual address space

When your program runs, the addresses it uses are virtual. The operating system and the CPU's memory-management unit translate each virtual address to a physical location in RAM, in units called pages (typically 4 KiB). Every process gets its own private address space, so address 0x1000 in one program is unrelated to 0x1000 in another, and one process cannot read another's bytes by accident.

On x86-64 Linux, current CPUs use 48-bit virtual addresses (57 with five-level paging), which are split into a lower half for user programs and an upper half for the kernel. A typical user layout, from low to high addresses:

flowchart TB
  K["kernel space (upper half, not accessible)"]
  S["stack (grows down)"]
  M["memory-mapped files and shared libraries"]
  H["heap (grows up)"]
  D[".data and .bss: global variables"]
  T[".text: machine code (read and execute)"]
  K --- S --- M --- H --- D --- T

Rough layout of a user process, with the highest addresses at the top; exact positions are randomized by the OS.

Pages carry permissions: code is readable and executable but not writable, data is readable and writable but not executable (this protection is called NX, and is part of security). Touching an address that is not mapped, or writing to a read-only page, raises a fault, which Linux reports as a segmentation fault. A null pointer is address 0, which is deliberately never mapped, so dereferencing it crashes immediately instead of corrupting data.

Putting it together: a tiny load/store machine

The last demo ties the ideas together by running three instructions against the toy memory: a load, an add, and a store, with a bounds check that mimics a fault.

JS Load, add, store (with a fault)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The final result 1234 = 0x04D2 sits at address 8 as bytes d2 04 00 00 (decimal 210 4 0 0), the little-endian layout from earlier.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. The 32-bit value 0xAABBCCDD is stored at address 0x2000 on a little-endian machine. What byte is at address 0x2000?

    Little-endian puts the least significant byte first, so 0xDD is at the lowest address, followed by CC, BB and AA.
  2. 2. What is the difference between mov rax, rbx and mov rax, [rbx]?

    Brackets mean 'the memory at this address'. Without them the register value itself is used.
  3. 3. Which pair is aligned for the access size?

    An address is aligned when it is a multiple of the access size. 0x1008 is divisible by 8; 0x1006 is not divisible by 4, 0x1004 is not divisible by 8, and 0x100B is odd.
  4. 4. Why does dereferencing a null pointer usually crash right away?

    Virtual page 0 is deliberately left unmapped. Any access to it raises a page fault that the OS reports as a segmentation fault.

Summary

  • Memory is a row of numbered bytes; an address names one byte and the instruction decides how many bytes to read or write.
  • A load copies memory to a register, a store copies a register to memory; brackets mean "memory at this address".
  • x86-64 is little-endian: the least significant byte of a multi-byte value is at the lowest address.
  • Aligned data (address a multiple of its size) is fastest and sometimes required; x86-64 usually tolerates misalignment, aligned SSE instructions do not.
  • Programs use virtual addresses with per-page permissions; unmapped or forbidden accesses fault.

Next: MOV, LEA and Moving Data, where you will meet the most common instruction in x86-64.