016K32K48K64KPHYSICAL MEMORY16 KB8 KB4 KB4 KB4 KB11 KB13 KB1a = mem[100]2b = mem[4200]3c = mem[15360]4d = mem[7168]MMUseg base size grows00 code 32K 2K up01 heap 34K 3K up11 stack 28K 2K downSEGOFFSET100328688 KB4 KB
Back: physical memory, 0 to 64 KB (blocks are used, flat gold plates are free holes)Front: the program's reads, the MMU's segment table and address register, new segmentsChips: a virtual address in, a physical address out; blocks: segments the kernel placesAt the back, physical memory from 0 to 64 KB, with the kernel, our program's three segments, two other programs and free holes. In front, the program's four reads, the MMU with its segment table and address register, and a tray of new segments. Each read is split into segment bits and an offset; the table row adds a base and checks a size, and the read lands inside its segment, or traps. Then freed segments become holes, best fit places a 4 KB segment, neighbouring holes merge, and an 8 KB segment fits. The segment table, the four addresses and their translations are the worked example from OSTEP chapter 16: a pretend 16 KB address space with 14-bit addresses in a 64 KB machine. Programs B and C and the two new segments are added for the free-space part.

Segments

  1. A program uses three pieces of memory: code, heap and stack, with an unused gap in between. Base and bounds moves the whole thing, gap and all. Segmentation places each piece on its own, so the gap costs nothing.

Translating addresses

Managing free space

What happens

;; step 1/11 · pieces
virtual address space (16 KB)      physical memory (64 KB)
  0K- 2K  code                      0K-16K  kernel
  2K- 4K  unused                   26K-28K  stack
  4K- 7K  heap                     32K-34K  code
  7K-14K  unused gap               34K-37K  heap
 14K-16K  stack

base and bounds: all 16 KB in one block, gaps included
segmentation:    only the 7 KB in use takes memory

The segment table, the four addresses and their translations are the worked example from OSTEP chapter 16: a pretend 16 KB address space with 14-bit addresses in a 64 KB machine. Programs B and C and the two new segments are added for the free-space part. Today x86-64 and RISC-V systems manage memory with paging, the next lesson, which uses fixed-size pages so free space is just a list of equal pieces. On x86 segmentation cannot be switched off, so systems such as Linux set every segment to cover all of memory. The name segmentation fault stayed.