How MS-DOS 6.22 ANSI.SYS Parses Escape Sequences
In MS-DOS 6.22, the ANSI.SYS device driver enables
standardized terminal control by intercepting console output and
processing ANSI escape sequences. Rather than passing raw text directly
to the screen via BIOS video routines, ANSI.SYS uses an
internal finite state machine to detect the escape character, collect
incoming command arguments, and execute hardware-level video and
keyboard operations. This article explains the technical mechanics of
how ANSI.SYS hooks system interrupts, tracks parsing
states, evaluates sequence parameters, and executes screen-manipulation
routines.
Interrupt Interception and Hooking
When loaded via CONFIG.SYS, ANSI.SYS
operates as a character device driver that hooks into the MS-DOS console
output pipeline. In MS-DOS 6.22, it primarily intercepts
INT 29h (the fast console output interrupt) and hooks
INT 16h (keyboard services) to support key
redefinition.
Every character sent through DOS standard output functions (such as
INT 21h, Functions 02h and 09h)
eventually routes to INT 29h. Under normal circumstances,
INT 29h writes characters directly to video memory or
invokes video BIOS routines. Once ANSI.SYS is active, its
interrupt service routine receives every outgoing byte first, allowing
it to inspect text streams character by character before anything
appears on the display.
The Parsing State Machine
The parser processes bytes sequentially using a finite state machine. The parsing pipeline moves through several distinct phases:
- Normal State (Pass-Through): The driver checks each
incoming byte. If the byte is any standard printable ASCII character or
common control code (such as carriage return
0Dhor line feed0Ah), the driver writes it to the screen using video BIOSINT 10hcalls. - Escape Detection State: When the driver detects
byte
1Bh(ASCII 27 orESC), it suppresses display output and changes its internal state flag to indicate an escape sequence has initiated. - Control Sequence Introducer (CSI) State: The driver
reads the subsequent byte. If the next character is an opening bracket
(
[, ASCII5Bh), the parser recognizes a valid CSI header (ESC[). If any other character follows the escape byte, the driver aborts the sequence, resets to the normal state, and outputs the characters literally. - Parameter Collection State: Once inside the CSI
state, the parser expects zero or more parameters. Parameters are
ASCII-encoded numeric values (0–9) separated by semicolons
(
;, ASCII3Bh). The driver converts these ASCII strings into binary integers on the fly and stores them sequentially in an internal parameter array. If a parameter is omitted (for example,ESC[;H),ANSI.SYSsubstitutes the default value (typically 1 or 0, depending on the command). - Command Dispatch State: When the parser encounters
an alphabetic character or symbol outside the delimiter range (typically
ASCII
40hto7Eh), it treats this character as the terminating command byte and triggers execution.
Command Execution and Hardware Dispatch
Once the terminating command byte arrives, ANSI.SYS
references an internal jump table to execute the code associated with
the command:
- Cursor Movement (
H,f,A,B,C,D): The driver reads row and column values from its parsed parameter buffer. It converts these 1-based ANSI coordinates into the 0-based coordinate system used by the PC BIOS, adjusts the values to remain within the active screen boundary, and callsINT 10h, AH=02hto set the cursor position. - Screen and Line Clearing (
J,K): Clearing operations check the parameter (e.g.,2Jto clear the entire screen). The driver queries the current display mode to determine dimensions, then callsINT 10h, AH=06h(scroll active page up) with the line count set to 0, which clears the specified video memory region and applies the active attribute byte. - Set Graphics Rendition (
m): Themcommand alters the active text attribute (color and intensity). The driver iterates through the gathered parameters, updating its internal byte that stores foreground colors, background colors, blinking states, and high-intensity flags. This attribute byte is appended to all subsequent text sent to video display routines. - Keyboard Key Reassignment (
p): If the terminating character isp, the driver reads the parameters as key codes (either ASCII codes or extended scan codes preceded by 0) followed by the replacement string.ANSI.SYSstores these mappings in an internal translation table. When applications request keyboard input throughINT 16h, the hooked routine checks this table and injects the remapped keystrokes into the keyboard buffer.
Error Handling and State Resets
If an illegal character appears while the driver is accumulating numeric parameters—such as an unexpected control character or non-numeric symbol—the parsing mechanism terminates immediately. The driver flushes its parameter buffer, resets its internal state index back to the default pass-through state, and resumes normal text output without displaying an error message.