MS-DOS 6.22 Environment Variable Memory Allocation
MS-DOS 6.22 manages environment variables by assigning a dedicated
memory segment to the command processor and active processes through
Memory Control Blocks (MCBs). This article explains how the operating
system handles memory allocation for extended environment variable
strings, covering default constraints, the /E switch,
structural storage within the Program Segment Prefix (PSP), and how
memory boundaries dictate string limits.
The Master Environment and Default Sizing
In MS-DOS 6.22, the primary instance of COMMAND.COM
creates the master environment table during system boot. By default,
MS-DOS allocates a fixed buffer of 256 bytes for this environment
block.
Every environment variable consists of an ASCII string formatted as
NAME=VALUE, terminated by a single null byte
(00h). The entire environment block is terminated by an
additional null byte, directly followed by a word count and the full
path of the executable that owns the environment. Because each string
consumes its character length plus formatting bytes, the default
256-byte pool quickly runs out when defining multiple extended directory
paths or large variable values.
Expanding the Environment via CONFIG.SYS
To accommodate extended strings or a large volume of variables, the
environment segment must be pre-allocated with a larger boundary in
CONFIG.SYS. MS-DOS does not dynamically expand the master
environment segment if it is hemmed in by subsequent memory
allocations.
Expansion is configured using the SHELL directive paired
with the /E switch:
SHELL=C:\DOS\COMMAND.COM C:\DOS\ /E:1024 /P
- The
/E:sizeparameter: Defines the environment size in bytes. - Range limits: Accepted values range from 160 to 32,768 bytes.
- Paragraph alignment: MS-DOS rounds the requested byte size up to the nearest 16-byte boundary (one paragraph) when assigning memory.
Memory Control Blocks and the PSP
When COMMAND.COM loads, MS-DOS uses standard Memory
Control Blocks (MCBs) to carve out conventional memory:
- MCB Header: A 16-byte structure placed immediately
before the environment block designating ownership, block type
(
Mfor intermediate orZfor end-of-chain), and paragraph size. - PSP Link: The Program Segment Prefix (PSP) of any
executing process stores the segment address of its environment block at
offset
2Ch. - Contiguous Allocation: The memory reserved for
environment variables must be contiguous. If a program attempts to
extend a variable beyond the allocated capacity, MS-DOS cannot scatter
the data across fragments; it aborts the operation and returns the
standard error message:
Out of environment space.
String Length Constraints
While the environment table itself can be expanded up to 32 KB, individual variable strings face practical input constraints imposed by the command line interface:
- Interactive CLI Limit: The standard MS-DOS 6.22
command buffer accepts a maximum of 127 characters per command line.
Therefore, a single
SETcommand executed directly from the prompt cannot exceed 127 characters in total length (inclusive ofSET, the variable name, the=operator, and the value). - Batch File Processing: Batch file parsing also adheres to the 127-character command line limit, constraining the length of strings passed dynamically.
- Programmatic Assignment: Software interfacing
directly with the DOS API (or modifying the memory block at the segment
located at PSP offset
2Ch) can write extended variable strings of arbitrary length, provided the combined length of all strings does not exceed the total allocated paragraph size of the environment block.
Child Process Inheritance and Truncation
MS-DOS treats the master environment differently from child environments. When an application or secondary shell launches:
- MS-DOS reads the parent's environment block.
- It allocates a new MCB for the child process sized only to fit the active variables of the parent, rounded up to the nearest paragraph.
- It copies the parent's active strings into the new segment,
discarding any empty or unused slack space defined by the original
/Eswitch.
As a result, a child process receives an environment segment that is
virtually full upon creation. If a child process or temporary shell
needs to define new extended strings, it will immediately trigger an
Out of environment space error unless explicitly launched
with its own secondary /E parameter.