Chapter 5.2: Interrupt Functions
Multiple Choice · Introduction
Question 1: Why INT n?
Why do programs reach BIOS and DOS services with INT n instead of a CALL to an address?
B: The real address of a service depends on the BIOS version and the installed DOS; the interrupt vector table (0000:0000–03FFh) holds the current address for each number. The program needs to know only the number. INT is slower than CALL because it also pushes FLAGS; a far address could also be reached with a far CALL.
Short Answer · Introduction
Question 2: BIOS and DOS Services
Which interrupt and which AH value is used for the following jobs? In which register is the input or the result?
- Read one character from the keyboard with echo
- Write the character in DL to the screen
- Write a string ending with $
- Terminate the program
- BIOS: write a teletype character
- BIOS: wait for a key press
Show answer
| Job | Interrupt / AH | Input / result |
|---|---|---|
| 1 | INT 21h / 01h | AL ← ASCII |
| 2 | INT 21h / 02h | DL = character |
| 3 | INT 21h / 09h | DS:DX = string |
| 4 | INT 21h / 4Ch | AL = exit code |
| 5 | INT 10h / 0Eh | AL = character |
| 6 | INT 16h / 00h | AL ← ASCII, AH ← scan code |
INT 10h is the BIOS screen service, INT 16h the BIOS keyboard service, and INT 21h the DOS services.
Multiple Choice · Introduction
Question 3: Subfunction
INT 21h is called. What determines which DOS service (write a character, write a string, exit …) runs?
D: The subfunction number is written to AH before the interrupt: 02h write a character, 09h write a string, 4Ch exit. Other registers such as AL, DL, DS:DX carry parameters or return results. Since AH is 8 bits, an interrupt can have at most 256 subfunctions.
Multiple Choice · Introduction
Question 4: Where the Vector Is
INT 16h is executed. From which addresses does the processor read the new IP and CS values?
C: Each vector is 4 bytes: vector address = 16h × 4 = 58h. The offset (IP, at 0058h) is stored first, then the segment (CS, at 005Ah). 0016h forgets to multiply the number by 4; 0040h computes the vector with decimal 16 × 4; the last option reverses the order.
Classic Question · INT 10h Video
Question 5: The Attribute Byte
Attribute = background × 16 + foreground; bit 7 is blink. Colors: 0 black, 1 blue, 2 green, 4 red, 6 brown, 7 light gray, A light green, E yellow, F white.
- Light green text on a brown background?
- How does 1Fh look?
- How does CEh look?
- Why can a light green (A) background not be chosen directly?
Show answer
- 6 × 16 + Ah = 6Ah.
- Blue background, white text.
- 1100 1110b: bit 7 = 1 → blinking, background 100b = red, text E = yellow.
- Only 3 bits (0–7) are reserved for the background; if A = 1010b is written, its top bit falls on bit 7 and blinking is switched on (depending on the BIOS setting it may instead show as a bright background).
Classic Question · INT 10h Video
Question 6: Address in Screen Memory
In mode 03h the offset of cell (row, column) = (row × 80 + column) × 2; in mode 13h the offset of pixel (x, y) is y × 320 + x.
- Where are the character and attribute bytes of the bottom-right cell (24, 79)?
- Cell (12, 40)?
- The last pixel (319, 199) in mode 13h?
- How many bytes does an 80 × 25 text screen occupy?
Show answer
- (1920 + 79) × 2 = 3998 = 0F9Eh → character B800:0F9Eh, attribute B800:0F9Fh.
- (960 + 40) × 2 = 2000 = 07D0h.
- 63.680 + 319 = 63.999 = A000:F9FFh, the last byte of the memory.
- 80 × 25 × 2 = 4000 bytes; mode 13h is 320 × 200 = 64,000 bytes.
The character is at an even offset and the attribute at an odd one; when a word is written, the low byte (character) goes to the lower address.
Step-by-Step Trace · INT 10h Video
Question 7: Teletype and Control Characters
The screen is empty and the cursor is at (0, 0). A, B, BS, C, CR, D are sent in turn with INT 10h/0Eh. What appears in the first row of the screen, and where does the cursor end up?
AH = 0Eh (teletype), AL = 'A'.
'A' is written at column 0, where the cursor is, and the cursor advances to 1.
'B' goes to column 1; cursor 2.
08h (BS) is not printed but interpreted as a command: the cursor moves back one, to column 1. 'B' is not erased.
'C' is written to column 1, over 'B': AC, cursor 2.
0Dh (CR): the cursor returns to the start of the row (column 0); the row does not change.
'D' is written over 'A'. Screen: DC, cursor at column 1. With AH = 09h, 08h and 0Dh would have been printed as symbols.
ORG 100h mov ah, 0Eh mov al, 'A' int 10h mov al, 'B' int 10h mov al, 08h ; BS int 10h mov al, 'C' int 10h mov al, 0Dh ; CR int 10h mov al, 'D' int 10h ret
Multiple Choice · INT 10h Video
Question 8: The Cursor after 09h
With the cursor at (3, 10), five asterisks were written using INT 10h, AH = 09h, AL = '*', BL = 1Eh, CX = 5. Where is the cursor now?
A: AH = 09h writes the character CX times with the attribute but does not advance the cursor: five asterisks are written to (3, 10)–(3, 14) and the cursor stays in place. The next write lands on the first asterisk; the cursor is moved by hand with AH = 02h. 0Eh, in contrast, advances the cursor by one at every character.
Spot the Bug · INT 10h Video
Question 9: Colored String Call
We want to write "Welcome" in yellow on a blue background at row 10, column 16. Find and fix the bugs in the code.
mov ah, 13h
mov al, 01h
mov bl, 1Eh
mov dx, 0A10h ; row 10, column 16
mov dx, offset msg
int 10h
ret
msg db 'Welcome$'
Show answer
- The address of the string is expected in ES:BP, not in DS:DX:
lea bp, msg(in a .COM program ES = DS). - The second
mov dxhas overwritten the position (DH = 10, DL = 16); DX is used only for the position. - The length goes in CX:
mov cx, 7. AH = 13h knows no terminator such as '$'; if '$' is counted, it is printed as well. - The page BH is not set:
mov bh, 0(ormov bx, 001Eh).
mov ax, 1301h ; AH=13h, AL=01h mov bx, 001Eh ; BH=0, BL=1Eh mov cx, 7 mov dx, 0A10h lea bp, msg ; ES:BP int 10h
Multiple Choice · INT 10h Video
Question 10: Positioning the Cursor
Which register values must be set before INT 10h to move the cursor to row 5, column 20 (counting from 0)?
C: INT 10h / AH = 02h positions the cursor: BH = page, DH = row, DL = column. 0Eh writes a teletype character; 00h selects a video mode (and clears the screen); 09h writes a character with color. D swaps row and column.
Multiple Choice · INT 16h Keyboard
Question 11: The F1 Key
During an INT 16h, AH = 00h call the user presses the F1 key. What are AH and AL on return?
D: AH returns the scan code (the physical position of the key), AL the ASCII code. F1 has no ASCII equivalent, so AL = 00h; the key is recognized from the 3Bh in AH (F1 … F10 = 3Bh … 44h). The arrow keys also return AL = 00h.
Step-by-Step Trace · INT 16h Keyboard
Question 12: A Keyboard Loop That Counts Digits
The user presses a, 7, :, 3 and Esc in turn. What is CX when the program ends?
CX = 0. XOR with itself clears it (ZF = 1).
'a' pressed: AL = 61h. (The scan code 1Eh goes to AH.)
Not Esc; CMP AL, '0': 61h ≥ 30h → CF = 0, JB does not jump.
CMP AL, '9': 61h > 39h → CF = 0, ZF = 0 → JA jumps: not a digit, not counted.
'7' pressed: AL = 37h.
37h − 39h needs a borrow: CF = 1 → JA does not jump.
INC CX: CX = 1.
':' pressed: AL = 3Ah, the character right after '9'.
3Ah > 39h → JA jumps; the upper bound is checked correctly.
'3': a digit, CX = 2.
Esc: AL = 1Bh.
CMP AL, 1Bh → ZF = 1, JE done: the loop ends. Result CX = 2. INT 16h does not echo keys to the screen.
ORG 100h xor cx, cx ; digit counter next_key: mov ah, 0 int 16h ; AL = ASCII cmp al, 1Bh ; Esc? je done cmp al, '0' jb next_key cmp al, '9' ja next_key inc cx jmp next_key done: ret
Step-by-Step Trace · INT 16h Keyboard
Question 13: Read a Key and Print It in Uppercase
While the program runs, the user presses the key k. What is printed on the screen?
AH = 00h: the “wait for a key and read it” subfunction.
INT 16h waits until the user presses a key. k key pressed: AL = 6Bh (ASCII), AH = scan code. Nothing is printed on the screen (no echo).
AL = 6Bh − 20h = 4Bh = 'K': in ASCII, lowercase and uppercase letters differ by 20h.
AH = 0Eh: teletype. AL is already 'K'.
INT 10h writes K to the screen, the cursor advances.
AH = 02h: DOS write character.
DL ← AL = 4Bh: INT 21h/02h expects the character in DL.
INT 21h writes a second K. The screen shows KK.
org 100h mov ah, 0 int 16h ; wait for a key sub al, 20h mov ah, 0Eh int 10h mov ah, 2 mov dl, al int 21h ret
Output Question · INT 21h DOS
Question 14: Writing to the Screen with INT 21h
What appears on the screen when the program runs? Why is the $ at the end of MSG needed?
ORG 100h MOV DX, OFFSET MSG MOV AH, 09h INT 21h MOV DL, 'B' MOV AH, 02h INT 21h RET MSG DB 'Welcome $'
Show answer
Screen: Welcome B
AH = 09h prints the string at DS:DX up to the $ (24h) character; the $ itself is not printed, but the space before it is. If the $ is forgotten, DOS prints whatever it finds in memory up to the first 24h byte (garbage on the screen).
AH = 02h prints the single character in DL ('B' = 42h). DX must be loaded with OFFSET MSG (the address), not the contents of MSG.
Step-by-Step Trace · INT 21h DOS
Question 15: Buffered Line Input
The user types Micro12 and presses Enter. What do the bytes of the buffer become? What appears on the screen?
DS:DX points to the buffer: DX = 011Ah. Byte 0 = 6: at most 6 characters, including Enter.
The user typed Micro12⏎. After 5 characters the buffer is full: '1' and '2' are not accepted (beep). Byte 1 = 5 (CR is not counted), the text starts at +2, and 0Dh is written after it.
BL ← [buffer+1] = 5: the number of characters read.
BX = 0005h: will be used in the address calculation.
[buffer + 5 + 2] = +7: '$' replaces 0Dh; the text can now be printed with 09h.
DX = buffer + 2: the start of the text.
On the screen, Micro is printed. Function 0Ah echoes only CR for Enter (no LF): the cursor is at the start of the same row and the new Micro lands on top of the old one, so a single Micro is visible on the screen. For a separate line, an LF (0Ah) must be printed first. The "12" that did not fit in the buffer was never read.
ORG 100h mov dx, offset buffer mov ah, 0Ah int 21h mov bl, buffer+1 mov bh, 0 mov buffer[bx+2], '$' mov dx, offset buffer+2 mov ah, 09h int 21h ret buffer db 6, ?, 6 dup(0)
Multiple Choice · INT 21h DOS
Question 16: $ in the Middle of a String
m db 'Price: 5$ TL$' is printed using INT 21h, AH = 09h with DS:DX = m. What appears on the screen?
C: The service stops at the first $ (24h) character and does not print the $; the " TL$" part is never read. To print a $ sign, use AH = 02h and DL = 24h, or write the text character by character with INT 10h/0Eh.
Coding Question · INT 21h DOS
Question 17: String, Key and Exit
Write a .COM program that, using INT 21h/09h, prints Done! on the screen, waits for a key press, and ends with INT 21h/4Ch, returning exit code 0.
Show answer
org 100h mov ah, 9 lea dx, msg int 21h ; print string mov ah, 0 int 16h ; wait for a key mov ah, 4Ch mov al, 0 int 21h ; terminate program msg db 'Done!$'
09h prints the string at DS:DX up to the '$' character; if '$' is forgotten, the bytes after it are printed too. In a .COM program DS is already set up. 4Ch ends the program with the exit code in AL; the data is not executed because it sits after the RET/exit.
Output Question · INT 1Ah and BIOS Data
Question 18: BCD or Binary?
The time is 14:23. The minutes are read from two sources and printed with the wrong routine. What appears on the screen?
- CL from INT 1Ah/02h is printed with the AAM-based
print2routine (aam·add ax, 3030h· 02h twice). - CL from INT 21h/2Ch is printed with the packed-BCD routine (
shr dl, clfor the upper digit,and dl, 0Fhfor the lower digit).
Show answer
- The RTC gives packed BCD: CL = 23h. AAM treats this as binary 35: AH = 3, AL = 5 → the screen shows 35.
- DOS gives binary: CL = 23 = 17h. The BCD routine splits the hex digits → the screen shows 17.
Correct: print the RTC value with SHR/AND and the DOS value with AAM (or division by 10). Both routines give the same result for values from 0 to 9, so the bug shows only from 10 upward.
Multiple Choice · INT 1Ah and BIOS Data
Question 19: 10 Seconds with the Tick Counter
The tick counter at 0040:006Ch increments about 18.2 times per second. How many ticks must be added to the starting value for a 10-second wait?
A: The 8253/8254 timer divides 1,193,182 Hz by 65,536: 18.2065 Hz. 10 × 18.2065 ≈ 182 ticks. A tick is ≈ 54.9 ms, so 549 ticks is about 30 seconds; 18 ticks is about 1 second.
Multiple Choice · INT 1Ah and BIOS Data
Question 20: Keys Waiting in the Keyboard Buffer
In the BIOS data area, 0040:001Ah (buffer head) = 0024h and 0040:001Ch (buffer tail) = 0028h. How many keys are waiting to be read in the keyboard buffer?
B: Each key takes one word in the buffer (ASCII + scan code): (0028h − 0024h) ÷ 2 = 2 keys. If head = tail, the buffer would be empty. The buffer is a circular area of 16 words and holds at most 15 keys. The physical address of 0040:001Ah is 0041Ah.
Multiple Choice · Program Termination
Question 21: Termination and Exit Code
Which of the following calls terminates the program and passes the exit code in AL to DOS?
D: AH = 4Ch works in both .COM and .EXE programs, closes open files and leaves AL as the exit code (ERRORLEVEL in a batch file). INT 16h/00h waits for a key, INT 21h/09h prints a string, INT 10h/AX = 0003h selects the video mode and clears the screen. RET terminates only a .COM program with a balanced stack, and INT 20h only when CS points to the PSP.
Spot the Bug · Application
Question 22: The Lost Key
The program wants to print the key it read again on a new line. The user presses k. What appears on the screen? Where is the bug?
mov ah, 01h int 21h ; AL = key mov dl, 0Dh ; CR mov ah, 02h int 21h mov dl, 0Ah ; LF int 21h mov dl, al ; print the key again int 21h ret
Show answer
Only the echoed k appears, the cursor moves down two rows; the second 'k' is not printed.
On return, INT 21h/02h sets AL = DL: after the last call AL = 0Ah. mov dl, al takes the LF, not the key, and one more LF is printed.
Fix: save the key that was read in another register right away (mov bl, al), then mov dl, bl is used. Never keep a value you will need later in registers that a service modifies.
Step-by-Step Trace · Application
Question 23: Binary Printing with TEST and SHL
BL = B5h. In each round the program tests the most significant bit of BL and prints '0' or '1'. What is the output?
BL = B5h = 1011 0101b. CX = 8: one round per bit.
Round 1: TEST bit 7 = 1 → ZF = 0, JZ does not jump, DL = '1'. INT 21h prints '1'; SHL moves the bit that falls out into CF (1). LOOP: CX = 7 ≠ 0 → back to print.
Round 2: TEST bit 7 = 0 → ZF = 1, JZ jumps, DL stays '0'. INT 21h prints '0'; SHL moves the bit that falls out into CF (0). LOOP: CX = 6 ≠ 0 → back to print.
Round 3: TEST bit 7 = 1 → ZF = 0, JZ does not jump, DL = '1'. INT 21h prints '1'; SHL moves the bit that falls out into CF (1). LOOP: CX = 5 ≠ 0 → back to print.
Round 4: TEST bit 7 = 1 → ZF = 0, JZ does not jump, DL = '1'. INT 21h prints '1'; SHL moves the bit that falls out into CF (1). LOOP: CX = 4 ≠ 0 → back to print.
Round 5: TEST bit 7 = 0 → ZF = 1, JZ jumps, DL stays '0'. INT 21h prints '0'; SHL moves the bit that falls out into CF (0). LOOP: CX = 3 ≠ 0 → back to print.
Round 6: TEST bit 7 = 1 → ZF = 0, JZ does not jump, DL = '1'. INT 21h prints '1'; SHL moves the bit that falls out into CF (1). LOOP: CX = 2 ≠ 0 → back to print.
Round 7: TEST bit 7 = 0 → ZF = 1, JZ jumps, DL stays '0'. INT 21h prints '0'; SHL moves the bit that falls out into CF (0). LOOP: CX = 1 ≠ 0 → back to print.
Round 8: TEST bit 7 = 1 → ZF = 0, JZ does not jump, DL = '1'. INT 21h prints '1'; SHL moves the bit that falls out into CF (1). LOOP: CX = 0 → the loop ends.
The suffix 'b' is printed at the end: the screen shows 10110101b. TEST does not change BL; it is SHL that deliberately shifts the bits. INT 21h/02h changes AL (AL = DL).
org 100h mov bl, 0B5h mov cx, 8 print: mov ah, 2 mov dl, '0' test bl, 10000000b jz zero mov dl, '1' zero: int 21h shl bl, 1 loop print mov dl, 'b' int 21h ret