09 Segments and Offsets

āĻāĻŦāĻžāϰ Segments and Offsets — āĻŦāĻ‡ā§Ÿā§‡āϰ āĻĒ⧃āĻˇā§āĻ āĻž ā§§ā§ŦāĨ¤ āĻāĻ–āĻžāύ⧇ āĻŦ⧁āĻāĻŦ: 16-bit register āĻĻāĻŋā§Ÿā§‡ 8086 āϕ⧀āĻ­āĻžāĻŦ⧇ 20-bit memory address āϤ⧈āϰāĻŋ āĻ•āϰ⧇āĨ¤

ā§§. āϕ⧇āύ segment āĻ“ offset āϞāĻžāϗ⧇?

8086-āĻāϰ address bus 20-bit, āϤāĻžāχ āĻāϟāĻŋ āĻŽā§‹āϟ:

2²⁰ bytes = 1 MB memory address āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āĨ¤

āĻ•āĻŋāĻ¨ā§āϤ⧁ āĻāĻ•āϟāĻŋ 16-bit register āĻĻāĻŋā§Ÿā§‡ āĻŽāĻžāĻ¤ā§āϰ 2šâļ = 65,536āϟāĻŋ āĻ…āĻŦāĻ¸ā§āĻĨāĻžāύ āĻŦā§‹āĻāĻžāύ⧋ āϝāĻžā§ŸāĨ¤ āϤāĻžāχ memory address āϤ⧈āϰāĻŋāϰ āϜāĻ¨ā§āϝ 8086 āĻĻ⧁āχāϟāĻŋ 16-bit value āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇:

Value āϕ⧀ āĻŦā§‹āĻāĻžā§Ÿ?
Segment value āϕ⧋āύ segment-āĻāϰ āĻļ⧁āϰ⧁ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻšāĻŦ⧇
Offset āϏ⧇āχ āĻļ⧁āϰ⧁ āĻĨ⧇āϕ⧇ āĻ•āϤ byte āĻĻā§‚āϰ⧇ āϝ⧇āϤ⧇ āĻšāĻŦ⧇

āĻāĻ•āϟāĻŋ segment āϏāĻ°ā§āĻŦā§‹āĻšā§āϚ 64 KBāĨ¤ āϤāĻžāϰ offset range: 0000H–FFFFHāĨ¤

⧍. Physical address āϕ⧀āĻ­āĻžāĻŦ⧇ āϤ⧈āϰāĻŋ āĻšā§Ÿ?

Physical address = (Segment value × 10H) + Offset

āĻāĻ–āĻžāύ⧇ 10H = decimal 16āĨ¤ Hexadecimal-āĻ 10H āĻĻāĻŋā§Ÿā§‡ āϗ⧁āĻŖ āĻ•āϰāϞ⧇ āĻĄāĻžāύ āĻĒāĻžāĻļ⧇ āĻāĻ•āϟāĻŋ 0 āĻŦāϏ⧇āĨ¤

āϧāϰ⧋:

  • Segment value = 2000H
  • Offset = 0030H

āϤāĻžāĻšāϞ⧇:

2000H × 10H = 20000H
20000H + 0030H = 20030H

āϏ⧁āϤāϰāĻžāĻ‚ 2000:0030 → physical address 20030HāĨ¤

āĻŽāύ⧇ āϰāĻžāĻ–ā§‹: segment value-āĻāϰ āĻļ⧇āώ⧇ āĻāĻ•āϟāĻŋ hex 0 āĻŦāϏāĻžāĻ“, āϤāĻžāϰāĻĒāϰ offset āϝ⧋āĻ— āĻ•āϰ⧋āĨ¤

Exam answer:

“The 8086 forms a 20-bit physical address by shifting a 16-bit segment value four bits to the left and adding a 16-bit offset.”

ā§Š. āϕ⧋āύ segment register āϕ⧀ āϰāĻžāϖ⧇?

