MS-DOS 6.22 Memory Allocation via INT 21h

This article explains how MS-DOS 6.22 manages conventional and upper memory allocation using Memory Control Blocks (MCBs) manipulated through software Interrupt 21h. It details the internal structure of an MCB, the traversal of the memory arena chain, the primary INT 21h allocation and deallocation subfunctions, and the memory search strategies MS-DOS employs to satisfy program requests.

The Memory Control Block (MCB) Structure

MS-DOS manages heap memory by organizing free and allocated conventional memory into a contiguous linked list of arenas. Each arena begins with a 16-byte tracking header aligned to a 16-byte paragraph boundary, known as a Memory Control Block (MCB) or Arena Header.

An MCB occupies one paragraph (16 bytes) and contains the following layout:

  • Offset 00h (1 byte) - Block Type: An ASCII character denoting the block status. An 'M' (4Dh) indicates an intermediate block in the chain, while a 'Z' (5Ah) indicates the final block in the chain.
  • Offset 01h–02h (2 bytes) - Owner PSP: The segment address of the Program Segment Prefix (PSP) that owns the memory. A value of 0000h indicates that the memory block is free. A value of 0008h indicates DOS system code.
  • Offset 03h–04h (2 bytes) - Block Size: The size of the memory block directly following the header, expressed in 16-byte paragraphs. This count excludes the 16 bytes occupied by the MCB itself.
  • Offset 05h–07h (3 bytes) - Reserved: Unused space reserved by DOS.
  • Offset 08h–0Fh (8 bytes) - Program Name: In MS-DOS 4.0 and later, including 6.22, this field stores an 8-character null-padded ASCII string indicating the executable or driver name that allocated the block.

The MCB Chain

MS-DOS locates the beginning of the MCB chain through an internal system variable known as the List of Lists (SYSVARS), which can be queried using INT 21h, AH=52h.

The chain is traversed sequentially:

  1. DOS reads the current MCB segment.
  2. The next MCB segment is calculated by adding the current MCB's segment address, its size in paragraphs (from offset 03h), and 1 (accounting for the MCB itself).
  3. Traversal continues until DOS reaches an MCB marked with 'Z'. If an MCB header contains a byte other than 'M' or 'Z', MS-DOS assumes memory corruption and halts the system with an "Arena trashed" error.

Primary INT 21h Memory Management Functions

Programs communicate with MS-DOS to request, resize, and release memory via software interrupt 21h.

1. Allocate Memory (AH = 48h)

  • Input: BX = Number of paragraphs requested.
  • Output (Success): Carry Flag (CF) clear; AX = Segment address of the allocated memory block (the paragraph immediately following the MCB).
  • Output (Failure): CF set; AX = Error code (07h = Memory control blocks destroyed, 08h = Insufficient memory); BX = Size of the largest available block in paragraphs.

When invoked, DOS searches the MCB chain according to the active allocation strategy for a free block (Owner = 0000h) that satisfies the requested size. If a matching block is larger than requested, DOS splits the block: it marks the requested size as owned, and creates a new free MCB immediately following it with the remaining paragraphs.

2. Free Allocated Memory (AH = 49h)

  • Input: ES = Segment address of the block to free (points to the data area, not the MCB).
  • Output (Success): CF clear.
  • Output (Failure): CF set; AX = Error code (07h or 09h = Invalid memory block address).

DOS locates the MCB header by inspecting ES - 1 paragraph. It sets the Owner PSP field to 0000h. To prevent memory fragmentation, DOS checks whether the adjacent preceding or succeeding blocks are also free; if so, it merges them into a single larger free MCB by updating the size field and removing intermediate headers.

3. Resize Memory Block (AH = 4Ah)

  • Input: ES = Segment address of the block to modify; BX = New requested size in paragraphs.
  • Output (Success): CF clear.
  • Output (Failure): CF set; AX = Error code; BX = Maximum possible paragraphs available for this block.

If shrinking, DOS creates a new free MCB in the released space and merges it with subsequent free blocks if applicable. If expanding, DOS verifies whether the immediately following MCB is free and large enough to accommodate the expansion. DOS does not relocate blocks to satisfy expansion requests; if adjacent space is insufficient, the call fails.

Allocation Strategy and UMB Management (AH = 58h)

MS-DOS 6.22 supports Upper Memory Blocks (UMBs) located between 640 KB and 1 MB, provided a memory manager like HIMEM.SYS and EMM386.EXE is loaded. The selection of blocks is controlled via INT 21h, AH=58h:

  • Subfunction AL = 00h / 01h (Get / Set Allocation Strategy): Sets how DOS searches the MCB chain. Supported strategies passed in BX include:

    • 00h: First Fit (conventional memory) — Selects the first available free block large enough.
    • 01h: Best Fit (conventional memory) — Scans the entire chain and selects the smallest free block that satisfies the request.
    • 02h: Last Fit (conventional memory) — Selects the highest available free block in conventional memory.
    • 40h–42h: High-memory variants that allocate from Upper Memory Blocks first before falling back to conventional memory.
    • 80h–82h: Strict high-memory variants that allocate exclusively from UMBs.
  • Subfunction AL = 02h / 03h (Get / Set UMB Link State): Controls whether UMBs are linked into the active DOS MCB chain. Passing BX = 0001h to subfunction 03h connects the UMB MCB chain to the conventional memory chain, allowing standard AH=48h calls to utilize upper memory based on the chosen allocation strategy. Passing BX = 0000h unlinks UMBs, restricting standard allocations back to the conventional 640 KB address space.