PadhaikoAll subjects

Interrupt Services: INT 21H and INT 10H

Microprocessors (ENEX 201)

Open in the Padhaiko app

3.12 Interrupt services: INT 21H and INT 10H

In 8086 MS-DOS systems, application programs rarely program keyboard controllers or video RAM hardware directly. Instead, they invoke built-in software interrupts using the INT n instruction. By convention, the caller loads a function number into AH and arguments into AL, BX, CX, DX, or DS:DX before executing INT.

AspectDOS Interrupt (INT 21H)BIOS Video Interrupt (INT 10H)
Provider LayerMS-DOS operating system kernelMotherboard / Video card ROM-BIOS
Primary ScopeKeyboard I/O, console text output, file/directory I/O, memory, program exitVideo modes, cursor positioning, window scrolling, screen clearing, colored text/pixels
Portability / RedirectionSupports DOS standard input/output redirection (<, >)Talks directly to display hardware/video buffer (no DOS redirection)
String TerminatorAH = 09H requires trailing '\$' (24H)Prints exact character count in CX (AH = 09H/0AH) or single char in AL (AH = 0EH)

DOS Function Calls (INT 21H)

Common INT 21H DOS Functions for Console I/O and Exit
AHService NameInput RegistersOutput / Behaviour
01HKeyboard Input with EchoNoneWaits for key; echoes character to screen; returns ASCII in AL (checks Ctrl-C).
02HDisplay Single CharacterDL = ASCII characterPrints character in DL at current cursor and advances cursor.
07HDirect Console Input (No Echo)NoneReads key without echo and without Ctrl-C check; ASCII in AL.
08HConsole Input (No Echo)NoneReads key without echo; checks Ctrl-C; ASCII in AL.
09HDisplay StringDS:DX = address of stringPrints characters from DS:DX up to (excluding) '\$' (24H).
0AHBuffered Keyboard InputDS:DX = address of input bufferReads line up to Enter (0DH) into buffer; stores actual count in byte [1].
4CHTerminate ProgramAL = return code (usually 00H)Closes open handles and returns control cleanly to DOS.
Definition

Buffered String Input Structure (INT 21H, AH = 0AH)

Before calling INT 21H with AH = 0AH, the program must reserve a buffer in .DATA whose layout is:

Example declaration: BUF DB 20, ?, 20 DUP(?) (or named fields MAXLEN DB 20, ACTLEN DB ?, CHARS DB 20 DUP(?)).

BIOS Video Services (INT 10H)

Common INT 10H BIOS Video Functions
AHService NameInput RegistersOutput / Effect
00HSet Video ModeAL = mode (03H = 80x25 16-color text)Switches mode and clears the entire screen.
01HSet Cursor ShapeCH = start scan line, CL = end scan lineChanges cursor thickness (bits 5–6 of CH hide cursor when 01).
02HSet Cursor PositionBH = page (0), DH = row (0–24), DL = col (0–79)Moves text cursor to (row, col).
03HRead Cursor PositionBH = display page (0)Returns DH = row, DL = col, CH/CL = cursor scan lines.
06HScroll Window Up / ClearAL = lines (0 = clear all), BH = attribute, CH,CL = top-left, DH,DL = bottom-rightScrolls window up or clears rectangular region to color attribute in BH.
07HScroll Window DownSame as 06HScrolls window downward by AL lines (0 clears window).
09HWrite Char & AttributeAL = char, BH = page, BL = attribute, CX = repeat countDisplays char CX times at cursor without advancing cursor.
0EHTeletype OutputAL = char, BH = pagePrints char and advances cursor (interprets 0DH, 0AH, 08H).
Example 1

1 (6 marks)

Write an 8086 assembly program that clears the full 80x25 screen with bright-yellow text on a blue background (1EH) using INT 10H, moves the cursor to row 12, column 34, and displays "Institute of Engineering 8086 LAB" using INT 21H.

Setting Screen Coordinates & Attribute Registers:

  • Attribute byte for non-blinking (0), blue background (001), intense (1), yellow foreground (110): 0001 1110B = 1EH.
  • Cursor position (row=12, col=34) in hex: DH = 0CH, DL = 22H, so DX = 0C22H.

Complete 8086 Program:

Clear Screen Window and Print Centered Text8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    MSG DB 'Institute of Engineering 8086 LAB$'
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    ; 1. Clear full screen (rows 0..24, cols 0..79) with attribute 1EH
    MOV AH, 06H         ; BIOS Scroll Up / Clear Window
    MOV AL, 00H         ; 00H = clear entire window
    MOV BH, 1EH         ; Yellow (EH) on Blue (1H)
    MOV CX, 0000H       ; Top-left: row 0, col 0
    MOV DX, 184FH       ; Bottom-right: row 24 (18H), col 79 (4FH)
    INT 10H

    ; 2. Position cursor at row 12 (0CH), col 34 (22H)
    MOV AH, 02H         ; BIOS Set Cursor Position
    MOV BH, 00H         ; Display page 0
    MOV DH, 12          ; Row 12 (0CH)
    MOV DL, 34          ; Column 34 (22H)
    INT 10H

    ; 3. Print string using DOS INT 21H AH=09H
    LEA DX, MSG
    MOV AH, 09H
    INT 21H

    ; 4. Exit to DOS
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN
Registers at INT 10H AH=02H Call
AH02H
BH00H
DH0CH
DL22H
Answer:

INT 10H AH=06H (AL=00H, BH=1EH, CX=0000H, DX=184FH) clears the screen in yellow-on-blue, INT 10H AH=02H (BH=00H, DH=0CH, DL=22H) places the cursor at (12, 34), and INT 21H AH=09H prints 'Institute of Engineering 8086 LAB\$'.

Example 2

2 (6 marks)

Write an 8086 program to read a string (up to 19 characters) from the user via INT 21H AH=0AH, replace the trailing 0DH with '\$' (24H), and display it back on a new line. Also trace the buffer bytes in memory if the user types Institute of Engineering and presses Enter.

Tracing BUF Memory Layout for Input "Institute of Engineering" + Enter: Declare MAX DB 20, ACT DB ?, STR_DATA DB 20 DUP(?).

  • Before call: [BUF+0] = 14H (20).
  • After typing Institute of Engineering + Enter: DOS sets [BUF+1] = 03H (ACT = 3), [BUF+2] = 'I' (49H), [BUF+3] = 'O' (4FH), [BUF+4] = 'E' (45H), [BUF+5] = 0DH (CR).
  • To print with AH = 09H, we index STR_DATA[BX] where BX = ACT = 0003H and overwrite 0DH at BUF+5 with '\$' (24H).

Complete Program:

Buffered Keyboard Input and String Echo8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    NEWLINE  DB 0DH, 0AH, '$'
    BUF      DB 20          ; Byte 0: Max characters (including CR)
    ACT_LEN  DB ?           ; Byte 1: Actual characters typed
    TXT      DB 20 DUP(?)   ; Byte 2+: Characters + CR (0DH)
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    ; 1. Read buffered string from keyboard
    LEA DX, BUF
    MOV AH, 0AH
    INT 21H

    ; 2. Append '$' terminator right after last typed character
    MOV BL, ACT_LEN         ; BL = actual count (3 for "Institute of Engineering")
    MOV BH, 00H             ; BX = 0003H
    MOV TXT[BX], '$'        ; Replace 0DH with '$' (24H)

    ; 3. Print CR+LF then user string
    LEA DX, NEWLINE
    MOV AH, 09H
    INT 21H

    LEA DX, TXT
    MOV AH, 09H
    INT 21H

    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN
Registers & Buffer After Patching for 'Institute of Engineering'
BL03H
BH00H
[BUF+0]14H
[BUF+1]03H
[BUF+2]49H
[BUF+3]4FH
[BUF+4]45H
[BUF+5]24H
Answer:

INT 21H AH=0AH fills ACT_LEN = 03H and TXT = 'I','O','E',0DH; indexing TXT[BX] with BX = ACT_LEN replaces 0DH at BUF+5 with 24H ('\$'), allowing INT 21H AH=09H to display "Institute of Engineering".

Example 3

3 (4 marks)

Trace a program that reads a single lowercase letter silently via INT 21H AH=08H, converts it to uppercase, and prints it via INT 21H AH=02H. Show register states if the user presses 'm' (6DH).

ASCII Lowercase-to-Uppercase Conversion: Lowercase 'a'–'z' occupy ASCII 61H–7AH, while uppercase 'A'–'Z' occupy 41H–5AH—an exact difference of 20H (32). Checking AL >= 'a' and AL <= 'z' and subtracting 20H converts 'm' (6DH) to 'M' (4DH).

Program & Register Trace:

Silent Key Read and Uppercase Output8086 ASM
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
    MOV AH, 08H         ; Read key without echo -> AL
    INT 21H
    CMP AL, 'a'
    JB  PRINT_CH
    CMP AL, 'z'
    JA  PRINT_CH
    SUB AL, 20H         ; Convert lowercase to uppercase
PRINT_CH:
    MOV DL, AL          ; AH=02H prints character in DL
    MOV AH, 02H
    INT 21H

    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN
Registers Before Final INT 21H Call (Input 'm' = 6DH)
AL4DH
DL4DH
AH02H
Answer:

AH = 08H returns AL = 6DH ('m') without echoing it; SUB AL, 20H yields 4DH ('M'), which is copied to DL and printed by INT 21H AH = 02H.

Exercises

Question 1

Distinguish between DOS interrupt INT 21H and BIOS video interrupt INT 10H, and summarize five commonly used INT 21H functions with their AH values and register conventions.

DOS INT 21H vs. BIOS INT 10H:

  • INT 21H provides OS-level services (console I/O, files, program termination) that support DOS redirection.
  • INT 10H provides lower-level ROM-BIOS display control (video mode, cursor row/col, window scrolling, color attributes).

Five INT 21H Functions:

  1. AH = 01H: Read character with echo -> returns ASCII in AL.
  2. AH = 02H: Display single character -> takes ASCII in DL.
  3. AH = 09H: Print '\$'-terminated string -> takes string offset in DS:DX.
  4. AH = 0AH: Buffered line input -> takes buffer offset in DS:DX.
  5. AH = 4CH: Exit program -> takes exit code in AL.