BIU-āϤ⧇ āϚāĻžāϰāϟāĻŋ 16-bit segment register āĻĨāĻžāϕ⧇:

Register Full name Purpose
CS Code Segment Identifies the segment containing instructions
DS Data Segment Identifies the segment normally used for data
SS Stack Segment Identifies the stack segment
ES Extra Segment Identifies an additional data segment

āϗ⧁āϰ⧁āĻ¤ā§āĻŦāĻĒā§‚āĻ°ā§āĻŖ āĻĒāĻžāĻ°ā§āĻĨāĻ•ā§āϝ: CS-āĻāϰ āĻŽāĻ§ā§āϝ⧇ instructions āĻĨāĻžāϕ⧇ āύāĻž; CS-āĻāϰ value āĻĻāĻŋā§Ÿā§‡ instructions āϝ⧇āĻ–āĻžāύ⧇ āφāϛ⧇, āϏ⧇āχ segment-āĻāϰ āĻļ⧁āϰ⧁ āύāĻŋāĻ°ā§āϧāĻžāϰāĻŖ āĻ•āϰāĻž āĻšā§ŸāĨ¤

ā§Ē. IP-āĻāϰ āĻ•āĻžāϜ āϕ⧀?

IP = Instruction Pointer, āĻāϟāĻŋāĻ“ 16-bitāĨ¤ āĻāϟāĻŋ code segment-āĻāϰ āĻŽāĻ§ā§āϝ⧇ āĻĒāϰ⧇āϰ āϝ⧇ instruction fetch āĻšāĻŦ⧇ āϤāĻžāϰ offset āϰāĻžāϖ⧇āĨ¤

āϤāĻžāχ instruction fetch-āĻāϰ address āϤ⧈āϰāĻŋ āĻšā§Ÿ:

Physical address = (CS × 10H) + IP

āωāĻĻāĻžāĻšāϰāĻŖ:

CS = 1000H, IP = 0020H → address = 10020H

Exam answer:

“The Code Segment register holds the segment value for the code segment. The Instruction Pointer holds the offset of the next instruction to be fetched. Together, CS and IP determine its physical address.”

Quick check — āĻļ⧁āϧ⧁ value āĻĻāĻžāĻ“:

  1. Segment = 3000H, Offset = 0020H. Find the physical address.
  2. CS = 1200H, IP = 0010H. Find the instruction’s physical address.
  3. Is CS a 16-bit register or a 64 KB register?

āĻāĻŦāĻžāϰ Segmentation-āĻāϰ āĻŦāĻžāĻ•āĻŋ āϗ⧁āϰ⧁āĻ¤ā§āĻŦāĻĒā§‚āĻ°ā§āĻŖ āĻ…āĻ‚āĻļ āĻļ⧇āώ āĻ•āϰāĻŋāĨ¤

ā§§. āĻāĻ•āϟāĻŋ segment-āĻāϰ āĻĒā§āϰāĻĨāĻŽ āĻ“ āĻļ⧇āώ address

āφāϗ⧇āϰ āϏ⧂āĻ¤ā§āϰ:

Physical address = (Segment value × 10H) + Offset

āϧāϰ⧋, segment value 1000HāĨ¤ Offset-āĻāϰ āϏāĻ°ā§āĻŦāύāĻŋāĻŽā§āύ āĻŽāĻžāύ 0000H, āϏāĻ°ā§āĻŦā§‹āĻšā§āϚ FFFFHāĨ¤

Location Calculation Physical address
First 10000H + 0000H 10000H
Last 10000H + FFFFH 1FFFFH

āĻ…āĻ°ā§āĻĨāĻžā§Ž āĻĒā§‚āĻ°ā§āĻŖ 64 KB segment-āĻāϰ range: 10000H–1FFFFHāĨ¤

Exam answer:
“For a segment value of 1000H, the first physical address is 10000H and the last is 1FFFFH, corresponding to offsets 0000H and FFFFH.”

