Chapter 5.2: Interrupt Functions

Microprocessors
BIOS and DOSCalling a serviceINT 10h videoThe attribute byteINT 16h keyboardINT 21h character I/OStrings and line inputDate and timeINT 1Ah and the BIOS data areaMode 13h graphicsEnding a programExample: printing binary
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Calling services

Call BIOS and DOS services with a function number in AH and parameters in registers.

Screen output

Print characters, colored characters and strings with INT 10h and INT 21h; position the cursor.

Keyboard input

Read keys with INT 16h and INT 21h, check for a key without waiting, read a line.

Clock and exit

Read and print the date and time; end a program correctly with RET, INT 20h or INT 21h/4Ch.

Introduction

BIOS and DOS Services

Application programmov ah, 09h / int 21hDOS services: INT 20h, INT 21hfiles, strings, time/date, exitBIOS services: INT 10h, 13h, 16h, 1Ahdevice routines in ROMHardwarevideo card, keyboard controller, disk, clockdirect portaccess (IN/OUT)
  • BIOS (Basic Input/Output System): routines in ROM on the motherboard that drive the hardware directly. They work without an operating system and fill the interrupt vector table at boot.
  • DOS sits on top of the BIOS and provides file, string, clock and program management. For its own work it usually calls the BIOS.
  • Both layers are reached through software interrupts (INT n); the caller does not need to know the service’s address.
The same job can be done at different layers: a character on screen via INT 21h/02h (DOS), INT 10h/0Eh (BIOS), or by writing straight into B800h video memory. Higher layers are more portable; lower layers are faster and more flexible.

Introduction

The Service Call Pattern

  1. AH ← service number (8 bits: at most 256 services)
  2. Other parameters: AL, BX, CX, DX, DS:DX, ES:BP …
  3. INT n (10h, 16h, 21h …)
  4. Results come back in registers: AL, AX, CX, DX, ZF/CF …
mov ah, 02h    ; service: print char
mov dl, 'A'    ; parameter
int 21h        ; call DOS
InterruptProviderPurpose
INT 10hBIOSvideo: mode, cursor, characters, strings
INT 16hBIOSkeyboard
INT 1AhBIOStick counter, real-time clock
INT 13hBIOSdisk
INT 20hDOSterminate program (COM)
INT 21hDOShundreds of services: I/O, files, clock, exit
A service may change the registers it uses (INT 21h/02h and 09h change AL, for example); keep values you need later in another register or on the stack. Some services also use AL as a subfunction (INT 10h, AX = 4F00h: VESA).

Introduction

ASCII on the Screen and Keyboard

CodeNameEffect (0Eh / 02h)
07hBELbeep
08hBScursor back one
09hTABtab (DOS)
0AhLFdown one line
0DhCRstart of line (Enter key)
1BhESCEsc key
CharacterASCIIRelation
space20h
'0' … '9'30h–39hdigit = code − 30h
'A' … 'Z'41h–5Ahbit 5 = 0
'a' … 'z'61h–7Ahbit 5 = 1 (+20h)
'$'24hend of string for service 09h
CR only (0Dh)ABCLF only (0Ah)ABCCR + LFABC
A new line takes two characters: 0Dh, 0Ah (CR LF). LF alone moves the cursor down but not back to the start of the line. Turkish letters (ç, ğ, ı, ö, ş, ü) are not in 7-bit ASCII; how they show depends on the code page (CP857).

INT 10h Video

INT 10h: BIOS Video Services

AHServiceInputOutput / note
00hset video modeAL = mode (03h: 80×25 color text)clears the screen
01hcursor shapeCH = start, CL = end scan lineCH = 20h → cursor hidden
02hset cursor positionBH = page, DH = row, DL = column
03hread cursor positionBH = pageDH, DL = position; CH, CL = shape
06hscroll window upAL = lines (0 = clear), BH = attribute, CH,CL – DH,DL = cornersclears screen/window
09hwrite char + attributeAL = char, BH = page, BL = attribute, CX = countcursor does not move
0Ahwrite char onlyAL, BH, CXkeeps the existing attribute
0Ehteletype outputAL = char, BH = pagecursor moves; CR, LF, BS, BEL are obeyed
13hwrite stringAL = mode, BH, BL, CX = length, DH,DL, ES:BP = stringAT and later BIOS

INT 10h Video

AH = 00h: Video Mode and the Text Screen

ALModeMemory
01h40×25 text, 16 colorsB800h
03h80×25 text, 16 colorsB800h
07h80×25 monochromeB000h
04h320×200, 4 colorsB800h
13h320×200, 256 colorsA000h
mov ah, 00h
mov al, 03h   ; or mov ax, 0003h
int 10h      ; clean 80×25 screen
010203040506070790510152024(DH = 1, DL = 4)DL: column 0–79 →DHrow0–24

The emu8086 screen is in 80×25 text mode by default; graphics modes have limited support.

INT 10h Video

Cursor Position: AH = 02h / 03h

  • Row in DH (0–24), column in DL (0–79); the top-left corner is (0, 0).
  • BH = display page. 80×25 mode has 8 pages (0–7); page 0 is the default.
  • Both in one instruction: mov dx, 0C28h → DH = 12, DL = 40 (the middle of the screen).
  • AH = 03h reads the position: DH, DL on return, plus the cursor shape in CH, CL.
  • Characters written through INT 21h also start at this cursor position.
; move the cursor to the middle
mov ah, 02h
mov bh, 0
mov dx, 0C28h   ; row 12, column 40
int 10h

; move the cursor one right
mov ah, 03h
mov bh, 0
int 10h        ; DH, DL = position
inc dl
mov ah, 02h
int 10h
(12, 40) → (12, 41)DH = 0Ch = 12 (row), DL = 28h = 40 (column)read with 03h → inc dl → set with 02h: (12, 41)offset (12 × 80 + 40) × 2 = 07D0h
In video memory, the offset of cell (row, column) = (row × 80 + column) × 2. For example (1, 4) → 84 × 2 = 00A8h; the character is at the even address, the attribute at the odd one.

INT 10h Video

The Attribute Byte

B7R6G5B4I3R2G1B0blinkbackground RGBbrightforeground RGBExample: 1Eh = 0001 1110b → yellow (bright brown) on blue
0 black1 blue2 green3 cyan4 red5 magenta6 brown7 light gray8 dark gray9 light blueA light greenB light cyanC light redD light magentaE yellowF white
  • Attribute = background × 16 + foreground. 07h is the default (light gray on black), 1Fh white on blue, 4Eh yellow on red.
  • If bit 7 = 1 the character blinks (or, depending on the BIOS setting, the background is bright): 8Fh.

INT 10h Video

Attribute Explorer

The chosen colors produce the attribute byte and the INT 10h/09h call.

INT 10h Video

Writing a Character: AH = 09h or 0Eh?

FeatureAH = 09hAH = 0Eh (teletype)
InputsAL = char, BH = page, BL = attribute, CX = countAL = char, BH = page
Coloryes (BL)not in text mode (the cell keeps its color)
Cursordoes not movemoves, wraps to the next line
CR, LF, BS, BELprinted as symbols (♪, ◘ …)obeyed as controls
Scrollingnonescrolls the screen up at the last line
Typical usecolored boxes, frames, one character CX timesplain text output
AH = 09h, AL = '*', CX = 3***cursor stayed putAH = 0Eh, AL = 'A'Acursor one right
A common pattern for colored text: write the character in color with 09h (CX = 1), then move the cursor one right with 02h; or use 13h for a whole string.

INT 10h Video · Step through the code