Answer:

INT 21H handles OS-level I/O and termination (01H, 02H, 09H, 0AH, 4CH), whereas INT 10H controls hardware-level video modes, cursor placement, scrolling, and attributes.

Question 2

Explain the 3-part buffer layout required by INT 21H AH=0AH. If BUF DB 15, ?, 15 DUP(?) is used and the user types 8086 followed by Enter, give the hexadecimal value stored in each of the first 7 bytes of BUF.

Buffer Structure:

  • Byte 0: Maximum allowed characters including CR (15 = 0FH).
  • Byte 1: Actual characters typed excluding CR (4 = 04H for "8086").
  • Bytes 2+: ASCII characters '8' (38H), '0' (30H), '8' (38H), '6' (36H), followed by Carriage Return 0DH.

First 7 Bytes of BUF:

  • [BUF+0] = 0FH (15)
  • [BUF+1] = 04H (4)
  • [BUF+2] = 38H ('8')
  • [BUF+3] = 30H ('0')
  • [BUF+4] = 38H ('8')
  • [BUF+5] = 36H ('6')
  • [BUF+6] = 0DH (CR)
Answer:

Bytes 0..6 contain 0FH, 04H, 38H, 30H, 38H, 36H, 0DH.

Question 3

Write an 8086 assembly program that uses INT 10H to clear the entire screen with white text on a blue background (17H), positions the cursor at row 10, column 25, and prints "WELCOME" using INT 21H.

Attribute and Coordinate Values:

  • Blue background (001B) + non-blinking (0B) + normal white foreground (0111B) = 0001 0111B = 17H.
  • Row 10 = 0AH, Column 25 = 19H -> DX = 0A19H.

Complete Program:

Clear Screen (17H) and Display WELCOME at (10,25)8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    MSG DB 'WELCOME$'
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV AH, 06H         ; Scroll up / clear screen
    MOV AL, 00H
    MOV BH, 17H         ; White on blue attribute
    MOV CX, 0000H       ; (0,0)
    MOV DX, 184FH       ; (24,79)
    INT 10H

    MOV AH, 02H         ; Set cursor position
    MOV BH, 00H
    MOV DH, 10          ; Row 10 (0AH)
    MOV DL, 25          ; Col 25 (19H)
    INT 10H

    LEA DX, MSG
    MOV AH, 09H         ; Print string
    INT 21H

    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN
Answer:

Use INT 10H AH=06H (AL=00H, BH=17H, CX=0000H, DX=184FH), INT 10H AH=02H (BH=00H, DH=0AH, DL=19H), and INT 21H AH=09H with DX = OFFSET MSG.

Question 4

Compare INT 21H functions AH = 01H, AH = 07H, and AH = 08H for keyboard input, and explain when you would choose INT 10H AH = 09H instead of INT 21H AH = 02H for displaying a character.

Keyboard Input Functions (01H, 07H, 08H):

  • AH = 01H: Echoes the typed character on screen and checks Ctrl-C/Ctrl-Break.
  • AH = 08H: Suppresses screen echo (useful for passwords/menus) while still checking Ctrl-C.
  • AH = 07H: Suppresses both screen echo and Ctrl-C handling (raw binary/control key capture).

INT 10H AH = 09H vs. INT 21H AH = 02H: INT 21H AH = 02H prints a single character using the existing screen attribute and advances the cursor. INT 10H AH = 09H lets you specify both a custom color attribute (BL) and a repeat count (CX) to draw colored characters or repeated symbols (e.g., horizontal bars) without advancing the cursor.

Answer:

01H echoes and checks Ctrl-C, 08H hides echo and checks Ctrl-C, and 07H hides echo without checking Ctrl-C. Choose INT 10H AH=09H over INT 21H AH=02H when you need custom color attributes (BL) or repeated character output (CX).

Question 5

Calculate the 8-bit video attribute byte for a blinking, bright-red foreground on a cyan (blue + green) background, and show the INT 10H register values to print 'X' 5 times at the current cursor with that attribute on page 0.

Constructing the Attribute Byte:

  • Bit 7 (Blink) = 1
  • Bits 6–4 (Background Cyan = Red 0, Green 1, Blue 1) = 011
  • Bit 3 (Intensity = Bright) = 1
  • Bits 2–0 (Foreground Red = Red 1, Green 0, Blue 0) = 100

Combining bits 7..0: 1 011 1 100B = 1011 1100B = BCH.

INT 10H Register Setup: Invoke INT 10H with AH = 09H, AL = 'X' (58H), BH = 00H (page 0), BL = BCH, and CX = 0005H.

Registers for INT 10H AH=09H
AH09H
AL58H
BH00H
BLBCH
CX0005H
Answer:

Attribute byte is BCH (1011 1100B); call INT 10H with AH=09H, AL='X', BH=00H, BL=BCH, CX=0005H.

Past exam questions for Microprocessors

← EXE vs COM, Assembling/Linking, One- & Two-Pass Assemblers8086 Programs: Arithmetic, Strings, I/O, Number Display →