Unit 3 · Module 4

Segmentation

Memory divided the way the programmer thinks: code, data, stack and heap become variable-size segments, each with its own base and limit for relocation and protection.

Segments, segment table and translation
Segment — A logical unit of a program — code, data, stack, heap — of any size.
Segment table — One entry per segment holding the base (start address in RAM) and the limit (length of the segment).
Base — Physical starting address where the segment was loaded.
Limit — Length of the segment; every offset must be smaller than it.
Address translation — If offset < limit then physical = base + offset, else trap: addressing error.
Protection — The limit check prevents a process reading past the end of one of its own segments or into another process.
Logical Address
+-----------+--------+
| Segment # | Offset |
+-----------+--------+
       |
  Segment Table  --> base, limit
       |
  offset < limit ? --no--> Addressing Error
       | yes
   Base + Offset
       |
 Physical Address

Real-world analogy: Library sections

A library is split into Fiction, Reference and Magazines (segments) of different sizes. "Reference, shelf 12" works only if Reference actually has 12 shelves — otherwise the librarian stops you. That is the limit check.
Experiment: Segmentation address translation & protection check
Segment #NameBaseLimit
0Code
1Stack
2Data
3Heap

Check: offset 300 < limit 1100 ✓

Physical address = base 4300 + offset 300 = 4600

The access falls inside segment 2 (Data) and is allowed.

Segment 2 inside physical memory

0 … 8000 bytes of physical memory

Paging vs Segmentation
FeaturePagingSegmentation
UnitFixed-size pageVariable-size segment
AddressPage number + offsetSegment number + offset
TablePage table (frame numbers)Segment table (base + limit)
FragmentationInternal (last page)External (holes between segments)
Protection checkValid/invalid bit per pageoffset < limit per segment
Programmer visibilityUsually invisible — purely physical divisionLogical: code, data, stack are real units

Paging — equal slices

Every block is the same size, so any page fits any frame.

Segmentation — logical slices

Blocks match program structure, so their sizes differ — holes appear.

Practice problem

Segment table: segment 0 → base 1400, limit 1000; segment 2 → base 4300, limit 1100. Translate (0, 1200) and (2, 500).

Quick quiz

1. Segmentation suffers mainly from which fragmentation?

2. What does the limit register in a segment entry do?

Next: when memory is full, which page do we throw out?

Unit 3
Page replacement lab