How MS-DOS 6.22 Processes ANSI Screen Colors
In MS-DOS 6.22, defining screen color attributes using the
PROMPT $e[ sequence relies on a coordinated pipeline
involving the command interpreter, character device drivers, and PC
display hardware. When a user defines a colored prompt,
COMMAND.COM expands meta-characters into raw escape
characters, passes them through DOS console output functions, and relies
on the ANSI.SYS driver to parse the sequence and translate
standard ANSI color codes into hardware-level video attribute bytes.
1. Macro Expansion in COMMAND.COM
When you enter a command such as PROMPT $e[32;40m$p$g,
the internal parser inside COMMAND.COM interprets the
string. The $e token represents an escape sequence macro.
COMMAND.COM scans the input string and replaces
$e with the single ASCII character 0x1B
(decimal 27, the standard ASCII Escape character).
The remaining characters—such as [32;40m—are preserved
as literal ASCII bytes:
[is the Control Sequence Introducer (CSI) delimiter.32represents the ANSI code for green foreground.;acts as the parameter separator.40represents the ANSI code for black background.mis the terminating command character representing "Select Graphic Rendition" (SGR).
2. Output Interception via the Console Subsystem
Whenever MS-DOS prepares to display the command prompt, it writes the
expanded string to the standard output device (STDOUT),
typically using MS-DOS interrupt INT 21h, Function 09h
(Display String) or INT 21h, Function 02h (Display
Character).
These system calls route the byte stream directly to the DOS console
device driver, CON.
- Without ANSI.SYS: The default ROM-BIOS console
handler does not interpret escape sequences. It attempts to print
character
0x1B(often displayed as a left arrow←) followed literally by[32;40m. - With ANSI.SYS Loaded: When
DEVICE=C:\DOS\ANSI.SYSis present inCONFIG.SYS, the driver replaces the defaultCONdevice handler and intercepts the raw character stream before it reaches video display routines.
3. State-Machine Parsing in ANSI.SYS
ANSI.SYS acts as a finite state machine monitoring the
incoming character stream:
- Normal State: Characters pass straight to the
display buffer until byte
0x1Bis encountered. - Escape Detected: Receiving
0x1Btransitions the driver into an escape state, expecting the[character. - Parameter Processing: Once
[is read, the driver enters argument collection mode, reading numeric ASCII digits and storing them in an internal parameter array. Semicolons reset the digit accumulator to begin capturing the next argument. - Command Execution: When a valid terminal letter
(such as
mfor SGR) is reached, parsing ends, and the driver executes the requested function using the collected parameters.
4. Translation to PC Video Attribute Bytes
The IBM PC text mode video hardware does not understand ANSI escape codes natively; it understands an 8-bit attribute byte packed with foreground, background, and intensity bits:
- Bits 0–2: Foreground Color (Blue, Green, Red)
- Bit 3: Foreground Intensity (Bold/High Intensity)
- Bits 4–6: Background Color (Blue, Green, Red)
- Bit 7: Blinking text or High Intensity Background
ANSI.SYS translates the collected SGR arguments into
this single 8-bit mask:
- Codes
30through37map to foreground colors (Black, Red, Green, Yellow, Blue, Magenta, Cyan, White) and write to bits 0–2. - Codes
40through47map to background colors and write to bits 4–6. - Code
1sets bit 3 (Intensity) to enable bright colors. - Code
0clears all attributes, restoring the hardware default (usually0x07, light gray on black).
5. Hardware Video Memory Update
Once the 8-bit attribute byte is computed, ANSI.SYS
updates an internal state variable holding the current active attribute.
Subsequent printable text emitted by COMMAND.COM (such as
the $p$g current path and > symbol) is
rendered to the screen using this active attribute.
To render the characters, ANSI.SYS bypasses standard
ASCII display routines and invokes ROM-BIOS
INT 10h, Function 09h (Write Character and Attribute at
Cursor Position) or writes character-attribute pairs directly to the
video memory buffer at memory segment B800h (for standard
color CGA/EGA/VGA text modes). The characters are combined with the
newly configured attribute byte, completing the color change.