How MS-DOS 6.22 Handles INT 21h Function 4Ch

When an MS-DOS 6.22 application invokes Interrupt 21h with register AH set to 4Ch, the operating system initiates a clean process termination sequence. This system call captures the program's return code, flushes disk buffers, closes open file handles, restores critical interrupt vectors to their pre-execution state, deallocates the process's conventional memory blocks, and returns execution control to the parent process (typically COMMAND.COM).

1. Interrupt Dispatch and Return Code Capture

When the CPU executes INT 21h, it consults the Interrupt Vector Table (IVT) at address 0000:0084h to jump to the DOS API entry point. The DOS kernel examines the AH register, identifies function 4Ch (Terminate Process With Return Code), and branches to the internal exit routine. The byte in the AL register is copied into an internal DOS system variable as the program's exit code, making it accessible to parent processes or batch scripts through INT 21h, AH=4Dh or the IF ERRORLEVEL shell command.

2. File and Buffer Management

The kernel accesses the terminating program's Program Segment Prefix (PSP) to locate its Job File Table (JFT), typically located at PSP offset 18h. DOS iterates through all active file handles assigned to the process, flushes any unwritten file data buffers from memory to disk, and closes the corresponding system file tables. This step prevents data corruption and frees system-wide file tracking resources.

3. Restoring Interrupt Vectors

MS-DOS applications frequently overwrite critical interrupt vectors during execution. To ensure system stability, Function 4Ch restores three vital interrupt vectors using the backup addresses stored in the terminating process's PSP:

  • INT 22h (Terminate Address): Restored from PSP offset 0Ah. This holds the return address in the parent process.
  • INT 23h (Ctrl-C Handler): Restored from PSP offset 0Eh. This restores the parent's break handler.
  • INT 24h (Critical Error Handler): Restored from PSP offset 12h. This restores the default hardware error handling routine.

4. Memory Deallocation

MS-DOS inspects the Memory Control Block (MCB) chain starting in conventional memory. The kernel scans for any memory arena headers tagged with the Program Segment Prefix (PID) of the exiting process. It marks these blocks as free, including the program's primary memory segment and its environment block (located via PSP offset 2Ch). This memory is immediately returned to the free pool for future allocations.

5. Context Switch and Return to Parent

The kernel reads the parent PSP segment address from offset 16h of the current PSP and updates its internal variable tracking the active process. Finally, DOS restores the parent process's stack pointer and transfers CPU execution to the address retrieved from the INT 22h vector. If the parent process was the transient portion of COMMAND.COM, the command interpreter checks its own integrity, reloads any overwritten code from disk if necessary, and displays the command prompt.