⧍. āĻ­āĻŋāĻ¨ā§āύ segment:offset āĻĻāĻŋā§Ÿā§‡ āĻāĻ•āχ address āĻšāϤ⧇ āĻĒāĻžāϰ⧇

āĻĻ⧇āĻ–ā§‹:

  • 1000:0020 → 10000H + 0020H = 10020H
  • 1002:0000 → 10020H + 0000H = 10020H

āĻĻ⧁āχāϟāĻŋ representation, āĻ•āĻŋāĻ¨ā§āϤ⧁ āĻāĻ•āχ physical memory locationāĨ¤ āϤāĻžāχ segments overlap āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āĨ¤ Memory āϏāĻŦāϏāĻŽā§Ÿ āĻĒāĻžāĻļāĻžāĻĒāĻžāĻļāĻŋ āφāϞāĻžāĻĻāĻž 64 KB āĻŦāĻžāĻ•ā§āϏ⧇ āĻ­āĻžāĻ— āĻ•āϰāĻž āĻĨāĻžāϕ⧇ āύāĻžāĨ¤

Exam answer:
“Segments may overlap, and different segment:offset combinations can refer to the same physical address.”

ā§Š. āϕ⧋āύ register pair āϕ⧋āĻĨāĻžā§Ÿ āĻ•āĻžāĻœā§‡ āϞāĻžāϗ⧇?

Register combination Main use
CS:IP Address of the next instruction to be fetched
SS:SP Address of the top of the stack
DS with an offset Normal data access
ES:DI Destination address in applicable string operations

āϏāϤāĻ°ā§āĻ•āϤāĻž: DI āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϞ⧇āχ āϏāĻŦāϏāĻŽā§Ÿ ES āύ⧟āĨ¤ String destination āĻšāĻŋāϏ⧇āĻŦ⧇ ES:DI āĻŽāύ⧇ āϰāĻžāĻ–ā§‹āĨ¤

ā§Ē. Segmentation-āĻāϰ āϏ⧁āĻŦāĻŋāϧāĻž

Advantage Exam wording
Code, data, stack āφāϞāĻžāĻĻāĻžāĻ­āĻžāĻŦ⧇ āϏāĻžāϜāĻžāύ⧋ Separate logical segments organize code, data, and stack.
16-bit register āĻĻāĻŋā§Ÿā§‡ āĻŦ⧜ memory access It allows 20-bit physical addresses to be generated using 16-bit segment and offset values.
Program āĻ…āĻ¨ā§āϝ āϜāĻžā§ŸāĻ—āĻžā§Ÿ āĻ¸ā§āĻĨāĻžāύāĻžāĻ¨ā§āϤāϰ āϏāĻšāϜ It supports relocation by allowing segment values to change while suitable offsets remain unchanged.
āĻāĻ•āĻžāϧāĻŋāĻ• program-āĻāϰ memory āϏāĻžāϜāĻžāύ⧋ It supports multiprogramming by allowing programs to use separate logical segments.

Relocation-āĻāϰ āωāĻĻāĻžāĻšāϰāĻŖ: āĻāĻ•āχ offset 0020H āϰ⧇āϖ⧇ segment 1000H → 2000H āĻ•āϰāϞ⧇ physical address 10020H → 20020H āĻšāĻŦ⧇āĨ¤ āĻ…āĻ°ā§āĻĨāĻžā§Ž āĻļ⧁āϰ⧁āϰ āϜāĻžā§ŸāĻ—āĻž āĻŦāĻĻāϞ⧇āϛ⧇, āϭ⧇āϤāϰ⧇āϰ relative position āĻāĻ•āχāĨ¤

Quick check — āϛ⧋āϟ āωāĻ¤ā§āϤāϰ:

  1. Segment = 2000H. What are its first and last physical addresses for a full 64 KB segment?
  2. Do 2000:0010 and 2001:0000 refer to the same physical address?
  3. Which pair identifies the top of the stack?