Segmentation Memory Model

Nolawz Nolawz #osdev #assembly #low-level

An introduction to the segmentation memory model used in x86 protected mode, how it works, and how to set it up.

Intro

Memory access in real mode is done using something called segmentation.

The original 8086 processor used mostly 16-bit registers. But a 16-bit register can only represent 216=65536Β bytes=64Β KB. So theoretically, the CPU should only have been only be able to access 64 KB of memory instead of 1 MB.

As a solution to this, segmentation was introduced. Instead of using a single address, memory locations were accessed using segment:offset

8086 Segment Registers

The 8086 introduced several special segment registers:

  1. CS (Code Segment)
  2. SS (Stack Segment)
  3. DS (Data Segment)
  4. ES (Extra Segment)

Calculating Physical Addresses

In real mode, the CPU converts a segment:offset pair into an actual physical memory address using this formula: πš™πš‘πš’πšœπš’πšŒπšŠπš•Β πšŠπšπšπš›πšŽπšœπšœ=(πšœπšŽπšπš–πšŽπš—πšΓ—16)+𝚘𝚏𝚏𝚜𝚎𝚝

For example, suppose:

Code Segment = 0x7c0
Offset = 0x0

Then the physical address becomes: πŸΆπš‘πŸ½πšŒπŸΆΓ—πŸΆπš‘πŸ·πŸΆ+𝟢𝚑𝟢=𝟢𝚑𝟽𝚌𝟢𝟢