Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 66 ยท Memory Management

Memory Management Basics: Addresses, Binding and the MMU

Learn logical and physical addresses, address binding times, base and limit registers, the MMU, dynamic loading and linking, and overlays, with Python simulations.

The CPU can only execute instructions and read data that are in main memory. Disks are far too slow to run programs from directly, so the operating system must decide which programs live in memory, where they are placed, and how they are kept from damaging each other. That job is memory management.

The goals are the same on every system, from a phone to a server:

  • Relocation: a program should run no matter where in memory it is placed.
  • Protection: one process must not read or write another process's memory, or the kernel's.
  • Sharing: processes should be able to share code (such as a C library) when that is safe.
  • Efficiency: memory should not sit idle, and finding space should be fast.

Logical and physical addresses

A running program never needs to know where it physically is in RAM. It uses logical addresses (also called virtual addresses), usually starting at 0. The hardware turns each one into a physical address, the actual cell on the memory chips.

Logical (virtual) address Physical address
Generated by The CPU while running the program The memory management unit (MMU)
Seen by The program The memory hardware
Range The logical address space of the process The physical address space of the machine
Can differ between runs? No (stays the same) Yes (the process may be loaded anywhere)

The mapping hardware is the memory management unit (MMU). It sits between the CPU and memory and translates on every access, in hardware, so there is almost no speed cost.

flowchart LR
    CPU["CPU"] -- "logical address" --> MMU["MMU (base + limit)"]
    MMU -- "physical address" --> RAM["Main memory"]
    MMU -. "out of range: trap to OS" .-> OS["Operating system"]

Every memory access passes through the MMU; a bad address is caught before it reaches memory.

Base and limit registers

The simplest MMU uses two registers per process, loaded by the OS on every context switch:

  • Base (relocation) register: the physical address where the process starts.
  • Limit register: the size of the process's address space.

For each logical address a: if a >= limit, the hardware raises a trap (addressing error); otherwise the physical address is base + a.

Python A base-and-limit MMU

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both processes use the same logical address 0, yet they land in different physical places, and a stray address is trapped instead of corrupting a neighbour. The last step shows relocation: the OS can move a process in memory at any time by copying it and updating one register. This is what makes later techniques such as compaction possible.

Only the kernel may load the base and limit registers (a privileged instruction), which is why a user program cannot simply widen its own limit.

Address binding

A program is written with symbolic names (count, main). Before it runs, these must be bound to numeric addresses. The binding can happen at three moments:

When Who Consequence
Compile time The compiler, if the load address is known Produces absolute code. It only works at that address; changing it means recompiling (old MS-DOS .COM programs).
Load time The loader Compiler produces relocatable code (addresses relative to the start); the loader adds the real start address. To move the process, reload it.
Execution time The hardware, while running Binding is delayed until each access, using base/limit registers or paging. The process can move during execution. All modern general-purpose systems use this.
Python Absolute, load-time and run-time binding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only execution-time binding lets the OS move a running process, which is why it is required for paging and virtual memory.

Dynamic loading and linking

Dynamic loading loads a routine into memory only when it is first called. Error-handling code that is rarely used then costs no memory in most runs, and the OS does not need to provide anything special: it is up to the program design.

Dynamic linking postpones the linking step until run time. Instead of copying a library such as the C library into every executable (static linking), the executable contains a small stub. At the first call, the stub finds the library routine in memory (loading it if needed) and patches itself. Benefits: smaller executables, one shared copy of library code in RAM used by many processes (shared libraries: .so on Linux, .dll on Windows, .dylib on macOS), and library updates without relinking. The OS must help here, because it has to let a process use memory that belongs to another process (the shared library).

Overlays

Early systems with very small memories used overlays: only the part of a program needed at the moment is kept in memory, and other parts replace it when required. A two-pass assembler, for example, needs the code of pass 1 and its tables first, and later only pass 2. The programmer had to design the overlay structure by hand, which was slow and error-prone.

flowchart TB
    subgraph Memory["Memory (smaller than the whole program)"]
        D["Common code and data (always resident)"]
        O["Overlay area"]
    end
    P1["Pass 1 code"] -. "loaded first" .-> O
    P2["Pass 2 code"] -. "replaces pass 1" .-> O

Overlays are managed by the programmer; virtual memory later automated the idea for everyone.

Overlays are obsolete on general-purpose systems, but the same thinking survives in embedded devices with tiny memories and in game consoles with fixed memory budgets.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the MMU do?

    The memory management unit is the hardware between the CPU and memory that maps and checks addresses.
  2. 2. A process has base = 4000 and limit = 600. What happens on logical address 650?

    650 >= 600, so the hardware raises an addressing-error trap to the OS.
  3. 3. Which binding time allows the OS to move a process while it is running?

    With execution-time binding addresses are translated on every access, so moving the process only needs a new base value.
  4. 4. What is a benefit of dynamic linking with shared libraries?

    The library code is loaded once and used by all processes that link to it.
  5. 5. Why can only the kernel change the base and limit registers?

    Loading them is a privileged instruction. Otherwise any program could widen its limit and read other processes' memory.

Summary

  • Programs use logical addresses; the MMU turns them into physical addresses and enforces protection.
  • Base and limit registers give relocation and protection with one comparison and one addition per access.
  • Address binding can happen at compile, load or execution time; modern systems bind at execution time.
  • Dynamic loading and dynamic linking save memory and let libraries be shared.
  • Overlays were the manual forerunner of virtual memory.

Next: Contiguous Allocation and Fragmentation