CIS-261 Home http://www.c-jump.com/bcc/
Real mode flat model means
strictly converting one address value into a physically meaningful location in the RAM.
Real mode segmented model means
strictly converting two address values into a physically meaningful memory location.
gives access to one megabyte (1,048,576 bytes) of directly addressable memory, known as real mode memory.
|
|
Segmented organization
16-bit wide segments
Two components
Base (16 bits)
Offset (16 bits)
Two-component specification is called logical address, also called effective address.
Logical address translates to a 20-bit physical address.
|
|
Segmentation often caused grief for programmers who tried to access large data structures:
Since an offset cannot exceed 16 bits, you cannot increment beyond 64k.
Instead, program must watch out for a 64k boundary and then play games with the segment register.
This nightmare was originally created to support CP/M-80 programs ported from 8080 chip to 8086.
Successful short-term thinking;
Catastrophically bad long-term thinking that resulted in never-ending Windows 9x problems!
Address space in real mode segmented model runs from
00000h to 0FFFFFh,
within one megabyte of memory.
For compatibility reasons, Pentium CPU is capable of switching itself into real mode segmented model, is effectively becoming a good old 8086 chip!
Size | Decimal | Hex |
---|---|---|
Byte | 1 | 01H |
Word | 2 | 02H |
Double word | 4 | 04H |
Quad word | 8 | 08H |
Ten byte | 10 | 0AH |
Paragraph (*) | 16 | 10H |
Page, or page frame, (almost never used) | 256 | 100H |
Segment | 65,536 | 10000H |
______________
(*) Paragraphs are almost never used, except in connection with the places where segments may begin.
Any memory address evenly divisible by 16 is called a paragraph boundary.
The first paragraph boundary is address 0.
The second is address 10H (10H is equal to decimal 16.)
The third address is 20H, and so on.
Any paragraph boundary may be considered the start of a segment.
There are 64K different paragraph boundaries where a segment may begin.
Each paragraph boundary has a number.
The numbers range from 0 to 64K minus one (decimal 65,535 or hex 0FFFFh.)
Any segment may begin at any paragraph boundary.
The number of the paragraph boundary is called the segment address.
Assembly language program can have up to four or five segments.
Segment address is 16 bytes in size.
|
|
|
|
The 8088, 8086, and 80286 CPUs have four segment registers to hold segment addresses.
The 386 and later CPUs have two more, also available in real mode.
Note: the 386 and later Intel x86 CPUs still use 16-bit size segment registers.
Each segment register is 16-bit in size.
No matter how location in memory is accessed, the segment address of that location must be present in one of the six segment registers:
CS, DS, SS, ES, FS and GS.
All x86 segment registers are 16 bits in size, irrespective of the CPU:
CS, code segment. Machine instructions exist at some offset into a code segment. The segment address of the code segment of the currently executing instruction is contained in CS.
DS, data segment. Variables and other data exist at some offset into a data segment. There may be many data segments, but the CPU may only use one at a time, by placing the segment address of that segment in register DS.
SS, stack segment. The stack is a very important component of the CPU used for temporary storage of data and addresses. Therefore, the stack has a segment address, which is contained in register SS.
ES, extra segment. The extra segment is exactly that: a spare segment that may be used for specifying a location in memory.
|
|
|
|
Pentium can turn off segmentation.
Flat model
Consists of one segment of 4GB
Used by linux
Multisegment model
Up to six active segments
Can have more than six segments (all segment descriptors are in the descriptor table.)
A segment becomes active by loading its descriptor into one of the segment registers.
CPU can see only 1 megabyte (1,048,576) of memory.
segment:offset pairs form a 20-bit addresses out of two 16-bit addresses.
In real mode flat model a program and all its data must exist within a single 64K block of memory.
The real mode flat model is similar to protected mode flat model, the code model used on Linux and Windows XP/Vista.
|
|
|
|
|
|
|
|
On 32-bit processors, Windows and Linux use the so-called protected mode flat memory model.
Under flat memory model,
entire address space is described by a 32-bit segment, which provides 232 = 4 gigabytes of address space.
program can (in theory) access up to 4 gigabytes of virtual or physical memory.
In protected mode,
segment registers contain selector values rather than actual physical segment addresses.
Selector values cannot be calculated by the program; they must be obtained by calling the operating system.
Programs that update segment values or attempt to address memory directly do not work in protected mode.
32-bit Protected Mode supports much larger data structures than Real mode.
Because code, data, and stack reside in the same segment, each segment register can hold the same value that never needs to change.
Rather than using a formula (such as CS:IP) to determine the physical address, protected mode processors use a look up table.
Segment registers simply point to OS data structures that contain the information needed to access a location.
Protected mode uses privilege levels to maintain system integrity and security.
Programs cannot access data or code that is in a higher privilege level.
(Segment value exchanges at the same privilege level are allowed.)
|
|
|
|
|
|
Application programs cannot make use of protected mode by themselves.
The operating system must set up and manage a protected mode.
Capability provided by Linux and Windows NT/2000/XP/Vista systems.
Each address is a 32-bit quantity.
All of the general-purpose registers are 32 bits in size.
An easiest way to write protected mode assembly program under Windows is to create console application.
Console application is text-mode program that runs in a text-mode window called a console.
The console is controlled by a user through a command line interface,
(almost identical to the MS-DOS command window.)
Console applications use protected mode flat model and are fairly straightforward.
The default memory mode for text console app under Linux is also protected mode.
Segment registers are called selectors when operating in protected mode.
In protected mode, segment registers simply point to data structures called segment descriptors that contain the information needed to access a physical memory location.
|
|
RPL requestor privilege level - 2-bit field, specifies if the access to the segment is allowed.
Segment register (segment selector) -> segment descriptor -> physical memory
The 2-bit requestor privilege level field, the RPL, specifies segment protection level:
|
|
|
|
3. Interrupt descriptor table IDT, used by interrupt processing.
|
|
Understanding segments is an essential part of programming in assembly language.
In the family of 8086-based processors, the term segment has two meanings:
A block of memory of discrete size, called a physical segment. The number of bytes in a physical memory segment is
(a) 64K for 16-bit processors
(b) 4 gigabytes for 32-bit processors.
A variable-sized block of memory, called a logical segment occupied by a program's code or data.
Segmented architecture presents certain hurdles for 16-bit assembly-language program.
For small 16-bit flat model programs, the limitations lose importance:
code and data each occupy less than 64K and reside in individual segments.
a simple offset locates each variable or instruction within a segment.
Larger 16-bit programs, however, must contend with problems of segmented memory areas:
If data occupies two or more segments, the program must specify both segment and offset to access a variable.
When the data forms a continuous stream across segments, such as the text in a word processor's workspace, the problems become more acute.
Whenever it adds or deletes text in the first segment, the word processor must seamlessly move data back and forth over the boundaries of each following segment.
The problem of segment boundaries disappears in flat address space of 32-bit protected mode:
Although segments still exist, they easily hold all the code and data of the largest programs.
Even a very large program becomes, in effect, a small application, capable to reach all code and all data with a single offset address.
The MS-DOS and Older Windows Operating Systems | |||||
---|---|---|---|---|---|
Operating
System |
System Access |
Available Active Processes |
Addressable
Memory |
Contents of Segment
Register |
Word Length |
MS-DOS and Windows real mode | Direct to hardware and OS call | One | 1 megabyte |
Actual
address |
16 bits |
Windows 3.x virtual-86 mode | Operating system call | Multiple | 1 megabyte |
Segment
selectors |
16 bits |
Windows 3.x protected mode | Operating system call | Multiple | 16 megabytes |
Segment
selectors |
16 bits |
Windows NT 3.x | Operating system call | Multiple | 512 megabytes |
Segment
selectors |
32 bits |