1.1.6 Segment Arithmetic

Manipulating segment and offset addresses directly in real-mode programming is called “segment arithmetic.” Programs that perform segment arithmetic are not portable to protected-mode operating systems, where addresses do not correspond to a known segment and offset.

Summary: The segment selects a region of memory; the offset selects the byte within that region.

To perform segment arithmetic successfully, it helps to understand how the processor combines a 16-bit segment and a 16-bit offset to form a 20-bit linear address. In effect, the segment selects a 64K region of memory, and the offset selects the byte within that region. Here's how it works:

1.The processor shifts the segment address to the left by four binary places, producing a 20-bit address ending in four zeros. This operation has the effect of multiplying the segment address by 16.

2.The processor adds this 20-bit segment address to the 16-bit offset address. The offset address is not shifted.

3.The processor uses the resulting 20-bit address, often called the “physical address,” to access an actual location in the one-megabyte address space.

Figure 1.2 illustrates this process.

A 20-bit physical address may actually be specified by 4,096 equivalent segment:offset addresses. For example, the 20-bit physical address 0F800 is equivalent to 0000:F800, 0F00:0800, or 0F80:0000.

You may need to convert two segmented addresses with different segments to segmented addresses with the same segment to write TSRs (see Chapter 19), to write code to handle huge arrays, or to determine the size of an area of memory.