X86 memory models explained

In computing, the x86 memory models are a set of six different memory models of the x86 CPU operating in real mode which control how the segment registers are used and the default size of pointers.

Memory segmentation

See main article: x86 memory segmentation. Four registers are used to refer to four segments on the 16-bit x86 segmented memory architecture. DS (data segment), CS (code segment), SS (stack segment), and ES (extra segment). Another 16-bit register can act as an offset into a given segment, and so a logical address on this platform is written segment:offset, typically in hexadecimal notation. In real mode, in order to calculate the physical address of a byte of memory, the hardware shifts the contents of the appropriate segment register 4 bits left (effectively multiplying by 16), and then adds the offset.

For example, the logical address 7522:F139 yields the 20-bit physical address:

  75220
+ F139
  84359

Note that this process leads to aliasing of memory, such that any given physical address has up to 4096 corresponding logical addresses. This complicates the comparison of pointers to different segments.

Pointer sizes

Pointer formats are known as near, far, or huge.

mov bx, word [reg] mov ax, word [bx] mov dx, word [bx+2]

les bx,dword [reg] mov ax,word [es:bx] mov dx,word [es:bx+2]

les bx,dword [reg] mov ax,word [es:bx] add bx,2 test bx,0xfff0 jz lbl sub bx,0x10 mov dx,es inc dx mov es,dx lbl: mov dx,word [es:bx]

Memory models

The memory models are:

Model Data Code Definition
Tiny* near CS=DS=SS
Small near** near DS=SS
Medium near** far DS=SS, multiple code segments
Compact far near single code segment, multiple data segments
Large far far multiple code and data segments
Huge huge far multiple code and data segments; single array may be >64 KB

Other platforms

In protected mode a segment cannot be both writable and executable.[2] [3] Therefore, when implementing the Tiny memory model the code segment register must point to the same physical address and have the same limit as the data segment register. This defeated one of the features of the 80286, which makes sure data segments are never executable and code segments are never writable (which means that self-modifying code is never allowed). However, on the 80386, with its paged memory management unit it is possible to protect individual memory pages against writing.[4] [5]

Memory models are not limited to 16-bit programs. It is possible to use segmentation in 32-bit protected mode as well (resulting in 48-bit pointers) and there exist C language compilers which support that.[6] However segmentation in 32-bit mode does not allow to access a larger address space than what a single segment would cover, unless some segments are not always present in memory and the linear address space is just used as a cache over a larger segmented virtual space. It allows better protection for access to various objects (areas up to 1 MB long can benefit from a one-byte access protection granularity, versus the coarse 4 KiB granularity offered by sole paging), and is therefore only used in specialized applications, like telecommunications software. Technically, the "flat" 32-bit address space is a "tiny" memory model for the segmented address space. Under both reigns all four segment registers contain one and the same value.

x86-64

On the x86-64 platform, a total of seven memory models exist,[7] as the majority of symbol references are only 32 bits wide, and if the addresses are known at link time (as opposed to position-independent code). This does not affect the pointers used, which are always flat 64-bit pointers, but only how values that have to be accessed via symbols can be placed.

See also

Bibliography

Notes and References

  1. Web site: Intel Instruction Set - LES . Intel Instruction Set pages . . October 19, 2015.
  2. Web site: Intel 64 and IA-32 Architectures Developer's Manual: Vol. 3A . Intel . September 13, 2011 . 3–17.
  3. Web site: AMD64 Architecture Programmer's Manual Volume 2: System Programming . AMD . September 13, 2011 . 82–84.
  4. Web site: Intel 64 and IA-32 Architectures Developer's Manual: Vol. 3A . Intel . September 13, 2011 . 4–41.
  5. Web site: AMD64 Architecture Programmer's Manual Volume 2: System Programming . AMD . September 13, 2011 . 139.
  6. Web site: Open Watcom C Language Reference version 2 . github.com/open-watcom . Open Watcom . 10 January 2018.
  7. Web site: System V Application binary Interface, AMD64 Architecture Processor Supplement, Draft Version 0.99.7. 33–35.