Code Trace: Mode, Cursor, 09h and 0Eh

AH = 00h (set mode), AL = 03h (80×25 16-color text). One instruction loads both registers.

int 10h: the mode is set, the screen is cleared and the cursor goes to (0,0).

AH = 02h: set the cursor position.

BH = display page (0–7 in text mode; normally 0).

DX = 0104h → DH = 1 (row), DL = 4 (column). The top-left corner is (0,0).

The cursor moves to row 1, column 4. Nothing on the screen yet.

AH = 09h: write AL at the cursor CX times with attribute BL.

The character to write: AL = '*' (2Ah).

BL = 4Eh: background 4 (red), foreground E (yellow).

Repeat count CX = 3.

*** appears, but 09h does not move the cursor: it is still at (1,4).

AH = 0Eh: teletype output.

AL = 'A'.

'A' is written at the cursor (1,4), over the first star; the cursor moves to (1,5).

AL = 'B'. AH is still 0Eh.

'B' goes to (1,5): the screen shows AB*. In text mode 0Eh takes no attribute; the cells keep their 4Eh color.

09h does not move the cursor; after writing, advance it separately with AH = 02h.

org 100h
mov ax, 0003h    ; AH=00h, AL=03h
int 10h          ; clears screen
mov ah, 02h      ; set cursor
mov bh, 0        ; page 0
mov dx, 0104h    ; DH=1, DL=4
int 10h
mov ah, 09h      ; char + attribute
mov al, '*'
mov bl, 4Eh      ; yellow on red
mov cx, 3        ; 3 times
int 10h          ; cursor stays!
mov ah, 0Eh      ; teletype
mov al, 'A'
int 10h          ; cursor moves
mov al, 'B'
int 10h
ret

INT 10h Video

Demo: From Text to INT 10h Code

For each character of the text, the MOV AL / INT 10h pair and its ASCII code are generated.

INT 10h Video · Step through the code

Code Trace: A Colored String with AH = 13h

AH = 13h (write string). AL = 01h: every character gets attribute BL, and the cursor moves after writing.

BH = 0 (page), BL = 1Eh: yellow (E) on blue (1).

CX = number of characters. There is no terminator such as '$'.

DH = 2, DL = 5: where writing starts.

The string’s address must be in ES:BP (not DS:DX!). In a COM program ES = DS: BP = 0112h.

Welcome is written from row 2, column 5; the cursor goes to (2,12).

With AL = 00h the string would still be written but the cursor would stay put. With AL = 02h/03h the string is made of character, attribute pairs.

org 100h
mov ax, 1301h    ; AH=13h, AL=01h
mov bx, 001Eh    ; BH=0, BL=1Eh
mov cx, 7        ; string length
mov dx, 0205h    ; row 2, col 5
lea bp, msg      ; ES:BP → string
int 10h
ret
msg db 'Welcome'

INT 10h Video

AH = 06h: Scrolling a Window and Clearing the Screen

RegisterMeaning
ALnumber of lines to scroll; 0 = clear the window
BHattribute for the blanked lines
CH, CLtop-left corner (row, column)
DH, DLbottom-right corner (row, column)
  • AH = 07h does the same, scrolling down.
  • The cursor stays put; after clearing, move it to (0,0) with 02h.
; clear the whole screen to blue
mov ax, 0600h   ; AH=06h, AL=0
mov bh, 1Fh     ; blue/white
mov cx, 0000h   ; (0,0)
mov dx, 184Fh   ; (24,79)
int 10h

; a 10×40 red box in the middle
mov ax, 0600h
mov bh, 4Fh
mov cx, 0714h   ; (7,20)
mov dx, 103Bh   ; (16,59)
int 10h
184Fh = row 24 (18h), column 79 (4Fh). Selecting mode 03h again also clears the screen, but resets the colors to the default (07h).

INT 10h Video · Step through the code

Clearing the Screen and a Window

org 100h
mov ax, 0600h   ; AH=06h, AL=0
mov bh, 1Fh     ; blue/white
mov cx, 0000h   ; (0,0)
mov dx, 184Fh   ; (24,79)
int 10h
mov ax, 0600h
mov bh, 4Fh     ; red/white
mov cx, 0714h   ; (7,20)
mov dx, 103Bh   ; (16,59)
int 10h
ret
AX0600hBH00hCX0000hDX0000hCH,CL = (0, 0)DH,DL = (0, 0)(0,0)(24,79)

AH = 06h (scroll window up), AL = 0: clear instead of scrolling.

AX0600hBH1FhCX0000hDX0000hCH,CL = (0, 0)DH,DL = (0, 0)(0,0)(24,79)

BH = 1Fh: the attribute of the blanked cells (background 1 blue, text F white).

AX0600hBH1FhCX0000hDX0000hCH,CL = (0, 0)DH,DL = (0, 0)(0,0)(24,79)

CX = 0000h → CH = 0 (row), CL = 0 (column): the top-left corner.

AX0600hBH1FhCX0000hDX184FhCH,CL = (0, 0)DH,DL = (24, 79)(0,0)(24,79)

DX = 184Fh → DH = 18h = 24, DL = 4Fh = 79: the bottom-right corner.

AX0600hBH1FhCX0000hDX184FhCH,CL = (0, 0)DH,DL = (24, 79)(0,0)(24,79)

INT 10h: the whole 80 × 25 screen is cleared to blue. The cursor stays put.

AX0600hBH4FhCX0000hDX184FhCH,CL = (0, 0)DH,DL = (24, 79)(0,0)(24,79)

Second call: AX = 0600h, BH = 4Fh (white on red).

AX0600hBH4FhCX0714hDX184FhCH,CL = (7, 20)DH,DL = (24, 79)(0,0)(24,79)

CX = 0714h → (7, 20).

AX0600hBH4FhCX0714hDX103BhCH,CL = (7, 20)DH,DL = (16, 59)(0,0)(24,79)

DX = 103Bh → (16, 59): a box 10 rows × 40 columns.

AX0600hBH4FhCX0714hDX103BhCH,CL = (7, 20)DH,DL = (16, 59)(0,0)(24,79)

INT 10h: only this window is cleared to red; the rest stays blue.

AX0600hBH4FhCX0714hDX103BhCH,CL = (7, 20)DH,DL = (16, 59)(0,0)(24,79)

RET: the program ends.

Register values come from running the code in the interpreter; the interpreter does not simulate the screen effect of 06h, so the screen is drawn from the BIOS definition.

INT 10h Video

Output Without the BIOS: B800h Video Memory

  • In mode 03h the screen is held in 4000 bytes of memory starting at B800:0000h (80 × 25 × 2).
  • Each cell is 2 bytes: character (even address) + attribute (odd address).
  • The video card shows whatever is written there at once: much faster than an interrupt call.
  • Written as a word it is little-endian: 4E41h → 41h ('A') at the low address, 4Eh as the attribute.
; yellow-on-red 'A' at cell (1,4)
mov ax, 0B800h
mov es, ax
mov di, (1*80+4)*2  ; 00A8h
mov ax, 4E41h
mov es:[di], ax

; fill the screen with '.'
xor di, di
mov ax, 072Eh
mov cx, 2000
cld
rep stosw
41hB800:00A8 · char 'A'4EhB800:00A9 · attributemov es:[di], ax (AX = 4E41h)low byte 41h → even address (char)
Writing directly does not move the cursor and does not update the cursor position the BIOS keeps. emu8086 supports this memory too.

INT 10h Video · Step through the code

Writing Directly to Video Memory

