Q2Microprocessor and Interfaces
Question
Describe the memory segmentation in 8086.
Answer
Memory segmentation in the 8086 divides the 1MB physical address space into logical 64KB segments addressed through four dedicated segment registers, letting a 16-bit internal architecture reach a 20-bit address space.
The 8086 has internal registers that are only 16 bits wide, which on their own can address a maximum of 64 KB. Yet the processor's address bus is 20 bits wide, giving it access to a full 1 MB of physical memory. Segmentation is the mechanism that bridges this gap: the 1 MB memory space is conceptually divided into overlapping 64 KB logical blocks called segments, and every physical address is expressed as a combination of a segment base and an offset within that segment.
The Four Segment Registers
- Code Segment (CS): Holds the base address of the segment containing the currently executing program's machine code. Used together with the Instruction Pointer (IP).
- Data Segment (DS): Holds the base address of the segment containing the program's variables and data, used with general offset registers such as SI, DI, or a displacement.
- Stack Segment (SS): Holds the base address of the segment used for the LIFO stack, used together with the Stack Pointer (SP) and Base Pointer (BP).
- Extra Segment (ES): An auxiliary data segment, primarily used as the destination segment during string manipulation instructions.
Physical Address Computation
To generate the 20-bit physical address, the Bus Interface Unit takes the 16-bit value in the relevant segment register, shifts it left by 4 bits (equivalent to multiplying by 16, or appending a hex 0), and adds the 16-bit offset.
For example, if CS = 2000H and IP = 0050H, the physical address fetched is (2000H x 10H) + 0050H = 20000H + 0050H = 20050H. Because a given physical address can be reached by many different segment:offset combinations, segments can overlap, which gives the programmer flexibility in organizing memory. Segmentation also allows programs to be relocated simply by changing the segment register value, and it keeps code, data, and stack logically separated, reducing the chance that a stack overflow silently corrupts program instructions or data.