How to Edit Binary Files in MS-DOS 6.22

In MS-DOS 6.22, the primary built-in utility for inspecting and modifying binary files directly from the command line is DEBUG (DEBUG.EXE). This article covers how DEBUG functions as a command-line hex editor and machine code debugger, provides the essential subcommands required to inspect and alter binary data, and walks through a practical step-by-step example of editing a file safely.

Understanding the DEBUG Utility

DEBUG is a 16-bit program provided natively with MS-DOS to test, alter, and debug executable files and raw binary data. Unlike text editors like EDIT.COM, which corrupt binary files by interpreting control characters, DEBUG interacts directly with raw hexadecimal bytes in memory.

To load a file into DEBUG, pass the filename as an argument directly from the DOS prompt:

DEBUG FILENAME.BIN

Once loaded, DEBUG displays a hyphen prompt (-), indicating it is ready to receive internal commands.

Essential Commands for Binary Modification

Navigating and changing bytes within DEBUG requires only a few core single-letter commands:

  • D (Dump): Displays memory contents in both hexadecimal and ASCII representation. Running D sequentially pages through the file, or you can specify an offset range (e.g., D 100 120).
  • E (Enter): Modifies memory starting at a specified address. You can provide replacement bytes in hexadecimal notation directly.
  • R (Register): Displays or modifies CPU registers. When saving files, the BX and CX registers define the file size in bytes (BX holds the high word, CX holds the low word).
  • W (Write): Writes the loaded or modified data from memory back to the disk.
  • Q (Quit): Exits the DEBUG program back to the standard MS-DOS prompt without saving unless W was issued previously.

Step-by-Step Binary Editing Example

To change specific bytes within a target file:

  1. Load the File: Type DEBUG TARGET.DAT and press Enter.
  2. Locate the Target Data: Type D 100 to dump memory starting at offset 0100h (the standard entry offset for files loaded in DEBUG). Note the memory offset where you wish to make changes.
  3. Edit the Bytes: Type E followed by the offset (for example, E 010A). DEBUG will display the current byte followed by a period. Type the new two-digit hexadecimal value and press Space to advance to the next byte, or press Enter to finish editing that address.
  4. Save the Changes: Type W and press Enter. DEBUG will report "Writing XXXX bytes" to confirm the changes have been written to disk.
  5. Exit: Type Q and press Enter to return to the MS-DOS prompt.

Limitations

DEBUG was designed primarily for small 16-bit .COM programs and raw memory segments. By default, it operates comfortably within a single 64KB segment. Modifying files larger than 64KB requires manually managing segment registers and file length pointers, making it best suited for targeted binary patches, firmware alterations, and small binary utilities.