INT 21h in Depth: DOS Services Every 8086 Student Uses

INT 21h is the doorway between an 8086 program and DOS. A program puts a function number in AH, puts any parameters in the other registers, executes INT 21h, and DOS does the work: printing, reading the keyboard, opening files, or ending the program. Almost every student program uses a handful of these functions, and understanding the pattern matters more than memorising the whole list.

How a DOS call works

MOV AH, 09h          ; select the function: print a string
LEA DX, message      ; parameter: address of the string
INT 21h              ; DOS runs the function

Three conventions run through nearly all INT 21h functions. AH selects the function. Input parameters arrive in registers such as DX, BX, CX or AL, and the documentation for each function says exactly which. Results come back in registers, most often AX, and file functions signal an error by setting the carry flag, with an error code in AX. INT 21h itself does not print anything or read anything on its own; it dispatches to the function AH names.

The functions students use first

AH Function Parameters and result
01h Read a character with echo Returns the character in AL; waits for a key.
02h Write a character Character in DL; prints it.
09h Print a string DS:DX points to the string, which must end with ‘$’.
0Ah Buffered keyboard input DS:DX points to a buffer whose first byte is its maximum length.
2Ah Get the system date Returns year in CX, month in DH, day in DL.
2Ch Get the system time Returns hours in CH, minutes in CL, seconds in DH.
4Ch Exit to DOS Exit code in AL; the standard way to end a program.

File functions: the same pattern, with error handling

AH Function Key registers
3Dh Open a file DS:DX points to the filename, AL is the access mode, handle returns in AX.
3Eh Close a file Handle in BX.
3Fh Read from a file Handle in BX, buffer in DS:DX, count in CX, bytes read return in AX.
40h Write to a file Handle in BX, buffer in DS:DX, count in CX, bytes written return in AX.
42h Move the file pointer Handle in BX, mode in AL, offset in CX:DX.

For these functions, always check the carry flag after the call. If CF is set, AX holds an error code instead of a handle or a byte count, and using AX as if it were a valid handle is a classic bug.

Worked example: print a string, read a key, exit cleanly

ORG 100h

LEA DX, message
MOV AH, 09h          ; print the '$'-terminated string
INT 21h

MOV AH, 01h          ; wait for one keypress, echo it
INT 21h

MOV AX, 4C00h        ; exit with code 0
INT 21h

message DB 'Press any key to continue...', 0Dh, 0Ah, '$'
RET

In a .COM program, DS starts out pointing at the program segment, so LEA DX, message reaches the string without a separate segment setup step. The 0Dh, 0Ah pair before the ‘$’ is the carriage return and line feed that move the cursor to the start of the next line.

What INT 21h is not

INT 21h is a DOS service, not a processor instruction. It does not exist on a bare 8086 chip, and modern operating systems do not run DOS programs natively. In Emu8086 it works because the emulator provides a DOS environment, which is exactly why student code written for the emulator cannot simply be copied onto a modern machine and expected to run. For screen and keyboard work that is not DOS-specific, see the interrupt table post, which covers INT 10h and INT 16h alongside INT 21h.

Further reading

Similar Posts