Chapter 10.2: Device Control

Microprocessors
IN / OUTPollingTraffic lightsINT 15h delaysSoftware delays8255 PPIStepper motorLED display7-segment displayThermometerPrinterRobotTest device
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Port-based I/O

Know the forms and limits of the IN and OUT instructions.

Bit-level control

Map each bit of a port to a lamp, a magnet or a status condition.

Polling

Write loops that wait for a device's ready bit.

Timing

Compute delays in microseconds for INT 15h / AH = 86h.

emu8086 devices

Program the traffic lights, stepper motor, LED display, thermometer, printer, robot and test device.

The embedded view

Explain how a program is loaded from ROM into RAM and run.

Introduction

7 Virtual Devices Attached to emu8086

DevicePort(s)DirectionData
Traffic lights4OUTword: 12 lamp bits
Stepper motor7OUT / INbits 0–2 magnets, bit 7 ready
Robot9, 10, 11OUT / INcommand, data, status
Test device (simple io test)110, 112IN / OUTbyte (110), word (112)
Thermometer and heater125, 127IN / OUTtemperature, heater on/off
Printer130OUTcharacter (via INT 21h/05h)
LED display199OUTword: 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 .bin file 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.
ROMprogram.binRAMrunning copyCPUexecutesI/O ports① read② load③ IN / OUT

I/O Basics

The IN and OUT Instructions

FormMeaningPort range
IN AL, portread 1 byte from the port0–255 (immediate)
IN AX, portread 1 word from the port0–255 (immediate)
IN AL, DX / IN AX, DXport number in DX0–65,535
OUT port, AL / OUT port, AXwrite to the port0–255 (immediate)
OUT DX, AL / OUT DX, AXport number in DX0–65,535
CPUAL / AXI/O portn or [DX]OUT port, AL/AXIN AL/AX, port
Data goes only through AL or AX: OUT 4, BX and IN CL, 7 are invalid.
For a port above 255, first 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

Port-mapped (isolated) 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.
Memory-mapped I/O
  • 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.
memory 00000h – FFFFFh (1 MB)I/O 0000h – FFFFhIN / OUT, M/IO̅ = 0B800hmemorydevice in memory: MOV, ADD, CMP … all workport-mappedmemory-mapped
Polling: the CPU reads the device's status bit in a loop until it is “ready”. Simple, but the CPU can do nothing else while waiting. The alternative is for the device to send the CPU an interrupt.

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 A0RegisterAddress (base 310h)
0 0port A310h
0 1port B311h
1 0port C312h
1 1control register313h
8255D0–D7A0A1R̅D̅ (IOR̅)W̅R̅ (IOW̅)RESETaddress decoderC̅S̅PA0–PA7PB0–PB7PC4–PC7PC0–PC3port Aport Bport C upperport C lower
On the 8086 even addresses move over D0–D7 (low bank); to put the registers on even addresses, wire 8255.A0 ← A1, A1 ← A2 (310h, 312h, 314h, 316h). On the 8088, A0 ← A0, A1 ← A1 is enough. Port > 255: access through DX.

I/O Basics

8255 Control Word (Mode 0)

D7D6 D5D4D3D2D1D0
1 = mode setgroup A mode
00 = mode 0
port APC7–PC4group B mode
0 = mode 0
port BPC3–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
DX0313hAL82h8255control = none (all inputs)A 310hinput— (not written)B 311hinput ← switches10010110C 312hinput— (not written)I/O log:

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.

DX0313hAL82h8255control = 82hA 310houtput → LEDs— (not written)B 311hinput ← switches10010110C 312houtput— (not written)OUT → controlI/O log:OUT 313h ← 82h

OUT 313h ← 82h = 1 00 0 0 0 1 0b: mode 0; A output, C upper output, B input, C lower output.

DX0311hAL82h8255control = 82hA 310houtput → LEDs— (not written)B 311hinput ← switches10010110C 312houtput— (not written)I/O log:OUT 313h ← 82h

DX = 311h: port B. Since 311h > 255, the port number goes in DX.

DX0311hAL96h8255control = 82hA 310houtput → LEDs— (not written)B 311hinput ← switches10010110C 312houtput— (not written)IN ← port BI/O log:OUT 313h ← 82hIN 311h → 96h

IN AL, DX: the switches are read → AL = 96h.

