Both real mode and protected mode involve referencing memory through a "logical address" which contains a segment and an offset. The offset can occur in a variety of forms such as [ax], [1234h], or [bx+di]. The segment is always stored in special segment registers which can be cs,ds,es,fs,gs, or ss.
Although assembly instructions take logical address, each byte in memory is actually identified by a unique physical address. A phyisical address consists of a single 32-bit number rather segment:offset logical address combination. Because of this, the processor must convert logical addresses referred to by assembly instructions into physical addresses that will actually be accessed.
Logical addresses do not have a one-to-one correspondence with physical addresses. In real mode, the processor uses 16-bit segments and 16-bit offsets, which can collectively represent about 4 billion of unique logical addresses. In real mode, however, only one MB of memory is accessible (an explanation will follow). Since there are many more logical addresses than physical addresses, while each logical address can only reference one physical address, each physical address can be referenced my a multitude of distinct logical addresses.
In real mode, logical addresses are coverted to physical addresses through the "shift-and-add" method, which means that the value of the segment is shifted to the left by four bits and is then added to the value of the offset to compute the actual physical addressed being referenced. For example, consider the following code (all numbers in the following discussion are assumed to be in hexidecimal):
mov ax,1234 mov ds,ax mov cl,byte ptr ds:[5678]
In the 'mov' instruction above, data is being copied from which memory location? From the "shift-and-add" method, the physical address is :
(1234<<4)+5678 =12340+5678 =179B8
Applying the shift-and-add method in reverse is more complicated since one has one equation and two unknowns to work with. We begin by taking the equation
P=S<<4+O (P=physical address, S=segment, O=offset) (1)
and solving it algebraically for the offset value in terms of the segment value:
P=s<<4+O O=P-S<<4 (2)
Or, we can solve for the segment value in terms of the offset value:
P=s<<4+O S<<4=P-O S=(P-O)>>4 (3)
Now, given the physical address, we can compute the offset for any segment and the segment for the offset. The only conditions are that both the offset and the segment must be positive and neither cannot exceed 0FFFFh. Here is a table illustrating the results for physical address 179B8:
segment offset 179b 8 179a 18 1799 28 1798 38 . . . . . . 1234 5678 . . . . . . 079D FFE8 079C FFF8
The main point of the above example is that there are literally thousands of logical addresses for almost every physical address. Failure to understand this concept can create all sorts of nasty bugs.
Now, that we have a basic understanding of our topic, let's try some problems
|
Back to table of contents |   | Chapter 1 | Chapter 2 | Chapter 3 |   | On to chapter 1 problems |