org 100h
mov ax, 0B800h
mov es, ax
mov di, (1*80+4)*2  ; 00A8h
mov ax, 4E41h
mov es:[di], ax
xor di, di
mov ax, 072Eh
mov cx, 3           ; first 3 cells
cld
rep stosw
ret
ESB800hDI0000hAXB800hCX0000hB800:0000…000000000000B800:00A80000char · attributescreen: rows 0–1, cols 0–5

ES = B800h: the segment of the mode 03h text screen. A segment register cannot take an immediate, so it goes through AX.

ESB800hDI00A8hAXB800hCX0000hB800:0000…000000000000B800:00A80000char · attributescreen: rows 0–1, cols 0–5

Cell (1, 4): (1 × 80 + 4) × 2 = 168 = 00A8h. The product is computed at assembly time.

ESB800hDI00A8hAX4E41hCX0000hB800:0000…000000000000B800:00A80000char · attributescreen: rows 0–1, cols 0–5

AX = 4E41h: AL = 41h ('A', the character), AH = 4Eh (attribute: yellow on red).

ESB800hDI00A8hAX4E41hCX0000hB800:0000…000000000000B800:00A8414Echar · attributeAscreen: rows 0–1, cols 0–5

MOV ES:[DI], AX: little-endian → B80A8h ← 41h, B80A9h ← 4Eh. The character appears at once; the cursor stays put.

ESB800hDI0000hAX4E41hCX0000hB800:0000…000000000000B800:00A8414Echar · attributeAscreen: rows 0–1, cols 0–5

DI = 0: the top-left corner.

ESB800hDI0000hAX072EhCX0003hB800:0000…000000000000B800:00A8414Echar · attributeAscreen: rows 0–1, cols 0–5

AX = 072Eh ('.' + 07h light gray), CX = 3.

ESB800hDI0000hAX072EhCX0003hB800:0000…000000000000B800:00A8414Echar · attributeAscreen: rows 0–1, cols 0–5

CLD: DF = 0, DI counts up.

ESB800hDI0006hAX072EhCX0000hB800:0000…2E072E072E07B800:00A8414Echar · attribute...Ascreen: rows 0–1, cols 0–5

REP STOSW: ES:[DI] ← AX, DI += 2, CX times → the first 3 cells become '.'; DI = 0006h, CX = 0. (For the whole screen, CX = 2000.)

ESB800hDI0006hAX072EhCX0000hB800:0000…2E072E072E07B800:00A8414Echar · attribute...Ascreen: rows 0–1, cols 0–5

RET: the program ends.

INT 10h Video

Mode 13h: 320×200, 256 Colors

  • mov ax, 0013h / int 10h: 320 × 200 graphics mode. Video memory is A000:0000h – A000:F9FFh (64,000 bytes).
  • Each pixel is 1 byte: a color number (palette entry). The first 16 colors match the text colors: 0Eh yellow, 0Fh white.
  • 320 = 140h bytes per row: offset = y · 320 + x.
  • Example: (100, 50) → 50 · 320 + 100 = 16,100 = 3EE4h.
INT 10h / AH = 0Ch: write pixel
ALcolor
BHpage (0 in mode 13h)
CX / DXx (column 0–319) / y (row 0–199)

AH = 0Dh, with the same CX, DX, reads the pixel’s color into AL.

x=0x=1x=2x=3x=319y=00000000100020003013Fy=10140014101420143027Fy=2028002810282028303BFy=199F8C0F8C1F8C2F8C3F9FF…………⋮⋮offset = y·320 + x · orange diagonal: +141h

INT 10h Video

A Diagonal Line: Through the BIOS and Through A000h

INT 10h / 0Ch: by coordinates

mov ax, 0013h
int 10h          ; 320×200, 256 colors
xor cx, cx       ; x = 0
xor dx, dx       ; y = 0
mov bh, 0        ; page
dot: mov ax, 0C0Eh   ; AH=0Ch, AL=yellow
int 10h
inc cx           ; one right
inc dx           ; one down
cmp dx, 200
jb  dot           ; 200 pixels
Why 141h? +140h (320) goes to the same column one row down, +1 one column right: 141h = 321. The last pixel (199, 199) is at 199 · 321 = F987h. For the anti-diagonal from the top right, add 13Fh (320 − 1).

A000h memory: by offset

mov ax, 0013h
int 10h
mov ax, 0A000h
mov es, ax
xor di, di       ; (0,0) → offset 0
mov al, 0Eh      ; yellow
mov cx, 200
draw: mov es:[di], al
add di, 141h     ; 320 + 1
loop draw
mov ah, 0
int 16h          ; wait for a key
mov ax, 0003h
int 10h          ; back to text mode

Writing to memory directly is much faster than an interrupt call per pixel.

emu8086’s graphics support is limited: 0Ch draws slowly, and direct writes to A000h may not show in every version; in DOSBox they show fully.

INT 10h Video · Step through the code

Drawing a Diagonal into Mode 13h Memory

org 100h
mov ax, 0A000h
mov es, ax
xor di, di       ; (0,0) → offset 0
mov al, 0Eh      ; yellow
mov cx, 4        ; first 4 pixels
draw: mov es:[di], al
add di, 141h     ; 320 + 1
loop draw
ret
ESA000hDI0000hCX0000hAL00hDI = 0 → y = 0, x = 0320 = 140h bytes per row+141h = one down, one righttop left of screen (6 × 6 pixels)

ES = A000h: mode 13h video memory (320 × 200, 1 byte per pixel).

ESA000hDI0000hCX0000hAL00hDI = 0 → y = 0, x = 0320 = 140h bytes per row+141h = one down, one righttop left of screen (6 × 6 pixels)

DI = 0: pixel (x, y) = (0, 0), offset y · 320 + x = 0.

ESA000hDI0000hCX0004hAL0EhDI = 0 → y = 0, x = 0320 = 140h bytes per row+141h = one down, one righttop left of screen (6 × 6 pixels)

AL = 0Eh (yellow), CX = 4: draw the first 4 pixels.

ESA000hDI0141hCX0003hAL0EhDI = 321 → y = 1, x = 1320 = 140h bytes per row+141h = one down, one righttop left of screen (6 × 6 pixels)

ES:[0000h] ← 0Eh: pixel (0, 0). ADD DI, 141h → DI = 0141h (one row down + one right). LOOP: CX = 3.

ESA000hDI0282hCX0002hAL0EhDI = 642 → y = 2, x = 2320 = 140h bytes per row+141h = one down, one righttop left of screen (6 × 6 pixels)

ES:[0141h] ← 0Eh: pixel (1, 1). ADD DI, 141h → DI = 0282h (one row down + one right). LOOP: CX = 2.

ESA000hDI03C3hCX0001hAL0EhDI = 963 → y = 3, x = 3320 = 140h bytes per row+141h = one down, one righttop left of screen (6 × 6 pixels)

ES:[0282h] ← 0Eh: pixel (2, 2). ADD DI, 141h → DI = 03C3h (one row down + one right). LOOP: CX = 1.

ESA000hDI0504hCX0000hAL0EhDI = 1284 → y = 4, x = 4320 = 140h bytes per row+141h = one down, one righttop left of screen (6 × 6 pixels)

ES:[03C3h] ← 0Eh: pixel (3, 3). ADD DI, 141h → DI = 0504h (one row down + one right). LOOP: CX = 0. The loop ends.

ESA000hDI0504hCX0000hAL0EhDI = 1284 → y = 4, x = 4320 = 140h bytes per row+141h = one down, one righttop left of screen (6 × 6 pixels)

RET. With 200 passes the last pixel (199, 199) would be at 199 · 321 = F987h.

INT 10h Video

Pixel Address Explorer (320 × 200)

Click the screen to compute the offset y · 320 + x and the physical address of (x, y).

INT 16h Keyboard

INT 16h: BIOS Keyboard Services

AHServiceOutput
00hwait for a key and read itAH = scan code, AL = ASCII; the key is removed from the buffer
01his a key ready? (no wait)ZF = 1: none · ZF = 0: AX = key, not removed from the buffer
02hshift statesAL bits: 0 right Shift, 1 left Shift, 2 Ctrl, 3 Alt, 4 Scroll, 5 Num, 6 Caps, 7 Insert
KeyAH (scan)AL (ASCII)
a / A1Eh61h / 41h
102h31h
Enter1Ch0Dh
Esc01h1Bh
Space39h20h
KeyAH (scan)AL
↑ / ↓48h / 50h00h
← / →4Bh / 4Dh00h
F1 … F103Bh … 44h00h
Backspace0Eh08h
Ctrl+C2Eh03h

The scan code is the key’s physical position: 'a' and 'A' give the same scan code. Keys with no ASCII code (extended keys) return AL = 00h; the key is identified by AH. A MOV AH, 0 · INT 16h at the end of a program waits for a key so the output can be read before the screen closes.

INT 16h Keyboard

Keyboard Explorer: What Does INT 16h / AH = 00h Return?

AH (scan code)--
AL (ASCII)--
AX----

Letters, digits, arrow keys, F1–F10, Enter, Esc and Ctrl+letter are recognized. Values follow the IBM PC/AT (US) keyboard layout.

INT 16h Keyboard · Step through the code

Code Trace: Read Until Enter, Echo in Upper Case

AH = 00h: wait for a key from the keyboard buffer and take it.

The user pressed O (Shift+o): AL = 4Fh (ASCII), AH = 18h (scan code).

4Fh ≠ 0Dh.

Not Enter; carry on.

'O' (4Fh) < 'a' (61h) → CF = 1.

Not lower case → straight to echo.

AH = 0Eh (teletype). AL holds the character to print.

O on the screen.

Back for the next key.

AH = 00h: wait for a key from the keyboard buffer and take it.

The user pressed k: AL = 6Bh, AH = 25h.

6Bh ≠ 0Dh.

Carry on.

'k' ≥ 'a' → CF = 0.

May be lower case; carry on.

6Bh − 7Ah: borrow → CF = 1, ZF = 0. So 'k' < 'z'.

ja needs CF = 0 and ZF = 0; CF = 1 → no jump: lower case.

Bit 5 is cleared: 6Bh − 20h = 4Bh = 'K'.

AH = 0Eh (teletype). AL holds the character to print.

K on the screen.

Back for the next key.

AH = 00h: wait for a key from the keyboard buffer and take it.

The user pressed Enter: AL = 0Dh, AH = 1Ch.

AL = 0Dh → ZF = 1.

Enter → done.

The program ends at Enter. INT 16h/00h does not echo the key; the program did the echoing.

org 100h
read:
mov ah, 0        ; wait for key
int 16h          ; AH=scan, AL=ASCII
cmp al, 0Dh      ; Enter?
je done
cmp al, 'a'
jb echo
cmp al, 'z'
ja echo
sub al, 20h      ; lower → upper
echo:
mov ah, 0Eh
int 10h          ; print it
jmp read
done:
ret

INT 16h Keyboard · Step through the code

Code Trace: Polling Without Waiting, AH = 01h

DX = 0: this counter counts the work done while waiting.

Background work: DX = 0001h.

AH = 01h: ask for the key status (no wait).

No key in the buffer → ZF = 1.

ZF = 1 → back to poll; the program does not freeze.

Background work: DX = 0002h.

AH = 01h: ask for the key status (no wait).

Still no key → ZF = 1.

Keep polling.

Background work: DX = 0003h.

AH = 01h: ask for the key status (no wait).

A key arrived: ZF = 0, AL = 'x' (78h), AH = 2Dh. The key stays in the buffer.

ZF = 0 → leave the loop.

AH = 00h to take the key out of the buffer.

The same key is read and removed from the buffer: AL = 'x', AH = 2Dh.

Games and animations use this pattern: keep running, react when a key is pressed.

org 100h
xor dx, dx       ; work counter
poll:
inc dx           ; background work
mov ah, 01h      ; key ready?
int 16h          ; ZF=1 → none
jz poll
mov ah, 00h      ; take the key
int 16h
ret

INT 21h DOS

INT 21h: Common DOS Services

AHServiceInputOutput
01hread char with echo–AL = char
02hwrite charDL = charAL = DL
07h / 08hread char, no echo–AL (08h checks Ctrl+C)
09hwrite stringDS:DX → '$'-terminated stringAL = 24h
0Ahbuffered line inputDS:DX → bufferbuf[1] = count, buf+2 = text
2Ahget date–CX = year, DH = month, DL = day, AL = weekday (0 = Sun)
2Chget time–CH = hour, CL = min, DH = sec, DL = 1/100 s
2Bh / 2Dhset date / timeas for 2Ah / 2ChAL = 00h on success
4Chterminate programAL = exit code(does not return)

DOS services honor redirection: with program > out.txt, 02h/09h output goes to the file; BIOS INT 10h output always reaches the screen.

INT 21h DOS

INT 10h/0Eh or INT 21h/02h?

BIOS · INT 10h, AH = 0Eh
  • The character is in AL.
  • Goes straight to the video BIOS; no DOS needed.
  • Works in boot code too.
  • A color can be given (BL, in graphics modes).
DOS · INT 21h, AH = 02h
  • The character is in DL.
  • A DOS call; output can be redirected (to a file, a printer).
  • On return AL = DL.
  • Checks for Ctrl+Break.
Both print one character and advance the cursor; the choice depends on where the program will run. They can be mixed in one program.

INT 21h DOS · Step through the code

Code Trace: Read a Character, New Line, Write a Character

AH = 01h: wait for a character from the keyboard and echo it.

The user pressed k: AL = 6Bh, and 'k' appears on the screen.

Later INT 21h calls will change AL, so it is saved in BL.

AH = 02h: write the character in DL.

DL = 0Dh (carriage return).

The cursor returns to the start of the line (no visible change).

DL = 0Ah (line feed).

The cursor moves down a line. New line = CR + LF.

BL = 6Bh + 1 = 6Ch = 'l'.

DL = 'l'.

l is printed. After 02h, AL also holds the character written.

The program ends.

org 100h
mov ah, 01h      ; read + echo
int 21h          ; AL = char
mov bl, al       ; save it
mov ah, 02h      ; write char
mov dl, 0Dh      ; CR: line start
int 21h
mov dl, 0Ah      ; LF: next line
int 21h
inc bl           ; next letter
mov dl, bl
int 21h
ret

INT 21h DOS · Step through the code

Code Trace: Printing a String with AH = 09h

DX = 010Dh: the offset of the string. The segment is DS.

AH = 09h: print a '$'-terminated string.

Printing runs until '$'; 0Dh 0Ah ends the line. On return AL = 24h ('$').

Second string: DX = 011Eh.

AH has not changed, so there is no need to reload it (reloading is still a good habit).

Two lines of output. This service cannot print the '$' character itself.

org 100h
mov dx, offset msg1   ; DS:DX → string
mov ah, 09h
int 21h
mov dx, offset msg2
int 21h               ; AH still 09h
ret
msg1 db 'Welcome, 8086!', 0Dh, 0Ah, '$'
msg2 db 'Good-bye!$'

INT 21h DOS

AH = 0Ah: Buffered Line Input

0Ah+0?+1?+2?+3?+4?+5?+6maximumreadcharacters …DS:DX →

The program prepares the buffer: buffer db 10, ?, 10 dup(?). Byte 0 = at most 10 characters (Enter included).

0Ah+0?+1'A'+2?+3?+4?+5?+6maximumreadcharacters …DS:DX →

The user types A: the character goes to position 2 and is echoed. Backspace can correct mistakes.

0Ah+0?+1'A'+2'l'+3'i'+4?+5?+6maximumreadcharacters …DS:DX →

l and i are added too.

0Ah+003h+1'A'+2'l'+3'i'+40Dh+5?+6maximumreadcharacters …DS:DX →

Enter: 0Dh is stored at the end; DOS writes the count read (3, CR excluded) into byte 1 and returns.

0Ah+003h+1'A'+2'l'+3'i'+4'$'+5?+6maximumreadcharacters …DS:DX →

The text starts at buffer+2; its length is in [buffer+1]. It does not end with '$'; to print it with 09h, put '$' in place of the 0Dh.

  • DOS does the line editing itself (Backspace, Esc); the program is blocked until Enter is pressed.
  • When the buffer is full, extra keys are refused with a beep. The first byte cannot be 0.

INT 21h DOS · Step through the code

Code Trace: Read a Name, Say Hello

DS:DX points to the buffer: DX = 011Fh.

AH = 0Ah: read a line up to Enter.

The user typed Ali⏎. DOS: buffer[1] = 3, buffer[2..4] = 'Ali', buffer[5] = 0Dh. Enter prints only a CR: the cursor is at the start of the line.

BL = number of characters read = 3 (CR not counted).

BX = 0003h, for the address calculation.

buffer[3 + 2] = buffer[5]: '$' replaces the 0Dh → the string can be printed with AH = 09h.

DX = greet.

AH = 09h.

CR LF + 'Hello ' is printed (on a new line).

DX = buffer + 2: the start of the text entered (0121h).

Output: Hello Ali.

buffer[0] = 10: at most 9 characters + CR. Anything more is refused (DOS beeps).

org 100h
mov dx, offset buffer
mov ah, 0Ah          ; buffered input
int 21h
mov bl, [buffer+1]   ; count read
mov bh, 0
mov byte ptr buffer[bx+2], '$'
mov dx, offset greet
mov ah, 09h
int 21h
lea dx, [buffer+2]
int 21h
ret
buffer db 10, ?, 10 dup(?)
greet  db 0Dh, 0Ah, 'Hello $'

INT 21h DOS

AH = 2Ah / 2Ch: Date and Time

AH = 2Ah (date)
CXyear (e.g. 07EAh = 2026)
DHmonth (1–12)
DLday (1–31)
ALday of week (0 = Sunday … 6 = Saturday)
AH = 2Ch (time)
CHhour (0–23)
CLminute (0–59)
DHsecond (0–59)
DLhundredths (0–99)
; print the day name
mov ah, 2Ah
int 21h          ; AL = 0…6
mov ah, 0
mov bl, 4
mul bl           ; AX = AL × 4
mov dx, offset days
add dx, ax
mov ah, 09h
int 21h
ret
days db 'Sun$Mon$Tue$Wed$'
     db 'Thu$Fri$Sat$'
The values are binary, not BCD: hour 14 → CH = 0Eh. To print them, use AAM or divide by 10. The BIOS INT 1Ah/02h, by contrast, returns the time in BCD.

INT 21h DOS · Step through the code

Day Name from a Table: AL × 4

org 100h
mov ah, 2Ah
int 21h          ; AL = 0…6
mov ah, 0
mov bl, 4
mul bl           ; AX = AL × 4
mov dx, offset days
add dx, ax
mov ah, 09h
int 21h
ret
days db 'Sun$Mon$Tue$Wed$'
     db 'Thu$Fri$Sat$'
AL00hBL00hAX2A00hDX0000hdays (0114h): 7 × 4 bytesSun$+0Mon$+4Tue$+8Wed$+12Thu$+16Fri$+20Sat$+24screen▌

AH = 2Ah: the DOS date service.

AL03hBL00hAX2A03hDX0107hdays (0114h): 7 × 4 bytesSun$+0Mon$+4Tue$+8Wed$+12Thu$+16Fri$+20Sat$+24screen▌

Sample date Wednesday, 7 January 2026: CX = 07EAh (2026), DX = 0107h (DH = month 1, DL = day 7), AL = 3 (0 = Sunday).

AL03hBL04hAX0003hDX0107hdays (0114h): 7 × 4 bytesSun$+0Mon$+4Tue$+8Wed$+12Thu$+16Fri$+20Sat$+24screen▌

AH = 0 (AX = 0003h), BL = 4: each day name is 4 bytes including '$'.

AL0ChBL04hAX000ChDX0107hdays (0114h): 7 × 4 bytesSun$+0Mon$+4Tue$+8Wed$+12Thu$+16Fri$+20Sat$+24screen▌

MUL BL: AX = 3 × 4 = 12 = 000Ch: the offset within the table.

AL0ChBL04hAX000ChDX0114hdays (0114h): 7 × 4 bytesSun$+0Mon$+4Tue$+8Wed$+12Thu$+16Fri$+20Sat$+24screen▌

DX = days = 0114h.

AL0ChBL04hAX000ChDX0120hdays (0114h): 7 × 4 bytesSun$+0Mon$+4Tue$+8Wed$+12Thu$+16Fri$+20Sat$+24DXscreen▌

ADD DX, AX → DX = 0120h: the start of the string 'Wed$'.

AL24hBL04hAX0924hDX0120hdays (0114h): 7 × 4 bytesSun$+0Mon$+4Tue$+8Wed$+12Thu$+16Fri$+20Sat$+24DXscreenWed▌

AH = 09h, INT 21h: from DS:DX up to '$' → Wed on the screen.

AL24hBL04hAX0924hDX0120hdays (0114h): 7 × 4 bytesSun$+0Mon$+4Tue$+8Wed$+12Thu$+16Fri$+20Sat$+24DXscreenWed▌

RET: the program ends.

The date and weekday come from the system clock; the trace assumes Wednesday 07.01.2026. mov dx, offset days wipes the date in DX; save it first if you need it.

INT 21h DOS · Step through the code

Code Trace: Printing the Time as HH:MM

AH = 2Ch: read the system time.

Returned: CH = 14 (0Eh) hours, CL = 35 (23h) minutes, DH = 7 seconds, DL = 42 hundredths.

AL = hour = 0Eh.

The procedure is called: the return address is on the stack.

aam: 14 → AH = 1, AL = 4.

Both digits to ASCII: AX = 3134h.

Save in BX; INT 21h/02h changes AL.

DL = tens digit.

AH = 02h.

'1' on the screen.

DL = ones digit.

'4' on the screen → 14.

Return from the procedure.

DL = ':'.

AH = 02h.

':' on the screen.

AL = minute = 23h (35). No call has touched CL.

The same procedure for the minutes.

35 → AH = 3, AL = 5.

Both digits to ASCII: AX = 3335h.

Save in BX; INT 21h/02h changes AL.

DL = tens digit.

AH = 02h.

'3' on the screen.

DL = ones digit.

'5' on the screen → 14:35.

Return from the procedure.

RET: SP = 0000h; the INT 20h in the PSP ends the program. The screen shows 14:35.

org 100h
mov ah, 2Ch       ; get system time
int 21h           ; CH=hour CL=min
mov al, ch
call print2
mov dl, ':'
mov ah, 02h
int 21h
mov al, cl
call print2
ret
print2:           ; AL (0-99) → 2 digits
aam               ; AH=tens AL=ones
add ax, 3030h     ; ASCII
mov bx, ax
mov dl, bh
mov ah, 02h
int 21h
mov dl, bl
int 21h
ret

INT 1Ah and BIOS Data

INT 1Ah: Tick Counter and Real-Time Clock

AHFunctionReturns
00hread tick counterCX:DX = ticks since midnight, AL = midnight-rollover flag
02hread RTC timeCH = hour, CL = minute, DH = second (BCD), DL = daylight saving; CF = 1: error
04hread RTC dateCH = century, CL = year, DH = month, DL = day (BCD); CF = 1: error

The RTC is the battery-backed CMOS clock (AT and later); it keeps running while the computer is off.

Packed BCD → ASCII (CL = 35h, i.e. minute 35)

35h = 0011 0101b

Each nibble is one decimal digit: high nibble 3, low nibble 5.

shr dl, cl (CL = 4) → 03h

The high digit is shifted right. On the 8086 a shift count above 1 must be in CL.

and dl, 0Fh → 05h

The low digit is isolated.

+30h → '3' 33h, '5' 35h

Each digit becomes an ASCII digit. INT 21h/2Ch returns the same minute in binary, CL = 23h, which needs AAM instead.

Among the interrupts emu8086 supports, INT 1Ah has only 00h (tick counter); 02h works under real DOS or DOSBox. In emu8086, read the time with INT 21h/2Ch.

INT 1Ah and BIOS Data · Step through the code

Code Trace: Printing the RTC Time as HH:MM:SS

AH = 02h: read the BIOS real-time clock (RTC).

The result is packed BCD: CH = 14h (hour 14), CL = 35h (minute 35), DH = 07h (second). DL = 00h (no daylight saving).

CF = 0: the read succeeded. CF = 1 would mean the clock is not running; the program would end without printing.

CL is about to get the shift count, so the minute is copied to BL first.

CL = 4: SHR DL, CL will shift 4 bits (the 8086 has no immediate shift count above 1).

BH = hour = 14h. The procedure expects the value in BH; INT 21h/02h does not touch BH.

The procedure is called for the hour (skipping colon, straight to putbcd).

DL = BH = 14h = 0001 0100b.

shr dl, cl: 0001 0100b → 0000 0001b. DL = 01h, the high digit.

+30h: DL = 31h = '1'.

AH = 02h: write the character in DL.

'1' on the screen. DOS copies DL into AL (AL = 31h).

DL = BH = 14h again, for the second digit.

and dl, 0Fh: the high 4 bits are cleared: 0000 0100b = 04h.

+30h: DL = 34h = '4'.

'4' on the screen → 14. AH is still 02h, so no reload is needed.

Return from the procedure.

BH = minute = 35h (saved in BL).

This time the colon entry is called: it prints ':' first, then falls into putbcd.

DL = ':' (3Ah).

AH = 02h.

':' on the screen → 14:. Execution then falls through into the putbcd label.

35h → 03h (high digit).

'3' (33h) on the screen.

35h AND 0Fh = 05h.

'5' on the screen → 14:35.

Return.

BH = second = 07h. INT 21h/02h left DH alone.

colon again, for the seconds.

DL = ':'.

':' on the screen → 14:35:.

07h → 00h (high digit).

'0' (30h) on the screen.

07h AND 0Fh = 07h.

'7' on the screen → 14:35:07.

Return.

The screen shows 14:35:07. INT 21h/2Ch would return the same time in binary (CH = 0Eh, CL = 23h); in BCD the hex digits are the decimal digits.

org 100h
mov ah, 02h
int 1Ah          ; CH=hr CL=min DH=sec
jc  done         ; CF=1: clock error
mov bl, cl       ; save the minute
mov cl, 4        ; SHR count
mov bh, ch
call putbcd      ; "14"
mov bh, bl
call colon       ; ":35"
mov bh, dh
call colon       ; ":07"
done: ret
colon: mov dl, ':'
mov ah, 02h
int 21h
putbcd:          ; BH = packed BCD
mov dl, bh
shr dl, cl       ; high digit
add dl, 30h      ; ASCII digit
mov ah, 02h
int 21h
mov dl, bh
and dl, 0Fh      ; low digit
add dl, 30h
int 21h
ret

INT 1Ah and BIOS Data

The BIOS Data Area (Segment 0040h)

AddressSizeContents
0040:0010wordequipment list (the value INT 11h returns)
0040:0013wordbase memory in KB (INT 12h)
0040:0017bytekeyboard flags: bit 0 right Shift, 1 left Shift, 2 Ctrl, 3 Alt, 4 Scroll Lock, 5 Num Lock, 6 Caps Lock, 7 Insert
0040:001Awordbuffer head: offset of the next key to read
0040:001Cwordbuffer tail: offset of the next free slot (head = tail → buffer empty)
0040:001E–003D32 byteskeyboard buffer: 16 words (ASCII + scan code), circular; at most 15 keys
0040:0049bytecurrent video mode (03h, 13h, …)
0040:004Awordcolumns on screen (80 in mode 03h)
0040:00508 wordscursor position for the 8 pages (low byte column, high byte row)
0040:006Cdwordtick counter: number of ≈ 18.2 Hz interrupts since midnight
0040:0070bytemidnight-passed flag (INT 1Ah/00h returns it in AL and clears it)

Physical address = 00400h + offset: 0040:006C = 0046Ch = 0000:046C. INT 16h and INT 1Ah use this area; a program can read it directly too.

INT 1Ah and BIOS Data

Reading the BIOS Data Area

; wait about 1 second
mov ax, 0040h
mov es, ax
mov bx, es:[006Ch]  ; starting tick
wait1:
mov ax, es:[006Ch]  ; current tick
sub ax, bx          ; ticks elapsed
cmp ax, 18          ; 18 ticks ≈ 0.99 s
jb  wait1

; Caps Lock on? Any key waiting?
test byte ptr es:[0017h], 40h
jnz caps_on         ; bit 6 = 1
mov ax, es:[001Ah]  ; buffer head
cmp ax, es:[001Ch]  ; buffer tail
je  buf_empty

1,193,182 Hz ÷ 65,536 = 18.2065 Hz

With a divisor of 65,536 the 8253/8254 timer raises IRQ 0 at this rate; each interrupt adds one to 0040:006C.

1 tick = 65,536 ÷ 1,193,182 s ≈ 54.9 ms
18 ticks ≈ 0.989 s · 5 s ≈ 91 ticks

Delays are measured in ticks; the resolution is about 55 ms.

1800B0h = 1,573,040 ticks a day

When the counter reaches this value it resets and the 0040:0070 flag is set.

The difference with sub ax, bx

Only the low word is read. Because we take a difference, the result is right even if the low word wraps from FFFFh to 0000h; only the midnight reset spoils it.

emu8086 does not update the whole BIOS data area as a real PC does; the tick counter and keyboard buffer examples work fully in DOSBox.

Ending a Program

Three Ways to End a Program

MethodHow does it work?Where?
retIn a COM program the top of the stack holds 0000h; RET returns to PSP:0000h, where int 20h (CD 20) sits.COM only, with a balanced stack
int 20hDOS terminates the program; CS must point to the PSP segment.COM
mov ax, 4C00h
int 21h
Terminates with the exit code in AL and closes open files.COM and EXE · recommended
Exit codeAL = 0 means success, nonzero an error. A batch file tests it with IF ERRORLEVEL 1.
A common mistakeForget the exit instruction and the processor executes the bytes after the program (the data!) as code; the system may hang.