DX0310hAL96h8255control = 82hA 310houtput → LEDs— (not written)B 311hinput ← switches10010110C 312houtput— (not written)I/O log:OUT 313h ← 82hIN 311h → 96h

DX = 310h: port A.

DX0310hAL96h8255control = 82hA 310houtput → LEDs10010110B 311hinput ← switches10010110C 312houtput— (not written)OUT → port AI/O log:OUT 313h ← 82hIN 311h → 96hOUT 310h ← 96h

OUT 310h ← 96h: port A is an output, so the LEDs mirror the switches (lit: bits 7, 4, 2, 1).

DX0313hAL0Fh8255control = 82hA 310houtput → LEDs10010110B 311hinput ← switches10010110C 312houtput— (not written)I/O log:OUT 313h ← 82hIN 311h → 96hOUT 310h ← 96h

DX = 313h, AL = 0Fh = 0000 111 1b: D7 = 0 → not a mode word but bit set/reset (BSR).

DX0313hAL0Fh8255control = 82hA 310houtput → LEDs10010110B 311hinput ← switches10010110C 312houtput10000000OUT → control (BSR)I/O log:IN 311h → 96hOUT 310h ← 96hOUT 313h ← 0Fh

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).
PoleBits
bottom right0 1 2
top right3 4 5
top left6 7 8
bottom left9 A B
0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

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
NameHexLamps
situation030Chbottom right: green; top right: red; top left: green; bottom left: red
s1069Ahbottom right: amber; top right: red + amber; top left: amber; bottom left: red + amber
s20861hbottom right: red; top right: green; top left: red; bottom left: green
s30861hbottom right: red; top right: green; top left: red; bottom left: green
s404D3hbottom right: red + amber; top right: amber; top left: red + amber; bottom left: amber
all_red0249hbottom 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

0123456789ABFEDC BA98 7654 32100000 0010 0100 1001

all_red = 0249h → bottom right: red; top right: red; top left: red; bottom left: red

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

situation = 030Ch → bottom right: green; top right: red; top left: green; bottom left: red

0123456789ABFEDC BA98 7654 32100000 0110 1001 1010

s1 = 069Ah → bottom right: amber; top right: red + amber; top left: amber; bottom left: red + amber

0123456789ABFEDC BA98 7654 32100000 1000 0110 0001

s2 = 0861h → bottom right: red; top right: green; top left: red; bottom left: green

0123456789ABFEDC BA98 7654 32100000 1000 0110 0001

s3 = 0861h → bottom right: red; top right: green; top left: red; bottom left: green

0123456789ABFEDC BA98 7654 32100000 0100 1101 0011

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,000,000 µs = 004C4B40h (32 bits)004C4B40CX = 004ChDX = 4B40hhigh wordlow word

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
InstructionClocks
MOV CX, imm4
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.

These figures are approximate: interrupts, memory wait states and the prefetch queue can stretch the time. emu8086 doesn't model instruction timing; the loop runs at the speed of your PC and the step-delay setting. In emu8086, timed waits use INT 15h/86h.

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
CX0004htotal: 4 clocks≈ 0.84 µs41717175MOVLOOP (jumps) ×3LOOP (exits)060 clocks

CX = 4. mov cx, imm takes 4 clocks; at 4.77 MHz, 1 clock ≈ 0.2096 µs.

CX0003htotal: 21 clocks≈ 4.40 µs41717175MOVLOOP (jumps) ×3LOOP (exits)060 clocks

LOOP 1: CX = 3 ≠ 0 → jumps (back to itself): 17 clocks. Total 21.

CX0002htotal: 38 clocks≈ 7.97 µs41717175MOVLOOP (jumps) ×3LOOP (exits)060 clocks

LOOP 2: CX = 2, again 17 clocks. Total 38.

CX0001htotal: 55 clocks≈ 11.53 µs41717175MOVLOOP (jumps) ×3LOOP (exits)060 clocks

LOOP 3: CX = 1, 17 clocks. Total 55.

CX0000htotal: 60 clocks≈ 12.58 µs41717175MOVLOOP (jumps) ×3LOOP (exits)060 clocks

LOOP 4: CX = 0 → falls through: only 5 clocks. Total 60 = 17 · 4 − 8 clocks.

CX0000htotal: 60 clocks≈ 12.58 µs41717175MOVLOOP (jumps) ×3LOOP (exits)060 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

0123456789ABFEDC BA98 7654 32100000 0000 0000 0000

The table is assumed at 0124h (ORG 100h; the address depends on the assembler). sit_end = 012Eh.

0123456789ABFEDC BA98 7654 32100000 0000 0000 0000

AX = all_red = 0249h (a constant, EQU).

0123456789ABFEDC BA98 7654 32100000 0010 0100 1001

Write port 4: every direction red.

0123456789ABFEDC BA98 7654 32100000 0010 0100 1001

SI = 0124h: start of the table (situation).

0123456789ABFEDC BA98 7654 32100000 0010 0100 1001

AX ← [SI] = 030Ch.

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

OUT 4, AX: the lamps switch to the new state (SI = 0124h).

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

CX = 004Ch (high word of the delay).

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

DX = 4B40h (low word).

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

AH = 86h: BIOS wait function.

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

INT 15h: wait 5 seconds.

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

SI = 0126h: next word.

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

CMP SI, sit_end (012Eh): SI < sit_end → CF = 1.

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

JB: CF = 1, back to next.

0123456789ABFEDC BA98 7654 32100000 0110 1001 1010

… wait and advance SI … OUT 4, AX: SI = 0126h → port 4 = 069Ah.

0123456789ABFEDC BA98 7654 32100000 1000 0110 0001

… wait and advance SI … OUT 4, AX: SI = 0128h → port 4 = 0861h.

0123456789ABFEDC BA98 7654 32100000 1000 0110 0001

… wait and advance SI … OUT 4, AX: SI = 012Ah → port 4 = 0861h.

0123456789ABFEDC BA98 7654 32100000 0100 1101 0011

… wait and advance SI … OUT 4, AX: SI = 012Ch → port 4 = 04D3h.

0123456789ABFEDC BA98 7654 32100000 0100 1101 0011

CMP SI, sit_end (012Eh): SI = sit_end → CF = 0.

0123456789ABFEDC BA98 7654 32100000 0100 1101 0011

JB: CF = 0, no jump: end of table.

0123456789ABFEDC BA98 7654 32100000 0100 1101 0011

SI = 0124h: restart the table.

0123456789ABFEDC BA98 7654 32100000 0100 1101 0011

JMP next: endless loop.

0123456789ABFEDC BA98 7654 32100000 0011 0000 1100

… 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.
2107654321000000110port 7 = 00000110b

Stepper Motor

Stepper Motor Data Tables

TableMeaning1234
datcwCW half step110100011010
datccwCCW half step011001110010
datcw_fsCW full step001011110000
datccw_fsCCW full step100110011000
110100011010011001110010001011110000100110011000datcw · 0102hdatccw · 0106hdatcw_fs · 010Ahdatccw_fs · 010Ehadd bx, 4 → next table
  • The tables sit back to back in memory, 4 bytes each, so add bx, 4 moves 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, then db 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.
2107654321000000110datcw[0] = 110b

CW half step, byte 1: magnets on → 2, 1

2107654321000000100datcw[1] = 100b

CW half step, byte 2: magnets on → 2

2107654321000000011datcw[2] = 011b

CW half step, byte 3: magnets on → 1, 0

2107654321000000010datcw[3] = 010b

CW half step, byte 4: magnets on → 1

2107654321000000001datcw_fs[0] = 001b

CW full step, byte 1: magnets on → 0

2107654321000000011datcw_fs[1] = 011b

CW full step, byte 2: magnets on → 1, 0

2107654321000000110datcw_fs[2] = 110b

CW full step, byte 3: magnets on → 2, 1

2107654321000000000datcw_fs[3] = 000b

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 = 20h is 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

2107654321000000000

ORG 100h, steps_before_direction_change = 20h. Port 7 will return 00h (busy) first, then 80h (ready).

2107654321000000000

Jump over the data tables.

2107654321000000000

BX = 0102h: the datcw table (CW half step).

2107654321000000000

SI = 0: first byte of the table.

2107654321000000000

CX = 0: round counter.

2107654321000000000

IN AL, 7 → AL = 00h (bit 7 = 0, motor busy).

2107654321000000000

TEST AL, 80h → ZF = 1.

2107654321000000000

JZ: ZF = 1 → wait again.

2107654321000000000

IN AL, 7 → AL = 80h (bit 7 = 1, ready).

2107654321000000000

TEST AL, 80h → ZF = 0.

2107654321000000000

JZ: ZF = 0 → continue.

2107654321000000000

AL ← [BX+SI] = [0102h] = 110b.

2107654321000000110

OUT 7, AL: magnets 110b → the motor turns a half step (11.25°).

2107654321000000110

SI = 1.

2107654321000000110

CMP SI, 4 → CF = 1.

2107654321000000110

JB: SI < 4 → next_step.

2107654321000000110

IN AL, 7 → AL = 80h (bit 7 = 1, ready).

2107654321000000110

TEST AL, 80h → ZF = 0.

2107654321000000110

JZ: ZF = 0 → continue.

2107654321000000110

AL ← [BX+SI] = [0103h] = 100b.

2107654321000000100

OUT 7, AL: magnets 100b → the motor turns a half step (11.25°).

2107654321000000100

SI = 2.

2107654321000000100

CMP SI, 4 → CF = 1.

2107654321000000100

JB: SI < 4 → next_step.

2107654321000000100

IN AL, 7 → AL = 80h (bit 7 = 1, ready).

2107654321000000100

TEST AL, 80h → ZF = 0.

2107654321000000100

JZ: ZF = 0 → continue.

2107654321000000100

AL ← [BX+SI] = [0104h] = 011b.

2107654321000000011

OUT 7, AL: magnets 011b → the motor turns a half step (11.25°).

2107654321000000011

SI = 3.

2107654321000000011

CMP SI, 4 → CF = 1.

2107654321000000011

JB: SI < 4 → next_step.

2107654321000000011

IN AL, 7 → AL = 80h (bit 7 = 1, ready).

2107654321000000011

TEST AL, 80h → ZF = 0.

2107654321000000011

JZ: ZF = 0 → continue.

2107654321000000011

AL ← [BX+SI] = [0105h] = 010b.

2107654321000000010

OUT 7, AL: magnets 010b → the motor turns a half step (11.25°).

2107654321000000010

SI = 4.

2107654321000000010

CMP SI, 4 → CF = 0.

2107654321000000010

JB: SI = 4: end of table.

2107654321000000010

SI = 0: read the table from the start.

2107654321000000010

CX = 1: one round (4 steps) done.

2107654321000000010

CMP CX, 20h → CF = 1 (not yet 32 rounds).

2107654321000000010

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.
8255ULN2003PA01B1CAPA12B2CBPA23B3CA̅PA34B4CB̅COM+V (motor supply)unipolar motorAA̅BB̅center tap

Stepper Motor

Real Motor: Drive Sequences

DrivePort A sequence (B̅ A̅ B A = bits 3…0)
One phase (wave), full step01h 02h 04h 08h
Two phase, full step03h 06h 0Ch 09h
Half step01h 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
BX0007hAL00hhalf[ ]0100310220630440C5086097port A (310h) = —ABA̅B̅

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.

BX0007hAL09hhalf[ ]0100310220630440C5086097port A (310h) = 09hABA̅B̅field ≈ 315°

AL ← half[7] = 09h = 1001b → windings B̅ and A: the field swings between them (≈ 315°). Written to port A.

BX0007hAL09hhalf[ ]0100310220630440C5086097port A (310h) = 09hABA̅B̅field ≈ 315°

CALL delay / RET: the delay between steps sets the motor speed. JMP fwd.

BX0000hAL09hhalf[ ]0100310220630440C5086097port A (310h) = 09hABA̅B̅field ≈ 315°

pos = 8 → AND 7 → BX = 0: the sequence wraps around.

BX0000hAL01hhalf[ ]0100310220630440C5086097port A (310h) = 01hABA̅B̅field ≈ 0°

half[0] = 01h → A only: the field is at 0° (a half step).

BX0000hAL01hhalf[ ]0100310220630440C5086097port A (310h) = 01hABA̅B̅field ≈ 0°

Wait and repeat.

BX0001hAL01hhalf[ ]0100310220630440C5086097port A (310h) = 01hABA̅B̅field ≈ 0°

pos = 9 → BX = 1.

BX0001hAL03hhalf[ ]0100310220630440C5086097port A (310h) = 03hABA̅B̅field ≈ 45°

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, AX is needed (199 ≤ 255, so an immediate port works).
  • #make_bin#: produces a plain binary (.bin) file that doesn't depend on DOS.
