How MS-DOS 6.22 Calculates Free RAM During Execution
In MS-DOS 6.22, the operating system calculates remaining system RAM by traversing a linked list of data structures known as Memory Control Blocks (MCBs) or Arena Headers. Rather than maintaining a dynamic runtime counter in real time, DOS inspects the conventional memory space—and any linked Upper Memory Blocks (UMBs)—paragraph by paragraph. By evaluating the owner fields and sizes stored within these MCBs, DOS determines the total unallocated space, as well as the largest contiguous block available for running applications.
The Structure of Memory Control Blocks (MCBs)
DOS manages real-mode conventional memory (the first 640 KB) in units called paragraphs, where one paragraph equals 16 bytes. Directly preceding every allocated or free region of memory lies a 16-byte MCB. The critical fields within this structure include:
- Signature Byte (Offset 0x00): A character
indicating the block's position in the chain. An ASCII
M(0x4D) indicates that more blocks follow, while an ASCIIZ(0x5A) marks the final block in the chain. - Owner PSP (Offset 0x01–0x02): A 16-bit word
containing the segment address of the Program Segment Prefix (PSP) that
owns the memory. If this value is
0000h, the memory block is completely free and unallocated. - Block Size (Offset 0x03–0x04): A 16-bit word specifying the size of the memory area immediately following the MCB, expressed in paragraphs.
- Program Name (Offset 0x08–0x0F): An 8-byte field introduced in later DOS versions that stores the name of the executable owning the block, if applicable.
Traversing the MCB Chain
To calculate available memory, MS-DOS begins at the first MCB, the segment address of which is stored internally in the DOS "List of Lists" (accessible via Interrupt 21h, Function 52h).
The calculation follows a deterministic loop:
- Read the Header: DOS reads the current MCB's signature, owner, and size fields.
- Evaluate Allocation: If the Owner PSP field
contains
0000h, DOS identifies the attached memory block as free. The paragraph count in the block size field is added to a running tally of total free memory. - Track the Largest Block: DOS compares the current free block's size against the largest free contiguous block encountered so far, updating the maximum value if the current block exceeds it.
- Advance to the Next MCB: DOS calculates the memory
segment of the next MCB by taking the current MCB's segment, adding
1(for the 16-byte MCB itself), and adding the paragraph count stored in the size field. - Termination: The loop repeats until an MCB with the
Zsignature is processed. If an invalid signature (neitherMnorZ) is found, DOS halts execution with a "Memory allocation error" (arena trashed).
Querying Memory During Execution
Programs query remaining RAM primarily through DOS Interrupt 21h services rather than parsing MCBs manually:
- INT 21h, Function 48h (Allocate Memory): When a
program requests more paragraphs than are available (typically by
passing
BX = 0xFFFF), the function fails with carry flag set and returns the size of the largest contiguous free block in theBXregister. This is the primary method programs use to measure their working headroom. - INT 21h, Function 4Ah (Modify Allocation): When an
.EXEprogram loads, DOS by default allocates all remaining conventional memory to that program. Well-behaved programs immediately calculate how much memory they actually need, use Function 4Ah to shrink their initial allocation, and thereby release the remaining space back into an unowned (0000h) MCB.
Handling Upper Memory Blocks (UMBs)
If DOS=UMB and a memory manager such as
HIMEM.SYS combined with EMM386.EXE are loaded
in CONFIG.SYS, MS-DOS extends its calculation to the Upper
Memory Area (between 640 KB and 1 MB).
DOS links UMBs into the global memory calculation by creating
additional MCB chains in the upper address segments. Utilities like
MEM.EXE use DOS internal flags to switch between searching
only conventional memory or scanning both conventional and upper memory
chains, summing the free paragraphs from both regions to report the
comprehensive system memory profile.