Labels

new blog 2.0

2008/08/17

[0x02]. Notes on Assembly - Acquainting oneself with the Memory

The biggest part of Assembly Language is all about the CPU talking to the main memory. I'm going to dive deep into this subject. Let's start with the memory management so we can smoothly move over to the CPU and understand what different CPU registers were designed for.

For a start read How Computers Work: Processor and Main Memory by Roger Young to understand in more detail how memory addressing and memory IO operations are performed.

Below a list of essential terms connected to memory and its management.

Random Access Memory (RAM)
  • main operational memory in PC computers. Its characteristic consists in the way the data is accessed (read from or written to), namely just by using electric impulses. (This is very different from other storage mediums, e.g. magnetic tape, where reading data requires mechanical movement of the tape what takes very long and the time of reading some particular data depends on data's physical location on the medium.)
  • Running programs and their data are read into RAM at execution time.
x86 Processor Modes
Memory is always accessed under a strong supervision of the processor, if not by the CPU itself, hence the CPU controls what mode the memory is accessed in.
  • Real mode - there original CPU mode introduced with 286 machines. It has a 20bit address space, thus allowing to address 2^20bytes (=1MiB) of memory only. Segments in real more are always 64KiB in size.

  • Protected mode - due to compatibility reasons, all x86 CPUs start in Real mode (so they can support archaic operating systems like DOS), and can be immediately switched to Protected mode by setting appropriate flags in the registers. Protected mode can enrich the system into additional features, like
    • the use of virtual memory,
    • 8086 virtual mode,
    • privilege levels,
    • multitasking
    • and others...
    Segment sizes can vary.

  • Unreal mode - this mode breaks the 20-bit addressing limit that exists in real mode and allows to address up to 4GiB of Memory

  • Long - this mode is available on 64bit processors only. It allows 64bit applications to run 64bit, at the same time 16 and 32 bit apps are switched to compatibility mode and can be executed without problems.
Virtual Memory
On modern operating systems hundreds of processes run at the same time. If you sum up the amount of memory they use at any given time it would exceed the physical RAM amount. This is possible thanks to virtual memory, a technique that tricks running programs into thinking, that they have more RAM memory at disposal than there is factually available. It is done by dumping the memory space of inactive processes into secondary storage. This is called paging. Moreover, Virtual Memory enables Operating Systems to protect and manage memory the way an Operating System is programmed to.

Paging
During this process inactive areas of real memory are dumped onto the secondary storage and used re-read back into RAM when a program calls them.

Segmentation
A relative way (not an absolute way) to address physical memory by a usage of the Segment:Offset notation. Best explained in this article by Daniel Sedory:

Memory Management Unit
a hardware part of the CPU that controls how the CPU accesses memory. Its 4 main functions are:
  • translating virtual-to-physical addresses;
  • memory protection;
  • cache control;
  • bus arbitration;
... so roughly:
  1. When a x86 computer is turned on, it starts in real mode and can only address the first mebibyte (1024*1024bytes=KiB^2) of RAM. This is more than enough to bootstrap an Operating System.
  2. The Operating System can switch the CPU from real into protected mode.
  3. When in protected mode, the system can take advantage of e.g. memory protection and virtual memory to manage memory resources. It does that under strong supervision of the Memory Management Unit.
http://en.wikipedia.org/wiki/X86
http://en.wikipedia.org/wiki/Memory_management_unit
http://en.wikipedia.org/wiki/Virtual_memory

No comments: