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.
  • 32 represents the ANSI code for green foreground.
  • ; acts as the parameter separator.
  • 40 represents the ANSI code for black background.
  • m is 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.SYS is present in CONFIG.SYS, the driver replaces the default CON device 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:

  1. Normal State: Characters pass straight to the display buffer until byte 0x1B is encountered.
  2. Escape Detected: Receiving 0x1B transitions the driver into an escape state, expecting the [ character.
  3. 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.
  4. Command Execution: When a valid terminal letter (such as m for 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 30 through 37 map to foreground colors (Black, Red, Green, Yellow, Blue, Magenta, Cyan, White) and write to bits 0–2.
  • Codes 40 through 47 map to background colors and write to bits 4–6.
  • Code 1 sets bit 3 (Intensity) to enable bright colors.
  • Code 0 clears all attributes, restoring the hardware default (usually 0x07, 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.