Chapter 10.2: Device Control
Introduction
Learning Outcomes
Know the forms and limits of the IN and OUT instructions.
Map each bit of a port to a lamp, a magnet or a status condition.
Write loops that wait for a device's ready bit.
Compute delays in microseconds for INT 15h / AH = 86h.
Program the traffic lights, stepper motor, LED display, thermometer, printer, robot and test device.
Explain how a program is loaded from ROM into RAM and run.
Introduction
7 Virtual Devices Attached to emu8086
| Device | Port(s) | Direction | Data |
|---|---|---|---|
| Traffic lights | 4 | OUT | word: 12 lamp bits |
| Stepper motor | 7 | OUT / IN | bits 0–2 magnets, bit 7 ready |
| Robot | 9, 10, 11 | OUT / IN | command, data, status |
| Test device (simple io test) | 110, 112 | IN / OUT | byte (110), word (112) |
| Thermometer and heater | 125, 127 | IN / OUT | temperature, heater on/off |
| Printer | 130 | OUT | character (via INT 21h/05h) |
| LED display | 199 | OUT | word: signed number |
Devices open from emu8086's virtual devices menu; a directive such as #start=Traffic_Lights.exe# at the top of the program makes the emulator start the device itself.
I/O Basics
Device Control
- x86 CPUs can control a wide range of devices. The basic I/O port numbers vary from system to system and can be changed with suitable hardware (e.g. an address decoder).
- In an embedded system the assembled
.binfile is burned into a Read-Only Memory (ROM) chip. - At power-up the system reads the program from ROM, loads it into RAM and runs it. Many modern devices (washing machines, printers, modems…) work this way.
I/O Basics
The IN and OUT Instructions
| Form | Meaning | Port range |
|---|---|---|
IN AL, port | read 1 byte from the port | 0–255 (immediate) |
IN AX, port | read 1 word from the port | 0–255 (immediate) |
IN AL, DX / IN AX, DX | port number in DX | 0–65,535 |
OUT port, AL / OUT port, AX | write to the port | 0–255 (immediate) |
OUT DX, AL / OUT DX, AX | port number in DX | 0–65,535 |
OUT 4, BX and IN CL, 7 are invalid.MOV DX, 300, then OUT DX, AL. All emu8086 ports in this chapter are below 255.I/O Basics
Port-Mapped and Memory-Mapped I/O
- The 8086 has a separate 64 K I/O address space.
- Accessed only with
IN/OUT; the M/IO̅ pin marks an I/O cycle. - Uses no memory addresses.
- All of emu8086's virtual devices work this way.
- Device registers live at memory addresses.
- Any MOV/ADD/CMP instruction works on them.
- Example: the PC text screen starts at B800:0000; a byte written there shows up on screen.
I/O Basics
The 8255 Programmable Peripheral Interface (PPI)
- Three 8-bit ports (A, B and C, which splits into two 4-bit halves) plus a control register; A1, A0 select the register, C̅S̅ comes from the address decoder.
- After RESET all ports are inputs; write the control word first.
| A1 A0 | Register | Address (base 310h) |
|---|---|---|
| 0 0 | port A | 310h |
| 0 1 | port B | 311h |
| 1 0 | port C | 312h |
| 1 1 | control register | 313h |
DX.I/O Basics
8255 Control Word (Mode 0)
| D7 | D6 D5 | D4 | D3 | D2 | D1 | D0 |
|---|---|---|---|---|---|---|
| 1 = mode set | group A mode 00 = mode 0 | port A | PC7–PC4 | group B mode 0 = mode 0 | port B | PC3–PC0 |
| direction bits: | 1 = input, 0 = output (D4, D3, D1, D0) | |||||
1 00 0 0 0 1 0b = 82h
Wanted: A output (LEDs/relays), B input (switches), both halves of C output, all mode 0.
D7 = 1: mode set · D6 D5 = 00 and D2 = 0: mode 0
Mode 0 is simple I/O without handshaking; modes 1 and 2 are for strobed (handshake) transfers.
D4 = 0 (A output) · D3 = 0 (C upper output)
D1 = 1 (B input) · D0 = 0 (C lower output)
Result: 1000 0010b = 82h. For A input, B output, C input you would get 1001 1001b = 99h.
If D7 = 0, it is bit set/reset (BSR):
D3–D1 = bit number in C, D0 = new value
0Fh (0000 1111b) → PC7 = 1, 0Eh → PC7 = 0. The other C bits are untouched.
mov dx, 313h ; control register mov al, 82h ; A out, B in, C out out dx, al mov dx, 311h ; port B in al, dx ; read switches mov dx, 310h ; port A out dx, al ; drive the LEDs mov dx, 313h mov al, 0Fh ; BSR: PC7 = 1 out dx, al
I/O Basics · Step through the code
8255: Control Word, Read, Write, BSR
org 100h mov dx, 313h ; control register mov al, 82h ; A,C out; B in out dx, al mov dx, 311h ; port B in al, dx ; read switches mov dx, 310h ; port A out dx, al ; drive the LEDs mov dx, 313h mov al, 0Fh ; BSR: PC7 = 1 out dx, al ret
After RESET all three ports are inputs. The switches on port B are set to 1001 0110b = 96h (port map: 311h = 96h). DX = 313h, AL = 82h.
OUT 313h ← 82h = 1 00 0 0 0 1 0b: mode 0; A output, C upper output, B input, C lower output.
DX = 311h: port B. Since 311h > 255, the port number goes in DX.
IN AL, DX: the switches are read → AL = 96h.
DX = 310h: port A.
OUT 310h ← 96h: port A is an output, so the LEDs mirror the switches (lit: bits 7, 4, 2, 1).
DX = 313h, AL = 0Fh = 0000 111 1b: D7 = 0 → not a mode word but bit set/reset (BSR).
OUT 313h ← 0Fh: D3–D1 = 111 → PC7, D0 = 1 → PC7 = 1. The port directions (82h) stay, the other C bits are kept.
Port values come from the interpreter's I/O log (switch input 96h is an example). A BSR word changes only the selected C bit.
I/O Basics
Interactive 8255 Control Word
Click a port's direction to flip it, or type a value straight into the box (e.g. 9B, 80, 0F).
Traffic Lights
Traffic Lights (Port 4)
- The junction has 4 poles with 3 lamps each: 12 lamps = bits 0–11 of the word written to port 4.
- Bit = 1 → lamp on, 0 → off. Bits 12–15 are unused.
- On each pole (k = 0…3): bit 3k red, 3k+1 amber, 3k+2 green.
- Example:
0000_0011_0000_1100b→ bits 2 and 8 (green), bits 3 and 9 (red).
| Pole | Bits |
|---|---|
| bottom right | 0 1 2 |
| top right | 3 4 5 |
| top left | 6 7 8 |
| bottom left | 9 A B |
Traffic Lights
The Situation Table
; FEDC_BA98_7654_3210 situation dw 0000_0011_0000_1100b s1 dw 0000_0110_1001_1010b s2 dw 0000_1000_0110_0001b s3 dw 0000_1000_0110_0001b s4 dw 0000_0100_1101_0011b sit_end = $ all_red equ 0000_0010_0100_1001b
| Name | Hex | Lamps |
|---|---|---|
situation | 030Ch | bottom right: green; top right: red; top left: green; bottom left: red |
s1 | 069Ah | bottom right: amber; top right: red + amber; top left: amber; bottom left: red + amber |
s2 | 0861h | bottom right: red; top right: green; top left: red; bottom left: green |
s3 | 0861h | bottom right: red; top right: green; top left: red; bottom left: green |
s4 | 04D3h | bottom right: red + amber; top right: amber; top left: red + amber; bottom left: amber |
all_red | 0249h | bottom right: red; top right: red; top left: red; bottom left: red |
s2 and s3 are identical, so the side road stays green for two intervals (10 s). s1 and s4 are the amber transitions (red + amber = “get ready”).
Traffic Lights
The States Step by Step
all_red = 0249h → bottom right: red; top right: red; top left: red; bottom left: red
situation = 030Ch → bottom right: green; top right: red; top left: green; bottom left: red
s1 = 069Ah → bottom right: amber; top right: red + amber; top left: amber; bottom left: red + amber
s2 = 0861h → bottom right: red; top right: green; top left: red; bottom left: green
s3 = 0861h → bottom right: red; top right: green; top left: red; bottom left: green
s4 = 04D3h → bottom right: red + amber; top right: amber; top left: red + amber; bottom left: amber
The program first sets every direction to red, then cycles situation → s1 → s2 → s3 → s4 → situation… at 5-second intervals.
Traffic Lights
Waiting with INT 15h / AH = 86h
; wait 5 seconds (5 million µs) mov cx, 4Ch ; 004C4B40h = 5 000 000 mov dx, 4B40h mov ah, 86h int 15h
- BIOS function INT 15h, AH = 86h: waits CX:DX microseconds.
- CX is the high word, DX the low word: a 32-bit delay.
5 s = 5 × 1,000,000 = 5,000,000 µs
Convert the time to microseconds.
5,000,000 ÷ 16 = 312,500 rem. 0
312,500 ÷ 16 = 19,531 rem. 4
19,531 ÷ 16 = 1,220 rem. 11 (B)
1,220 ÷ 16 = 76 rem. 4
76 ÷ 16 = 4 rem. 12 (C)
4 ÷ 16 = 0 rem. 4
Repeated division by 16: read the remainders from last to first → 4C4B40h.
0 0 4 C | 4 B 4 0
CX = 004Ch · DX = 4B40h
The 32-bit value 0000 0000 0100 1100 0100 1011 0100 0000b is split in two.
1 s → 1,000,000 = 000F4240h → CX = 000Fh, DX = 4240h
Another example: a 1-second delay.
Traffic Lights
Software Delays: LOOP and Clock Cycles
; ≈ 1 ms delay (4.77 MHz 8086) mov cx, 281 ; 4 clocks delay: loop delay ; 280 × 17 + 1 × 5 clocks
| Instruction | Clocks |
|---|---|
MOV CX, imm | 4 |
LOOP (CX ≠ 0, jumps) | 17 |
LOOP (CX = 0, falls through) | 5 |
Total = 4 + 17(N − 1) + 5 = 17N − 8 clocks.
4.77 MHz: 1 clock ≈ 0.2096 µs
1 ms = 4,770 clocks
Time = number of clocks ÷ clock frequency.
17N − 8 = 4,770 → N ≈ 281.06
N = 281 → 4,769 clocks ≈ 0.9998 ms
CX takes an integer, so pick the nearest value.
5 MHz: 1 ms = 5,000 clocks → N ≈ 294.6
N = 295 → 5,007 clocks ≈ 1.0014 ms
The same N = 281 lasts only 0.954 ms at 5 MHz: the delay depends on the clock speed.
Longest: CX = 0 → 65,536 passes
17 · 65,536 − 8 = 1,114,104 clocks ≈ 233.6 ms
Longer delays need nested loops. Called as a procedure, it also costs CALL (19) and RET (8) clocks.
Traffic Lights · Step through the code
Counting Clocks in a Delay Loop
org 100h mov cx, 4 ; 4 clocks delay: loop delay ; 17 clocks, last one 5 ret
CX = 4. mov cx, imm takes 4 clocks; at 4.77 MHz, 1 clock ≈ 0.2096 µs.
LOOP 1: CX = 3 ≠ 0 → jumps (back to itself): 17 clocks. Total 21.
LOOP 2: CX = 2, again 17 clocks. Total 38.
LOOP 3: CX = 1, 17 clocks. Total 55.
LOOP 4: CX = 0 → falls through: only 5 clocks. Total 60 = 17 · 4 − 8 clocks.
60 clocks ÷ 4.77 MHz ≈ 12.6 µs. With CX = N the loop takes 17N − 8 clocks; for 1 ms we found N = 281.
Clock counts come from 8086 instruction timings; the interpreter only checks CX and the jumps (emu8086 doesn't model timing).
Traffic Lights · Step through the code
The Traffic Light Program
The table is assumed at 0124h (ORG 100h; the address depends on the assembler). sit_end = 012Eh.
AX = all_red = 0249h (a constant, EQU).
Write port 4: every direction red.
SI = 0124h: start of the table (situation).
AX ← [SI] = 030Ch.
OUT 4, AX: the lamps switch to the new state (SI = 0124h).
CX = 004Ch (high word of the delay).
DX = 4B40h (low word).
AH = 86h: BIOS wait function.
INT 15h: wait 5 seconds.
SI = 0126h: next word.
CMP SI, sit_end (012Eh): SI < sit_end → CF = 1.
JB: CF = 1, back to next.
… wait and advance SI … OUT 4, AX: SI = 0126h → port 4 = 069Ah.
… wait and advance SI … OUT 4, AX: SI = 0128h → port 4 = 0861h.
… wait and advance SI … OUT 4, AX: SI = 012Ah → port 4 = 0861h.
… wait and advance SI … OUT 4, AX: SI = 012Ch → port 4 = 04D3h.
CMP SI, sit_end (012Eh): SI = sit_end → CF = 0.
JB: CF = 0, no jump: end of table.
SI = 0124h: restart the table.
JMP next: endless loop.
… wait and advance SI … OUT 4, AX: SI = 0124h → port 4 = 030Ch.
#start=Traffic_Lights.exe# mov ax, all_red out 4, ax mov si, offset situation next: mov ax, [si] out 4, ax mov cx, 4Ch ; wait 5 s mov dx, 4B40h mov ah, 86h int 15h add si, 2 ; next state cmp si, sit_end jb next mov si, offset situation jmp next
The loop never ends: in embedded systems the main program is usually an endless loop.
Traffic Lights
Interactive Traffic Bit Editor
Click a bit to turn its lamp on or off (F…0). Bits 12–15 are unused.
Stepper Motor
Stepper Motor (Port 7)
- The motor can turn clockwise (CW) or counterclockwise (CCW).
- Bits 0, 1, 2 of the byte written to port 7 switch the three electromagnets.
- A half step is 11.25°, a full step 22.5°.
- Half step: two magnets on, then one magnet on, alternately.
- Full step: one, two, two magnets and an “all off” byte, in turn.
- When the motor is ready for a new command, it sets bit 7 of the byte read from the port.
Stepper Motor
Stepper Motor Data Tables
| Table | Meaning | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
datcw | CW half step | 110 | 100 | 011 | 010 |
datccw | CCW half step | 011 | 001 | 110 | 010 |
datcw_fs | CW full step | 001 | 011 | 110 | 000 |
datccw_fs | CCW full step | 100 | 110 | 011 | 000 |
- The tables sit back to back in memory, 4 bytes each, so
add bx, 4moves to the next table. - After 32 (20h) rounds the program switches tables: CW half → CCW half → CW full → CCW full → start over.
- The bits of each byte are the magnets:
110b= magnets 2 and 1 on;000b= all off. - In the original source:
datcw db 0000_0110b, thendb 0000_0100b… on the following lines (the underscores are just for readability).
Stepper Motor
Magnet Sequences Step by Step
- Half-step sequence (datcw): two → one → two → one magnets.
- Full-step sequence (datcw_fs): one, two, two magnets and an “all off” byte.
- After each OUT the motor is busy until it reaches its new position, so the program polls bit 7 before every byte.
CW half step, byte 1: magnets on → 2, 1
CW half step, byte 2: magnets on → 2
CW half step, byte 3: magnets on → 1, 0
CW half step, byte 4: magnets on → 1
CW full step, byte 1: magnets on → 0
CW full step, byte 2: magnets on → 1, 0
CW full step, byte 3: magnets on → 2, 1
CW full step, byte 4: magnets on → none
Stepper Motor
Stepper Motor Control Program
ORG 100h steps_before_direction_change = 20h jmp start datcw db 110b,100b,011b,010b datccw db 011b,001b,110b,010b datcw_fs db 001b,011b,110b,000b datccw_fs db 100b,110b,011b,000b start: mov bx, offset datcw mov si, 0 mov cx, 0 ; round counter
- Polling (wait): bit 7 = 0 means the motor is busy; read again.
[bx][si]= [BX + SI]: BX is the table start, SI the index (0–3).- After 4 bytes, SI = 0 and CX + 1; when CX = 20h, BX moves up 4, wrapping to the first table after the last.
steps_before_direction_change = 20his a constant (like EQU) and takes no memory.
next_step: wait: in al, 7 test al, 10000000b jz wait mov al, [bx][si] out 7, al inc si cmp si, 4 jb next_step mov si, 0 inc cx cmp cx, steps_before_direction_change jb next_step mov cx, 0 add bx, 4 ; next table cmp bx, offset datccw_fs jbe next_step mov bx, offset datcw jmp next_step
Stepper Motor · Step through the code
Stepper Motor: The First Round
ORG 100h, steps_before_direction_change = 20h. Port 7 will return 00h (busy) first, then 80h (ready).
Jump over the data tables.
BX = 0102h: the datcw table (CW half step).
SI = 0: first byte of the table.
CX = 0: round counter.
IN AL, 7 → AL = 00h (bit 7 = 0, motor busy).
TEST AL, 80h → ZF = 1.
JZ: ZF = 1 → wait again.
IN AL, 7 → AL = 80h (bit 7 = 1, ready).
TEST AL, 80h → ZF = 0.
JZ: ZF = 0 → continue.
AL ← [BX+SI] = [0102h] = 110b.
OUT 7, AL: magnets 110b → the motor turns a half step (11.25°).
SI = 1.
CMP SI, 4 → CF = 1.
JB: SI < 4 → next_step.
IN AL, 7 → AL = 80h (bit 7 = 1, ready).
TEST AL, 80h → ZF = 0.
JZ: ZF = 0 → continue.
AL ← [BX+SI] = [0103h] = 100b.
OUT 7, AL: magnets 100b → the motor turns a half step (11.25°).
SI = 2.
CMP SI, 4 → CF = 1.
JB: SI < 4 → next_step.
IN AL, 7 → AL = 80h (bit 7 = 1, ready).
TEST AL, 80h → ZF = 0.
JZ: ZF = 0 → continue.
AL ← [BX+SI] = [0104h] = 011b.
OUT 7, AL: magnets 011b → the motor turns a half step (11.25°).
SI = 3.
CMP SI, 4 → CF = 1.
JB: SI < 4 → next_step.
IN AL, 7 → AL = 80h (bit 7 = 1, ready).
TEST AL, 80h → ZF = 0.
JZ: ZF = 0 → continue.
AL ← [BX+SI] = [0105h] = 010b.
OUT 7, AL: magnets 010b → the motor turns a half step (11.25°).
SI = 4.
CMP SI, 4 → CF = 0.
JB: SI = 4: end of table.
SI = 0: read the table from the start.
CX = 1: one round (4 steps) done.
CMP CX, 20h → CF = 1 (not yet 32 rounds).
JB next_step: carry on with the same table. After 32 rounds, add bx, 4 moves to the next table.
jmp start ; tables 0102h-0111h start: mov bx, offset datcw mov si, 0 mov cx, 0 ; round counter next_step: wait: in al, 7 test al, 10000000b jz wait mov al, [bx][si] out 7, al inc si cmp si, 4 jb next_step mov si, 0 inc cx cmp cx, steps_before_direction_change jb next_step
Stepper Motor
Real Hardware: A Unipolar Stepper and the ULN2003
- A unipolar motor has two center-tapped windings: four half-windings A, B, A̅, B̅; the center taps go to +V (5 or 6 wires).
- The winding current (hundreds of mA) is far more than a port pin can drive, so a ULN2003 goes in between: 7 Darlington channels, up to 500 mA each.
- Input = 1 → the output pulls to ground → current flows through that half-winding.
- The back-EMF when current is cut is clamped by the ULN2003's internal diodes, with COM tied to +V.
- The motor runs open loop: there is no “ready” bit, and the delay between steps sets the speed. Too short a delay and it misses steps.
Stepper Motor
Real Motor: Drive Sequences
| Drive | Port A sequence (B̅ A̅ B A = bits 3…0) |
|---|---|
| One phase (wave), full step | 01h 02h 04h 08h |
| Two phase, full step | 03h 06h 0Ch 09h |
| Half step | 01h 03h 02h 06h 04h 0Ch 08h 09h |
- With two-phase drive, two half-windings always carry current: more torque.
- Half step interleaves the two sequences: the step angle halves (e.g. 1.8° → 0.9°).
- Play the sequence backwards and the motor turns the other way.
- emu8086's virtual motor is a simple 3-magnet model (bits 0–2, bit 7 ready); its tables (110b, 100b, …) don't apply to a real 4-phase motor.
; assume port A set to output with 82h fwd: inc pos ; reverse: dec pos mov bx, pos and bx, 7 ; index 0–7 mov al, half[bx] mov dx, 310h ; port A out dx, al call delay ; step gap = speed jmp fwd delay: ; ≈ 10 ms delay mov cx, 2500 loop $ ret half db 01h,03h,02h,06h db 04h,0Ch,08h,09h pos dw 0
Stepper Motor · Step through the code
Real Motor: Driving the Half-Step Sequence
org 100h fwd: inc pos ; reverse: dec pos mov bx, pos and bx, 7 ; index 0–7 mov al, half[bx] mov dx, 310h ; port A out dx, al call delay ; step gap jmp fwd delay: ret ; (delay) half db 01h,03h,02h,06h db 04h,0Ch,08h,09h pos dw 6
Windings: bit 0 = A, bit 1 = B, bit 2 = A̅, bit 3 = B̅. With pos = 6, INC → 7; AND BX, 7 keeps the index in 0–7 → BX = 7.
AL ← half[7] = 09h = 1001b → windings B̅ and A: the field swings between them (≈ 315°). Written to port A.
CALL delay / RET: the delay between steps sets the motor speed. JMP fwd.
pos = 8 → AND 7 → BX = 0: the sequence wraps around.
half[0] = 01h → A only: the field is at 0° (a half step).
Wait and repeat.
pos = 9 → BX = 1.
half[1] = 03h → A + B: 45°. Each OUT advances the field one half step (45° winding angle); read the table backwards and the motor reverses.
The bytes written to port 310h (09h, 01h, 03h …) come from the interpreter's I/O log; delay is just a RET here.
LED Display
LED Display (Port 199)
- The word written to port 199 is shown as a 5-digit decimal number.
- The value is read as signed (two's complement): −32,768 … 32,767.
- −5678 = 10000h − 162Eh = E9D2h; the display shows -5678.
- Only
OUT 199, AXis needed (199 ≤ 255, so an immediate port works). #make_bin#: produces a plain binary (.bin) file that doesn't depend on DOS.
LED Display · Step through the code
Driving the LED Display
The display starts at 00000.
AX = 1234 (decimal) = 04D2h.
OUT 199, AX: display shows 1234.
AX = −5678 = E9D2h (two's complement).
OUT 199, AX: display shows -5678.
AX = 0: the counter.
OUT 199, AX: display shows 0.
AX = 1.
JMP x1: endless loop (the HLT below is never reached).
OUT 199, AX: display shows 1.
AX = 2.
JMP x1: endless loop (the HLT below is never reached).
OUT 199, AX: display shows 2.
AX = 3.
JMP x1: endless loop (the HLT below is never reached); after 32767 the counter wraps to −32768.
#start=led_display.exe# mov ax, 1234 out 199, ax mov ax, -5678 out 199, ax ; write the port in an endless loop: mov ax, 0 x1: out 199, ax inc ax jmp x1 hlt ; never runs
With emu8086's step delay at 0 the numbers fly by; add an INT 15h/86h wait to the loop.
LED Display
Real Hardware: The 7-Segment Display
- Common cathode (CC): common pin to ground; bit = 1 lights a segment.
- Common anode (CA): common pin at +5 V; bit = 0 lights it, so the code is the inverse of the CC code (
NOT AL). - Each segment gets a current-limiting resistor.
- emu8086's port 199 LED display decodes the number itself; with a real display, the program produces the code.
| Digit | Lit segments | CC | CA |
|---|---|---|---|
| 0 | a b c d e f | 3Fh | C0h |
| 1 | b c | 06h | F9h |
| 2 | a b d e g | 5Bh | A4h |
| 3 | a b c d g | 4Fh | B0h |
| 4 | b c f g | 66h | 99h |
| 5 | a c d f g | 6Dh | 92h |
| 6 | a c d e f g | 7Dh | 82h |
| 7 | a b c | 07h | F8h |
| 8 | a b c d e f g | 7Fh | 80h |
| 9 | a b c d f g | 6Fh | 90h |
LED Display
Code Table, XLAT and Multiplexed Scanning
; digit (0–9) to segment code mov bx, offset segtab mov al, 3 ; digit xlat ; AL = [BX + AL] = 4Fh ; not al ; if common anode mov dx, 310h ; 8255 port A out dx, al ret segtab db 3Fh,06h,5Bh,4Fh,66h db 6Dh,7Dh,07h,7Fh,6Fh
- Multiplexing: the segment lines of all 4 digits share port A; each digit's common pin goes to port C through its own transistor.
- Only one digit is lit at a time; ≈ 4 ms per digit → a full pass every 16 ms ≈ 62 Hz. The eye sees no flicker (above ≈ 50 Hz).
- Blank everything before switching digits, or the previous code shows faintly on the next digit (ghosting).
scan: mov si, 0 next_dig: mov dx, 312h ; port C: digit select mov al, 0 out dx, al ; blank all digits first mov al, digits[si] mov bx, offset segtab ; table on the left xlat mov dx, 310h ; port A: segments out dx, al mov al, select[si] ; 01h, 02h, 04h, 08h mov dx, 312h out dx, al ; only this digit lit call delay_4ms inc si cmp si, 4 jb next_dig jmp scan delay_4ms: ; ≈ 4 ms delay mov cx, 1000 loop $ ret digits db 1, 2, 3, 4 select db 01h, 02h, 04h, 08h
LED Display · Step through the code
Multiplexed Scanning: Showing 2026
org 100h mov bx, offset segtab mov si, 0 next_dig: mov dx, 312h ; port C (digit) mov al, 0 out dx, al ; all blank mov al, digits[si] xlat ; segment code mov dx, 310h ; port A (segs) out dx, al mov al, select[si] mov dx, 312h out dx, al ; one digit lit inc si cmp si, 4 jb next_dig ret segtab db 3Fh,06h,5Bh,4Fh,66h db 6Dh,7Dh,07h,7Fh,6Fh digits db 2, 0, 2, 6 select db 01h, 02h, 04h, 08h
BX = table start, SI = 0. The number to show is 2026 (the digits array); all 4 digits share their segment lines on port A.
Digit 1: first port C ← 00h (all blank, no ghosting). AL = digits[0] = 2; XLAT → AL = 5Bh.
Port A ← 5Bh (segments), port C ← 01h: only digit 1 is lit. INC SI, CMP, JB → next digit.
Digit 2: first port C ← 00h (all blank, no ghosting). AL = digits[1] = 0; XLAT → AL = 3Fh.
Port A ← 3Fh (segments), port C ← 02h: only digit 2 is lit. INC SI, CMP, JB → next digit.
Digit 3: first port C ← 00h (all blank, no ghosting). AL = digits[2] = 2; XLAT → AL = 5Bh.
Port A ← 5Bh (segments), port C ← 04h: only digit 3 is lit. INC SI, CMP, JB → next digit.
Digit 4: first port C ← 00h (all blank, no ghosting). AL = digits[3] = 6; XLAT → AL = 7Dh.
Port A ← 7Dh (segments), port C ← 08h: only digit 4 is lit. INC SI, CMP, JB → SI = 4, the loop ends.
Each of the four digits lit once in turn. At ≈ 4 ms per digit a full pass takes 16 ms (≈ 62 Hz): the eye sees all four at once.
Common-cathode codes; port values from the interpreter's I/O log. In practice each digit stays lit for ≈ 4 ms.
Thermometer
Thermometer and Heater (Ports 125, 127)
IN AL, 125: reads the current air temperature (°C, a signed byte).OUT 127, AL: AL = 1 turns the heater on, AL = 0 turns it off.- With the heater on, the temperature slowly rises; with it off, it falls.
- Goal: a thermostat that keeps the temperature within 60–80 °C.
- The temperature can be negative, so comparisons use signed jumps (
JL,JLE,JG).
Thermometer · Step through the code
The Thermostat Program
Port 125 will return 55, 70 and 85 °C in turn. The heater starts off.
AX = CS = 0700h.
DS = 0700h (an immediate can't go straight into a segment register).
IN AL, 125 → temperature 55 °C.
CMP AL, 60: 55 − 60 → SF = 1, OF = 0.
JL (SF ≠ OF): 55 < 60 → low.
AL = 1.
OUT 127, AL: heater on.
JMP ok.
JMP start: new reading.
IN AL, 125 → temperature 70 °C.
CMP AL, 60: 70 − 60 → SF = 0, OF = 0.
JL (SF ≠ OF): no jump.
CMP AL, 80 → ZF = 0, SF = 1.
JLE: 60 ≤ 70 ≤ 80 → ok (leave the heater alone).
JMP start: new reading.
IN AL, 125 → temperature 85 °C.
CMP AL, 60: 85 − 60 → SF = 0, OF = 0.
JL (SF ≠ OF): no jump.
CMP AL, 80 → ZF = 0, SF = 0.
JLE: no jump.
JG: 85 > 80 → high.
AL = 0.
OUT 127, AL: heater off.
JMP start: new reading.
; make DS equal to CS: mov ax, cs mov ds, ax start: in al, 125 cmp al, 60 jl low cmp al, 80 jle ok jg high low: mov al, 1 out 127, al ; heater on jmp ok high: mov al, 0 out 127, al ; heater off ok: jmp start ; endless loop
jg high could just as well be jmp high: if JLE didn't jump, the temperature is already above 80.
Thermometer
Interactive Thermostat
Printer
Printer (Port 130)
- The emu8086 printer sits on port 130, but instead of writing the port directly the program uses a DOS function:
INT 21h,AH = 05h: sends the character in DL to the printer (PRN).- Control characters drive the printer:
| Code | Name | Effect |
|---|---|---|
| 0Ah (10) | LF | one line down |
| 0Dh (13) | CR | back to line start |
| 09h (9) | HT | horizontal tab |
| 0Ch (12) | FF | new page (form feed) |
db 13, 9: 13 = CR (carriage return), 9 = horizontal tab (HT). Vertical tab (VT) is 11 = 0Bh.0Ah, 0Dh is LF + CR; the usual order is CR + LF, but together they give the same result.Printer
Printer Program: Data
org 100h jmp start msg db "Hello, Printer!", 0Ah, 0Dh db "***************" db 13, 9 ; CR and horizontal tab (HT) db "Have a nice printing day!" msg_end db 0 msg2 db "press any key to eject the page.$"
offset msg_end - offset msg is computed at assembly time: 15 + 2 + 15 + 2 + 25 = 59 = 3Bh bytes.'$' terminates it: INT 21h / AH = 09h prints up to the $.Printer · Step through the code
Printer Program: The Print Loop
The output panel shows the printer paper: LF moves down a line, CR returns to the line start, TAB moves to the next 8-column stop.
DL = 12 (0Ch): form feed.
AH = 05h: DOS printer output.
INT 21h: the printer starts a new page.
SI = 0102h: address of msg.
CX = 59 (003Bh): number of characters.
DL ← [SI] = 'H'.
AH = 05h: DOS printer output.
INT 21h: 'H' goes to the printer.
SI = 0103h.
LOOP: CX = 58 → continue.
DL ← [SI] = 'e'.
AH = 05h: DOS printer output.
INT 21h: 'e' goes to the printer.
SI = 0104h.
LOOP: CX = 57 → continue.
DL ← [SI] = 'l'.
AH = 05h: DOS printer output.
INT 21h: 'l' goes to the printer.
SI = 0105h.
LOOP: CX = 56 → continue.
… INT 21h: 'l' printed (CX = 56).
… INT 21h: 't' printed (CX = 48).
… INT 21h: '*' printed (CX = 40).
… INT 21h: '*' printed (CX = 32).
… INT 21h: 'a' printed (CX = 24).
… INT 21h: 'c' printed (CX = 16).
… INT 21h: 'i' printed (CX = 8).
DL ← [SI] = '!'.
AH = 05h: DOS printer output.
… INT 21h: '!' printed (CX = 1).
SI = 013Dh.
LOOP: CX = 0 → all 59 characters done.
start: mov dl, 12 ; form feed: new page mov ah, 5 int 21h mov si, offset msg mov cx, offset msg_end - offset msg print: mov dl, [si] mov ah, 5 ; DOS printer function int 21h inc si ; next character loop print
CR (13) without LF makes the third line print over the stars (shown as overwriting in the panel). After the loop, mov dx, offset msg2 / mov ah, 9 / int 21h shows a message; int 16h (AH = 0) waits for a key; a final form feed ejects the page.
Robot
Robot (Ports 9, 10, 11)
- The robot moves around a grid world; it can detect walls and lamps and switch lamps on and off.
- Port 9 is the command (OUT), port 10 data (IN), port 11 status (IN). You edit the world with the toolbox in the device window.
Robot
Robot Commands and Status Bits
| Command (port 9) | Effect |
|---|---|
| 0 | do nothing |
| 1 | move forward |
| 2 | turn left |
| 3 | turn right |
| 4 | examine what's ahead |
| 5 | switch lamp on |
| 6 | switch lamp off |
| Data (port 10) | Ahead |
|---|---|
| 255 (FFh) | wall |
| 0 | empty |
| 7 | lamp on |
| 8 | lamp off |
| Status (port 11) | If 1 |
|---|---|
| bit 0 | new data in the data register |
| bit 1 | robot busy (command running) |
| bit 2 | error in last command |
Robot
Robot Program: Find the Lamp and Switch It On
#start=robot.exe# main_loop: CALL wait_ready MOV AL, 4 ; examine OUT 9, AL CALL wait_data IN AL, 10 CMP AL, 0FFh ; wall? JE turn CMP AL, 8 ; lamp off? JNE advance CALL wait_ready MOV AL, 5 ; lamp on OUT 9, AL JMP main_loop turn: MOV AL, 3 ; turn right JMP send advance: MOV AL, 1 ; forward send: PUSH AX ; keep command CALL wait_ready POP AX OUT 9, AL JMP main_loop
- Before every command, wait until the busy bit (bit 1) is zero.
- After an examine (4), wait for the data ready bit (bit 0), then read port 10.
- Wall: turn right; unlit lamp: switch it on; otherwise move forward.
- The wait routines are called with
CALL: the return address is on the stack.
Robot
Robot: Wait Routines and Pitfalls
wait_ready PROC b1: IN AL, 11 TEST AL, 010b ; busy? JNZ b1 RET wait_ready ENDP wait_data PROC v1: IN AL, 11 TEST AL, 001b ; new data? JZ v1 RET wait_data ENDP
wait_ready overwrites AL with IN. On the send path AL holds the command, so PUSH AX before the call and POP AX after; otherwise the status byte would be sent to the port.Robot
Simple I/O Test Device (Ports 110, 112)
- Port 110: a byte; port 112: a word.
- Values typed into the window become program input (use the
hsuffix for hex); what the program writes shows up in the window. - Input ports are refreshed every 100 ms.
- Ideal for trying out and debugging IN/OUT.
IN AL, 110 ; e.g. reads A7h INC AL OUT 110, AL ; writes A8h IN AX, 112 ; e.g. 1234h ADD AX, 1 OUT 112, AX ; 1235h
Robot · Step through the code
Test Device: Byte and Word I/O
org 100h in al, 110 ; read byte inc al out 110, al ; write byte in ax, 112 ; read word add ax, 1 out 112, ax ; write word ret
Values entered in the test device window: port 110 = FFh, port 112 = 7FFFh. IN AL, 110 → AL = FFh.
INC AL: FFh + 1 = 00h (wraps in 8 bits). ZF = 1; INC leaves CF unchanged.
OUT 110, AL: the window shows port 110 = 00h.
IN AX, 112: 16-bit read → AX = 7FFFh (+32,767).
ADD AX, 1 → 8000h: two positives gave a negative sum → OF = 1, SF = 1; no unsigned carry, CF = 0.
OUT 112, AX: the window shows port 112 = 8000h.
RET: the program ends.
Input values were fed into the interpreter's port map; outputs were read from the I/O log.
Lab · IN, OUT and the emu8086 virtual devices
Lab: Port Devices
Summary
Summary
| Device | Port | Key idea |
|---|---|---|
| Traffic lights | 4 | One bit per lamp; a situation table + INT 15h/86h waits. |
| Stepper motor | 7 | Bits 0–2 magnets, bit 7 ready; half step 11.25°, full step 22.5°. |
| LED display | 199 | A word, shown as signed decimal. |
| Thermometer | 125, 127 | Read + signed compare + heater on/off. |
| Printer | 130 | INT 21h/05h; control characters LF, CR, HT, FF. |
| Robot | 9, 10, 11 | Command, data, status; poll the busy/data bits. |
| Test device | 110, 112 | Byte and word I/O experiments. |
| 8255 PPI (real) | 310h–313h | The control word (e.g. 82h) sets the port directions; port > 255 → DX. |
The common pattern: read status → test a bit → if ready, write/read data → loop. IN/OUT only through AL/AX, and through DX for ports > 255.
Summary
Exercises
- What port 4 value lights only the amber lamps in every direction?
- What should CX and DX be for a 2.5-second wait?
- Which hex value in AX shows −1 on the LED display?
- Change the thermostat to a 20–25 °C range. What would happen at −10 °C if you used
JB? - Which lines of the stepper program must change to turn the motor only clockwise, in full steps?
Answers
- Bits 1, 4, 7, 10 → 0000_0100_1001_0010b = 0492h.
- 2,500,000 = 002625A0h → CX = 0026h, DX = 25A0h.
- FFFFh.
cmp al, 20 / jl low / cmp al, 25 / jle ok / .... −10 = F6h; to an unsigned JB that is 246 > 20, so the heater would never switch on.mov bx, offset datcw_fs, and drop the table-switching part (inc cx…jmp next_step), puttingjmp next_stepin its place.
Summary
Exercises (continued)
- Write the shortest program that prints “Hello” on the printer and ejects the page.
- On an 8 MHz 8086, what N gives about 1 ms with
mov cx, N/loop? - What 8255 control word makes A input, B output and all of C input (mode 0)?
- On a common-anode display, what are the codes for the digit '5' and the letter 'H' (order dp g f e d c b a)?
Answers
- For each character
MOV DL, 'H'/MOV AH, 5/INT 21h(or a loop), then a form feed with DL = 12. - 17N − 8 = 8,000 → N ≈ 471 (7,999 clocks).
- 1 00 1 1 0 0 1b = 99h.
- '5': CC 6Dh → CA 92h; 'H' (b c e f g): CC 76h → CA 89h.
Summary
References
- emu8086 help: I/O ports and hardware interrupts, Virtual devices and the sample programs (traffic_lights.asm, stepper_motor.asm, led_display_test.asm, thermometer.asm, printer.asm, robot.asm).
- Intel, The 8086 Family User's Manual, 1979 — IN/OUT, the I/O address space and instruction timings.
- Intel, 8255A Programmable Peripheral Interface data sheet; Texas Instruments, ULN2003A data sheet.
- M. Bodur, Mikroişlemcilere Giriş: Assembler ile Yazılım ve Arayüz, EMO, 2014 (in Turkish).
- R. Brown, Ralf Brown's Interrupt List — INT 15h/86h, INT 21h/05h, INT 16h/00h.
- B. B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009 — Chapter 11 (basic I/O interface).