PSP (Program Segment Prefix): the 256-byte area DOS places in front of every program; that is why COM code starts at ORG 100h.

Ending a Program

emu8086 Templates: COM and EXE

COM (single segment)

org 100h
  mov dx, offset msg
  mov ah, 09h
  int 21h
  ret             ; or int 20h
msg db 'Welcome$'
  • CS = DS = ES = SS; no setup needed.
  • At most 64 KB (code + data + stack).
  • The file is pure machine code (no header); execution starts at 0100h.

EXE (separate segments)

.model small
.stack 100h
.data
msg db 'Welcome$'
.code
start:
  mov ax, @data
  mov ds, ax      ; set DS!
  mov dx, offset msg
  mov ah, 09h
  int 21h
  mov ax, 4C00h
  int 21h
end start
An EXE file carries a header with relocation information, names its entry point with end start, and may be larger than 64 KB. DOS sets DS to the PSP at startup; without mov ax, @data / mov ds, ax, DS:DX points to the wrong place and garbage is printed.

Ending a Program · Step through the code

Setting DS in an EXE Program

.model small
.stack 100h
.data
msg db 'Welcome$'
.code
start:
mov ax, @data
mov ds, ax      ; set DS!
mov dx, offset msg
mov ah, 09h
int 21h
mov ax, 4C00h
int 21h
end start
DS0700hAX0720hDX0000h0700h PSP (256 bytes)DS0710h stack (.stack 100h)0720h data: 'Welcome$'0721h codeCSscreen▌

At startup DS = ES = 0700h: DOS points them at the PSP. CS = 0721h (code), SS = 0710h (stack). @data is the data segment value → AX = 0720h.

DS0720hAX0720hDX0000h0700h PSP (256 bytes)0710h stack (.stack 100h)0720h data: 'Welcome$'DS0721h codeCSscreen▌

MOV DS, AX: DS = 0720h now points at the data segment. Without this line DS:DX would point into the PSP and print garbage.

DS0720hAX0720hDX0000h0700h PSP (256 bytes)0710h stack (.stack 100h)0720h data: 'Welcome$'DS0721h codeCSscreen▌

DX = offset msg = 0000h: msg is at the start of the data segment.

DS0720hAX0920hDX0000h0700h PSP (256 bytes)0710h stack (.stack 100h)0720h data: 'Welcome$'DS0721h codeCSscreen▌

AH = 09h.

DS0720hAX0924hDX0000h0700h PSP (256 bytes)0710h stack (.stack 100h)0720h data: 'Welcome$'DS0721h codeCSscreenWelcome▌

INT 21h: from DS:DX = 0720h:0000h up to '$' → Welcome.

DS0720hAX4C00hDX0000h0700h PSP (256 bytes)0710h stack (.stack 100h)0720h data: 'Welcome$'DS0721h codeCSscreenWelcome▌

AX = 4C00h, INT 21h: back to DOS with exit code 0. RET cannot be used in an EXE (no PSP address on top of the stack).

The segment values are the interpreter’s layout (PSP at 0700h); under real DOS the numbers may differ, but the order and the need to set DS do not.

Application

Add, Subtract and Print the Result

org 100h
mov al, 5       ; 00000101b
mov bl, 10      ; 0Ah = 00001010b
; 5 + 10 = 15 = 0Fh = 00001111b
add bl, al
; 15 - 1 = 14 = 0Eh = 00001110b
sub bl, 1
mov cx, 8       ; print result in binary
print:
mov ah, 2   ; print function
mov dl, '0'
test bl, 10000000b  ; test top bit
jz zero
mov dl, '1'
zero:
int 21h
shl bl, 1
loop print
; print the binary suffix b
mov dl, 'b'
int 21h
; wait for a key press
mov ah, 0
int 16h
ret
  • Computes 5 + 10 − 1 = 14 = 0Eh; the result is in BL.
  • The loop runs 8 times (CX = 8): each pass tests the most significant bit (MSB) of BL, prints '0' or '1', and shifts BL left by one.
  • Finally it prints the suffix 'b' and waits for a key. Expected output: 00001110b.

Application

Reading Bits with TEST and SHL

BL0706050413121100mask10000000AND → 00000000ZF = 1 (bit 7 = 0)

TEST BL, 10000000b: BL is ANDed with the mask but not changed. If the result is 0, bit 7 = 0 → ZF = 1 and JZ jumps.

before0706050413121100after00011100CF0bit 7 → CF0 enters on the right

SHL BL, 1: every bit moves left; bit 7 goes out into CF and a 0 enters bit 0. The next bit moves into the tested position (bit 7).

pass:12345678bit:00001110

After 8 passes BL’s bits have been printed from MSB to LSB: 00001110, then b.

Alternative: SHL BL,1 and then JC on CF. In that order, though, the shift must come before printing.

Application · Step through the code

Code Trace (1/2): The Calculation

AL = 5 = 05h = 00000101b. MOV does not affect the flags.

BL = 10 = 0Ah = 00001010b.

ADD BL, AL: BL = 0Ah + 05h = 0Fh (15). No carry (CF = 0); the result has four 1 bits → PF = 1.

SUB BL, 1: BL = 0Eh (14) = 00001110b. Three 1 bits → PF = 0. No borrow: CF = 0.

CX = 8: the LOOP counter, one pass per bit.

org 100h
mov al, 5       ; 00000101b
mov bl, 10      ; 0Ah = 00001010b
; 5 + 10 = 15 = 0Fh = 00001111b
add bl, al
; 15 - 1 = 14 = 0Eh = 00001110b
sub bl, 1
mov cx, 8       ; print result in binary

Application · Step through the code

Code Trace (2/2): Printing in Binary

BL0706050413121100CF0DL—

Pass 1: AH = 2 (DOS write character).

BL0706050413121100CF0DL'0'

Default character DL = '0' (30h).

BL0706050413121100mask 10000000b → bit 7CF0DL'0'

TEST: bit 7 = 0 → ZF = 1, CF = 0.

BL0706050413121100mask 10000000b → bit 7CF0DL'0'

ZF = 1 → JZ zero jumps; DL stays '0'.

BL0706050413121100CF0DL'0'

INT 21h: the '0' in DL is printed.

BL0706051413120100CF0DL'0'

SHL BL,1: BL = 1Ch, the bit shifted out: CF = 0.

BL0706051413120100CF0DL'0'

LOOP: CX = 7 ≠ 0 → back to print.

BL0706151413020100CF0DL'0'

Pass 2 (summary): bit = 0, '0' printed, BL = 38h. LOOP: CX = 6 ≠ 0 → back to print.

BL0716151403020100CF0DL'0'

Pass 3 (summary): bit = 0, '0' printed, BL = 70h. LOOP: CX = 5 ≠ 0 → back to print.

BL1716150403020100CF0DL'0'

Pass 4 (summary): bit = 0, '0' printed, BL = E0h. LOOP: CX = 4 ≠ 0 → back to print.

BL1716150403020100CF0DL'0'

Pass 5: AH = 2 (DOS write character).

BL1716150403020100CF0DL'0'

Default character DL = '0' (30h).

BL1716150403020100mask 10000000b → bit 7CF0DL'0'

TEST: bit 7 = 1 → ZF = 0, CF = 0.

BL1716150403020100mask 10000000b → bit 7CF0DL'0'

ZF = 0 → JZ does not jump; the next instruction runs.

BL1716150403020100mask 10000000b → bit 7CF0DL'1'

DL = '1' (31h).