00355port 199 (2 bytes) · AX = 0163h

LED Display · Step through the code

Driving the LED Display

00000port 199 (2 bytes) · AX = 0000h

The display starts at 00000.

00000port 199 (2 bytes) · AX = 0000h

AX = 1234 (decimal) = 04D2h.

01234port 199 (2 bytes) · AX = 04D2h

OUT 199, AX: display shows 1234.

01234port 199 (2 bytes) · AX = 04D2h

AX = −5678 = E9D2h (two's complement).

-5678port 199 (2 bytes) · AX = E9D2h

OUT 199, AX: display shows -5678.

-5678port 199 (2 bytes) · AX = E9D2h

AX = 0: the counter.

00000port 199 (2 bytes) · AX = 0000h

OUT 199, AX: display shows 0.

00000port 199 (2 bytes) · AX = 0000h

AX = 1.

00000port 199 (2 bytes) · AX = 0000h

JMP x1: endless loop (the HLT below is never reached).

00001port 199 (2 bytes) · AX = 0001h

OUT 199, AX: display shows 1.

00001port 199 (2 bytes) · AX = 0001h

AX = 2.

00001port 199 (2 bytes) · AX = 0001h

JMP x1: endless loop (the HLT below is never reached).

00002port 199 (2 bytes) · AX = 0002h

OUT 199, AX: display shows 2.

00002port 199 (2 bytes) · AX = 0002h

AX = 3.

00002port 199 (2 bytes) · AX = 0002h

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

abcdefgdpD70dpD61gD50fD40eD31dD21cD11bD01acommon cathode '3': 4Fhcommon anode '3': B0h (inverse)PA7 … PA0 → dp g f e d c b a
  • 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.
DigitLit segmentsCCCA
0a b c d e f3FhC0h
1b c06hF9h
2a b d e g5BhA4h
3a b c d g4FhB0h
4b c f g66h99h
5a c d f g6Dh92h
6a c d e f g7Dh82h
7a b c07hF8h
8a b c d e f g7Fh80h
9a b c d f g6Fh90h

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
SI0000hAL00hport A = —port C = —digit 1digit 2digit 3digit 4

BX = table start, SI = 0. The number to show is 2026 (the digits array); all 4 digits share their segment lines on port A.

SI0000hAL5Bhport A = —port C = 00hdigit 1digit 2digit 3digit 4

Digit 1: first port C ← 00h (all blank, no ghosting). AL = digits[0] = 2; XLAT → AL = 5Bh.

SI0001hAL01hport A = 5Bhport C = 01hdigit 1digit 2digit 3digit 4

Port A ← 5Bh (segments), port C ← 01h: only digit 1 is lit. INC SI, CMP, JB → next digit.

SI0001hAL3Fhport A = 5Bhport C = 00hdigit 1digit 2digit 3digit 4

Digit 2: first port C ← 00h (all blank, no ghosting). AL = digits[1] = 0; XLAT → AL = 3Fh.

SI0002hAL02hport A = 3Fhport C = 02hdigit 1digit 2digit 3digit 4

Port A ← 3Fh (segments), port C ← 02h: only digit 2 is lit. INC SI, CMP, JB → next digit.

SI0002hAL5Bhport A = 3Fhport C = 00hdigit 1digit 2digit 3digit 4

Digit 3: first port C ← 00h (all blank, no ghosting). AL = digits[2] = 2; XLAT → AL = 5Bh.

SI0003hAL04hport A = 5Bhport C = 04hdigit 1digit 2digit 3digit 4

Port A ← 5Bh (segments), port C ← 04h: only digit 3 is lit. INC SI, CMP, JB → next digit.

SI0003hAL7Dhport A = 5Bhport C = 00hdigit 1digit 2digit 3digit 4

Digit 4: first port C ← 00h (all blank, no ghosting). AL = digits[3] = 6; XLAT → AL = 7Dh.

SI0004hAL08hport A = 7Dhport C = 08hdigit 1digit 2digit 3digit 4

Port A ← 7Dh (segments), port C ← 08h: only digit 4 is lit. INC SI, CMP, JB → SI = 4, the loop ends.

SI0004hAL08hport A = 7Dhport C = 08hdigit 1digit 2digit 3digit 4

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).
-4004080120port 125: 70 °Cheater OFFport 127 = 0target range: 60–80 °C

Thermometer · Step through the code

The Thermostat Program

-4004080120port 125: 50 °Cheater OFFport 127 = 0target range: 60–80 °C

Port 125 will return 55, 70 and 85 °C in turn. The heater starts off.

-4004080120port 125: 50 °Cheater OFFport 127 = 0target range: 60–80 °C

AX = CS = 0700h.

-4004080120port 125: 50 °Cheater OFFport 127 = 0target range: 60–80 °C

DS = 0700h (an immediate can't go straight into a segment register).

-4004080120port 125: 55 °Cheater OFFport 127 = 0target range: 60–80 °C

IN AL, 125 → temperature 55 °C.

-4004080120port 125: 55 °Cheater OFFport 127 = 0target range: 60–80 °C

CMP AL, 60: 55 − 60 → SF = 1, OF = 0.

-4004080120port 125: 55 °Cheater OFFport 127 = 0target range: 60–80 °C

JL (SF ≠ OF): 55 < 60 → low.

-4004080120port 125: 55 °Cheater OFFport 127 = 0target range: 60–80 °C

AL = 1.

-4004080120port 125: 55 °Cheater ONport 127 = 1target range: 60–80 °C

OUT 127, AL: heater on.

-4004080120port 125: 55 °Cheater ONport 127 = 1target range: 60–80 °C

JMP ok.

-4004080120port 125: 55 °Cheater ONport 127 = 1target range: 60–80 °C

JMP start: new reading.

-4004080120port 125: 70 °Cheater ONport 127 = 1target range: 60–80 °C

IN AL, 125 → temperature 70 °C.

-4004080120port 125: 70 °Cheater ONport 127 = 1target range: 60–80 °C

CMP AL, 60: 70 − 60 → SF = 0, OF = 0.

-4004080120port 125: 70 °Cheater ONport 127 = 1target range: 60–80 °C

JL (SF ≠ OF): no jump.

-4004080120port 125: 70 °Cheater ONport 127 = 1target range: 60–80 °C

CMP AL, 80 → ZF = 0, SF = 1.

-4004080120port 125: 70 °Cheater ONport 127 = 1target range: 60–80 °C

JLE: 60 ≤ 70 ≤ 80 → ok (leave the heater alone).

-4004080120port 125: 70 °Cheater ONport 127 = 1target range: 60–80 °C

JMP start: new reading.

-4004080120port 125: 85 °Cheater ONport 127 = 1target range: 60–80 °C

IN AL, 125 → temperature 85 °C.

-4004080120port 125: 85 °Cheater ONport 127 = 1target range: 60–80 °C

CMP AL, 60: 85 − 60 → SF = 0, OF = 0.

-4004080120port 125: 85 °Cheater ONport 127 = 1target range: 60–80 °C

JL (SF ≠ OF): no jump.

-4004080120port 125: 85 °Cheater ONport 127 = 1target range: 60–80 °C

CMP AL, 80 → ZF = 0, SF = 0.

-4004080120port 125: 85 °Cheater ONport 127 = 1target range: 60–80 °C

JLE: no jump.

-4004080120port 125: 85 °Cheater ONport 127 = 1target range: 60–80 °C

JG: 85 > 80 → high.

-4004080120port 125: 85 °Cheater ONport 127 = 1target range: 60–80 °C

AL = 0.

-4004080120port 125: 85 °Cheater OFFport 127 = 0target range: 60–80 °C

OUT 127, AL: heater off.

-4004080120port 125: 85 °Cheater OFFport 127 = 0target range: 60–80 °C

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:
CodeNameEffect
0Ah (10)LFone line down
0Dh (13)CRback to line start
09h (9)HThorizontal tab
0Ch (12)FFnew 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.
Hello, Printer!********Have a nice…0Ah, 0Dh: start of next lineCR (13): back to start of this lineHT (9): jump to column 8 → overprintFF (12): eject, new page

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.$"
Lengthoffset msg_end - offset msg is computed at assembly time: 15 + 2 + 15 + 2 + 25 = 59 = 3Bh bytes.
msg_endThe address just past the last character; its value (0) is not printed, it is only a marker.
msg2'$' 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)

command (9): 00000001data (10): 00000000status (11): 00000000lamp onlamp offwall
  • 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
