Memory byte Reading-Writing

Segments: Dividing processor (CPU) addressable memory space into smaller protected address space called segments.

Segment Selector: A Segment selector is a 16 bit identifier for a segment. It does not point directly to the segment, instead points to segment descriptor that defines the segment. This segment descriptor has all the details about actual segment along with other properties.

Three types of address we refer often:

  1. Logical address: A logical address is a combination of segment selector and offset. Size of this address is 16(Bits):32(Bits).
  2. Linear address: Linear address is the result of the segment value which either got from GDT (Global Descriptor Table) or LDT (Local Descriptor Table) + offset field in logical address. Logical offset size is 32 bits. Linear address size will be 32 bits.
  3. Physical address: Linear address will become physical address directly if there is no paging enabled. If paging is enabled, then the result of translation using page tables by the CPU will become Physical address. Physical address size will be 32 bits, which supports 4 GB physical RAM. If PAE (Physical Addressing Extension) is enabled, then physical address size will be 36 bits, which supports 64 GB Physical RAM.

WinNT uses flat memory model and it doesn’t use segmentation. In x86 there is no option to disable segmentation so, NT should be using at least one segment.

When you allocate a memory like following:

char *pBuffer = malloc( 100 );

In user mode, we can allocate memory up to 2 GB in Workstation version of WinNT and up to 3GB in Server version of WinNT. So, using malloc() function you will be getting within 2GB or 3GB of virtual memory.

‘pBuffer’ will have logical address which is combined of Segment Selector + Offset.

If application tried to Read/Write to memory, then CPU will do following operations:

  1. Using Segment Selector it will identify which Descriptor table to select GDT or LDT. This Segment selector will be a index value for the table. Each table entry size will be 64 Bits of segment descriptor. This will have other fields like privilege of the segment, segment limit, segment base, segment type, etc… Check following figure for other fields of segment descriptor:
  2. After getting Segment Base address either from LDT or GDT, it will add Offset (32bits) of logical address to get a linear address.
  3. If paging is disabled, then Logical address will become physical address and CPU will read/write to memory. If paging is enabled, then logical address will have combination of: Page Directory(10bits)+Page Table(10bits)+Offset(12bits) = Total 32 bits.
  4. First 10bits value will be used as index to Page Directory to get Page Directory Entry value. Each Page Directory is an array of 32 bit Page Directory Entries (PDE) contains in a 4K byte page and will have maximum 1024 page directory entries. Base address of Page Directory will be stored in CR3 register; this is changed often when there is a task switch happens by operating system.
  5. Page Directory Entry (PDE) will have base address to Page Table. Page Table will have 1024 entries of size 32 bit each entry. This can contain maximum 1024 Page Table Entries (PTE). Next 10bits of logical address is used as an index to Page Table.
  6. Page Table Entry will be a base address in physical address space and Offset of 12bits in logical address will be address to Page Table Entry value to get actual physical address. Further CPU can read or write to memory byte.


All the above steps represented in following figure:

Maximum 2 level of translation will happen to the physical address:

  1. Logical Address->Linear Address->Physical address
  2. Logical Address -> Linear Address translation will happen, since we cannot disable segmentation.
  3. Linear Address -> Physical address translation will happen only if Operating system enables paging. If there is no paging enabled we cannot get the benefit of virtual memory. All modern operating system uses paging. If you have 256MB physical RAM and if you want to allocate more than that then Operating system uses Hard Disk as a backup to allocate memory and brings to RAM based on the demand by application.