Real Mode Flat Model

In real mode, if you recall, the CPU can see only one megabyte (1,048,576) of memory. You can access every last one of those million-odd bytes by using the segment:offset register trick shown earlier to form a 20-bit address out of two 16-bit addresses contained in two registers. Or, you can be content with 64K of memory, and not fool with segments at all.

In the real mode flat model, your program and all the data it works on must exist within a single 64K block of memory. Sixty-four kilobytes! Pfeh! What could you possibly accomplish in only 64K bytes? Well, the first version of WordStar for the IBM PC fit in 64K. So did the first three major releases of Turbo Pascal—in fact, the Turbo Pascal program itself occupied a lot less than 64K because it compiled its programs into memory. The whole Turbo Pascal package—compiler, text editor, and some odd tools—came to just over 39K. Thirty-nine kilobytes! You can't even write a letter to your mother (using Microsoft Word) in that little space these days!

True, true. But that's mostly because we don't have to. Memory has become very cheap, and our machines now contain what by historical standards is a staggering amount of it. We've gotten lazy and hoggish and wasteful, simply because we can get away with it.

Spectacular things once happened in 64K, and while you may never be called upon to limit yourself to real mode flat model, the discipline that all those now gray-haired programmers developed for it is very useful. More to the point, real mode flat model is the ''little brother'' of protected mode flat model, which is the code model you will use when programming under Linux. If you learn the ways of real mode flat model, protected mode flat model will be a snap. (Any trouble you'll have won't be with assembly code or memory models, but with the byzantine requirements of Linux and its canonical code libraries.)

Real mode flat model is shown diagrammatically in Figure 4-8. There's not much to it. The segment registers are all set to point to the beginning of the 64K block of memory you can work with. (The operating system sets them when it loads and runs your program.) They all point to that same place and never change as long as your program is running. That being the case, you can simply forget about them. Poof! No segment registers, no fooling with segments, and none of the ugly complication that comes with them.

Because a 16-bit register such as BX can hold any value from 0 to 65,535, it can pinpoint any single byte within the full 64K your program has to work with. Addressing memory can thus be done without the explicit use of the segment registers. The segment registers are still functioning, of course, from the CPU's point of view. They don't disappear and are still there, but the operating system sets them to values of its own choosing when it launches your program, and those values will be good as long as your program runs. You don't have to access the segment registers in any way to write your program.

16-Bit Offset Addresses

0FFFFH

The Stack

SP (Stack Pointer) points to the memory location where the next "push" will occur. The Stack is a temporary LIFO (Last In First Out) buffer used by many x86 machine instructions.

Unused Memory Space

0100H

0000H

Your Program Data

Your Program Code

Program Segment Prefix (PSP)

IP points to the memory location of the next machine instruction to be executed by the CPU.

Segment registers are set by the operating system, and you don't fool with them!

The PSP is a holdover from ancient CP/M-80!

Segment registers are set by the operating system, and you don't fool with them!

CS

DS

SS

ES

Figure 4-8: Real mode flat model

Most of the general-purpose registers may contain addresses of locations in memory. You use them in conjunction with machine instructions to bring data in from memory and write it back out again.

At the top of the single segment that your program exists within, you'll see a small region called the stack. The stack is a LIFO (last in, first out) storage location with some very special uses. I will explain what the stack is and how it works in considerable detail later.

Was this article helpful?

0 0

Post a comment