Clear Keyboard Buffer in MS-DOS 6.22

In MS-DOS 6.22, clearing the keyboard input buffer is essential for batch files and user prompts to discard accidental keystrokes entered while a program was loading. While MS-DOS lacks a single native shell command (like CLS or DIR) dedicated to flushing the keyboard buffer, the standard and most reliable method is creating a lightweight utility using the native DEBUG.EXE tool to invoke DOS Interrupt 21h, Function 0Ch.

Why MS-DOS Lacks a Direct Command

The command processor (COMMAND.COM) does not feature a built-in keyword to flush pending keystrokes. Input is handled at the BIOS and DOS kernel levels. When keys are pressed before an input prompt appears, the scan codes sit inside a 16-word circular queue located in the BIOS Data Area (starting at memory segment 0040:001E). To clear this queue, you must execute a system call that tells DOS to flush the buffer.

The Solution: Creating CLEARBUF.COM with DEBUG

You can create a custom, 7-byte executable named CLEARBUF.COM that flushes the keyboard buffer immediately. This uses MS-DOS's standard DEBUG.EXE utility.

Run the following commands in the MS-DOS 6.22 prompt, or save them into a script:

A:\> DEBUG
-A 100
MOV AX, 0C00
INT 21
INT 20
-R CX
CX 0000
:7
-N CLEARBUF.COM
-W
Writing 00007 bytes
-Q

How It Works:

  • MOV AX, 0C00: Loads 0Ch into the AH register and 00h into AL. Function 0Ch tells DOS to flush the keyboard buffer and then execute the subfunction specified in AL. Setting AL to 0 flushes the buffer without waiting for any further character input.
  • INT 21: Calls the DOS API to perform the flush.
  • INT 20: Cleanly terminates the program and returns control to MS-DOS.

Automating the Command in a Batch File

To generate and run this command automatically inside an MS-DOS batch routine without manual typing:

@ECHO OFF
IF NOT EXIST CLEARBUF.COM (
    ECHO A 100 > FLUSH.DAT
    ECHO MOV AX, 0C00 >> FLUSH.DAT
    ECHO INT 21 >> FLUSH.DAT
    ECHO INT 20 >> FLUSH.DAT
    ECHO. >> FLUSH.DAT
    ECHO R CX >> FLUSH.DAT
    ECHO 7 >> FLUSH.DAT
    ECHO N CLEARBUF.COM >> FLUSH.DAT
    ECHO W >> FLUSH.DAT
    ECHO Q >> FLUSH.DAT
    DEBUG < FLUSH.DAT > NUL
    DEL FLUSH.DAT
)
CLEARBUF.COM

Whenever CLEARBUF.COM is invoked in your scripts, any pending keystrokes in memory are discarded, ensuring that subsequent prompts like PAUSE or CHOICE only respond to fresh user input.