BL1716150403020100CF0DL'1'

INT 21h: the '1' in DL is printed.

BL1716050403020100CF1DL'1'

SHL BL,1: BL = C0h, the bit shifted out: CF = 1.

BL1716050403020100CF1DL'1'

LOOP: CX = 3 ≠ 0 → back to print.

BL1706050403020100CF1DL'1'

Pass 6 (summary): bit = 1, '1' printed, BL = 80h. LOOP: CX = 2 ≠ 0 → back to print.

BL0706050403020100CF1DL'1'

Pass 7 (summary): bit = 1, '1' printed, BL = 00h. LOOP: CX = 1 ≠ 0 → back to print.

BL0706050403020100CF0DL'0'

Pass 8 (summary): bit = 0, '0' printed, BL = 00h. LOOP: CX = 0 → the loop ends.

BL0706050403020100CF0DL'b'

DL = 'b'. AH is still 2.

BL0706050403020100CF0DL'b'

INT 21h: the suffix 'b' is printed.

BL0706050403020100CF0DL'b'

AH = 0: wait for a key.

BL0706050403020100CF0DL'b'

INT 16h: waits for a key (so the output can be read). Enter: AH = 1Ch (scan code), AL = 0Dh.

BL0706050403020100CF0DL'b'

RET: the program ends. The screen shows 00001110b.

print:
mov ah, 2   ; print function
mov dl, '0'
test bl, 10000000b  ; test top bit
jz zero
mov dl, '1'
zero:
int 21h
shl bl, 1
loop print
; print the binary suffix b
mov dl, 'b'
int 21h
; wait for a key press
mov ah, 0
int 16h
ret

Application

Screen Output and Notes

00001110b_

emu8086 screen window (80×25)

  • The number is already binary inside; printing it means turning each bit into a '0'/'1' character.
  • TEST stores no result, so BL survives; only SHL changes it (on purpose).
  • LOOP decrements CX; do not use CX for anything else inside the loop.
  • INT 21h/02h changes AL (AL = DL); save any value kept in AL first.

Application · Step through the code

Code Trace: A Mini Menu Program

DX = address of the menu text.

AH = 09h.

The menu is printed.

AH = 01h: read one character with echo.

The user pressed 1: AL = 31h (shown on screen).

31h ≠ 'q' → ZF = 0.

Not 'q'.

AL = '1' → ZF = 1.

Equal → no jump (any other key would show the menu again).

DX = greet.

AH = 09h.

Hello! is printed on a new line.

Back to the menu.

DX = address of the menu text.

AH = 09h.

The menu is printed again.

AH = 01h: read one character with echo.

The user pressed q: AL = 71h.

AL = 'q' → ZF = 1.

→ quit.

AH = 4Ch (terminate), AL = 00h (exit code).

The program returns to DOS; batch files read the exit code through ERRORLEVEL.

org 100h
show:
mov dx, offset menu
mov ah, 09h
int 21h
mov ah, 01h       ; read choice
int 21h
cmp al, 'q'
je quit
cmp al, '1'
jne show
mov dx, offset greet
mov ah, 09h
int 21h
jmp show
quit:
mov ax, 4C00h     ; exit code 0
int 21h
menu  db 0Dh, 0Ah, '1:Hello q:Leave $'
greet db 0Dh, 0Ah, 'Hello!$'

Application

Common Mistakes

Forgotten '$'INT 21h/09h prints memory until it finds a '$': the screen fills with garbage.
DS not set (EXE)DX is right but the segment is wrong: mov ax, @data / mov ds, ax.
AL gets clobbered02h and 09h change AL; save a character you read in another register before printing.
Cursor after 09hINT 10h/09h does not move the cursor; the next output lands on top.
DS:DX with 13hThe BIOS string service expects the address in ES:BP.
The 0Ah bufferSet the first byte (capacity); the text starts at buffer+2 and does not end with '$'.
LF aloneA new line needs CR + LF (0Dh, 0Ah).
No exitForget RET/4Ch and the data is executed as instructions.

Lab · INT 10h, INT 16h, INT 21h step by step

Lab: BIOS and DOS Services

Summary

Summary: Quick Reference

TaskCallParameters
Clear the screenINT 10h / 00hAL = 03h
Position the cursorINT 10h / 02hBH = 0, DH = row, DL = column
Colored characterINT 10h / 09hAL, BH, BL = attribute, CX = count
Character (teletype)INT 10h / 0EhAL
Colored stringINT 10h / 13hAL = mode, BL, CX, DH:DL, ES:BP
Wait for / poll a keyINT 16h / 00h, 01h→ AH = scan, AL = ASCII; 01h: ZF
Read / write a characterINT 21h / 01h, 02h→ AL / DL
Write a string / read a lineINT 21h / 09h, 0AhDS:DX ('$' / buffer)
Date / timeINT 21h / 2Ah, 2Ch→ CX DH DL AL / CH CL DH DL
RTC time (BCD)INT 1Ah / 02h→ CH CL DH, CF = 1 error
Pixel (mode 13h)INT 10h / 0ChAL = color, CX = x, DX = y; or A000:(y·320 + x)
End the programINT 21h / 4Ch, INT 20h, RETAL = exit code

Summary

Exercises

  1. Using INT 10h/13h, write a program that prints "8086" in white on blue at the center of the screen (12, 38).
  2. Read two digits from the user (01h) and print their sum as a single digit (sum ≤ 9).
  3. Write a program that prints the scan code of each key in hex until Esc is pressed.
  4. Print a line read with 0Ah backwards.
  5. Print the date as DD.MM.YYYY. How would you convert the year?
Hints / answers
  1. AX = 1301h, BX = 001Fh, CX = 4, DX = 0C26h, ES:BP → '8086'.
  2. Subtract 30h from AL, add in BL, then add 30h and print with DL via 02h; print CR LF in between.
  3. INT 16h/00h; convert AH into two hex digits with an XLAT table; cmp al, 1Bh.
  4. Walk backwards from SI = buffer+2+count−1: std + lodsb + 02h, CX = count.
  5. AAM for day/month; for the year (CX), repeated DIV by 10 and the stack.

Summary

Exercises (continued)

  1. How would you print the '$' character with mov ah, 09h / int 21h?
  2. In mode 13h, what is the offset of pixel (160, 100) in the A000h segment?
  3. INT 1Ah/02h returned DH = 59h. What is printed? What value does INT 21h/2Ch return for the same second?
  4. How many ticks of the 0040:006C counter make about 5 seconds?
Hints / answers
  1. You cannot; use service 02h (DL = 24h) for '$'.
  2. 100 · 320 + 160 = 32,160 = 7DA0h.
  3. '5' and '9' → 59; 2Ch returns binary DH = 3Bh.
  4. 5 × 18.2065 ≈ 91 ticks.

Summary

References

  • Ralf Brown, Ralf Brown's Interrupt List (detailed reference for INT 10h, 16h, 21h).
  • IBM, Personal Computer AT Technical Reference, BIOS section, 1984.
  • Peter Abel, IBM PC Assembly Language and Programming, 5th ed., Prentice Hall, 2001.
  • Barry B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009.
  • emu8086 help: supported interrupt functions.
  • Ş. Ç. Öztürk, O. Ergin, Intel 8086 ile Mikroişlemci Programlamaya Giriş (in Turkish), 2009.

The End

Chapter 5.2: Interrupt Functions
Previous: Chapter 5.1: Interrupts · Next: Chapter 6: Arithmetic, Logic and Shift Instructions
1 / 1 Sercan KÜLCÜ. All rights reserved.