0do nothing
1move forward
2turn left
3turn right
4examine what's ahead
5switch lamp on
6switch lamp off
Data (port 10)Ahead
255 (FFh)wall
0empty
7lamp on
8lamp off
Status (port 11)If 1
bit 0new data in the data register
bit 1robot busy (command running)
bit 2error 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
Preserve ALwait_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.
Error bitA “forward” into a wall sets the error bit (bit 2); read and check the status.
The cost of pollingThe CPU is fully tied up in the wait loop; real systems prefer interrupts.

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 h suffix 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
simple io testwrite byte to port 110:write word to port 112:read byte from port 110:A8hread word from port 112: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
AX00FFhZF0SF0OF0CF0port 110 (byte)in: FFhout: —port 112 (word)in: 7FFFhout: —

Values entered in the test device window: port 110 = FFh, port 112 = 7FFFh. IN AL, 110 → AL = FFh.

AX0000hZF1SF0OF0CF0port 110 (byte)in: FFhout: —port 112 (word)in: 7FFFhout: —

INC AL: FFh + 1 = 00h (wraps in 8 bits). ZF = 1; INC leaves CF unchanged.

AX0000hZF1SF0OF0CF0port 110 (byte)in: FFhout: 00hport 112 (word)in: 7FFFhout: —

OUT 110, AL: the window shows port 110 = 00h.

AX7FFFhZF1SF0OF0CF0port 110 (byte)in: FFhout: 00hport 112 (word)in: 7FFFhout: —

IN AX, 112: 16-bit read → AX = 7FFFh (+32,767).

AX8000hZF0SF1OF1CF0port 110 (byte)in: FFhout: 00hport 112 (word)in: 7FFFhout: —

ADD AX, 1 → 8000h: two positives gave a negative sum → OF = 1, SF = 1; no unsigned carry, CF = 0.

AX8000hZF0SF1OF1CF0port 110 (byte)in: FFhout: 00hport 112 (word)in: 7FFFhout: 8000h

OUT 112, AX: the window shows port 112 = 8000h.

AX8000hZF0SF1OF1CF0port 110 (byte)in: FFhout: 00hport 112 (word)in: 7FFFhout: 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

DevicePortKey idea
Traffic lights4One bit per lamp; a situation table + INT 15h/86h waits.
Stepper motor7Bits 0–2 magnets, bit 7 ready; half step 11.25°, full step 22.5°.
LED display199A word, shown as signed decimal.
Thermometer125, 127Read + signed compare + heater on/off.
Printer130INT 21h/05h; control characters LF, CR, HT, FF.
Robot9, 10, 11Command, data, status; poll the busy/data bits.
Test device110, 112Byte and word I/O experiments.
8255 PPI (real)310h–313hThe 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

  1. What port 4 value lights only the amber lamps in every direction?
  2. What should CX and DX be for a 2.5-second wait?
  3. Which hex value in AX shows −1 on the LED display?
  4. Change the thermostat to a 20–25 °C range. What would happen at −10 °C if you used JB?
  5. Which lines of the stepper program must change to turn the motor only clockwise, in full steps?
Answers
  1. Bits 1, 4, 7, 10 → 0000_0100_1001_0010b = 0492h.
  2. 2,500,000 = 002625A0h → CX = 0026h, DX = 25A0h.
  3. FFFFh.
  4. 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.
  5. mov bx, offset datcw_fs, and drop the table-switching part (inc cx … jmp next_step), putting jmp next_step in its place.

Summary

Exercises (continued)

  1. Write the shortest program that prints “Hello” on the printer and ejects the page.
  2. On an 8 MHz 8086, what N gives about 1 ms with mov cx, N / loop?
  3. What 8255 control word makes A input, B output and all of C input (mode 0)?
  4. 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
  1. For each character MOV DL, 'H' / MOV AH, 5 / INT 21h (or a loop), then a form feed with DL = 12.
  2. 17N − 8 = 8,000 → N ≈ 471 (7,999 clocks).
  3. 1 00 1 1 0 0 1b = 99h.
  4. '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).

The End

Chapter 10.2: Device Control
Previous: Chapter 10.1: 8086 Pins · Next: Chapter 11: Instruction Encoding and String Instructions
1 / 1 Sercan KÜLCÜ. All rights reserved.