Chapter 10.2: Device Control
Multiple Choice · I/O Basics
Question 1: IN and OUT Rules
Which of the following is a valid I/O instruction sequence on the 8086?
B: A port number 0–255 may be written as a constant in the instruction (OUT 4, AL); above 255 it must be given in DX, so OUT 300h, AL is invalid. Data moves only through AL (byte) or AX (word); BX cannot be used. The syntax is OUT port, AL and IN AL, port; the last option has the operands reversed.
Multiple Choice · I/O Basics
Question 2: Transfer Without the CPU
Which method moves a large block from disk to memory without the CPU carrying every byte, and which chip does the job?
A: In direct memory access (DMA) the controller requests the bus from the processor with HOLD, gets it with HLDA, and moves the data between device and memory itself. In polling the CPU reads a status bit in a loop; with interrupts the device alerts the CPU when ready but the CPU still moves the data. The 8255 is parallel I/O, the 8259A an interrupt controller, the 8253/8254 a counter/timer; the 8279 is a keyboard/display controller.
Multiple Choice · I/O Basics
Question 3: Memory-Mapped I/O
Which of the following is an example of memory-mapped I/O?
C: Text-screen memory starts at B800:0000; it can be written by any instruction such as MOV or ADD, and the byte written appears on screen. The other three are port-mapped I/O: the separate 64 K I/O address space is reached only with IN and OUT (the M/IO̅ pin marks the I/O cycle).
Classic Question · I/O Basics
Question 4: The 8255 Control Word
8255 control word: D7 = 1 mode select, D6 D5 group A mode, D4 port A, D3 C upper half, D2 group B mode, D1 port B, D0 C lower half (1 = input).
- In mode 0, what is the control word for A input, B output, C upper half input, C lower half output?
- If 8Ah is written to the control register, what are the port directions?
- Which words set PC5 to 1 and clear PC2 to 0 with bit set/reset (BSR)?
Show answer
- 1 00 1 1 0 0 0b = 98h.
- 8Ah = 1 00 0 1 0 1 0b: mode 0; A output, C upper input, B input, C lower output.
- In BSR, D7 = 0, D3–D1 are the bit number, D0 is the new value: PC5 = 1 → 0000 1011b = 0Bh; PC2 = 0 → 0000 0100b = 04h. The other C bits are unchanged.
Since the port address is above 255 (e.g. control register 313h), the write is done with MOV DX, 313h · OUT DX, AL.
Step-by-Step Trace · Traffic Lights
Question 5: Changing the Lamp Bits
On each pole, bit 3k is red (R), 3k+1 yellow (Y), 3k+2 green (G). What value is written to port 4, and which lamps light?
AX = 0249h = 0010 0100 1001b: bits 0, 3, 6, 9 → the red lamps of all four poles.
XOR flips bits 0 and 2: the lower-right red goes off and green comes on → AX = 024Ch.
OR sets bit 4: yellow is added to red at the upper right ("get ready") → AX = 025Ch.
OUT 4, AX: the lamps switch to the new state. Bits 12–15 are unused; XOR/OR change only the selected bits.
ORG 100h mov ax, 0249h ; all_red xor ax, 0005h ; flip bits 0 and 2 or ax, 0010h ; set bit 4 out 4, ax ret
Classic Question · Traffic Lights
Question 6: Delay Time with INT 15h
INT 15h, AH = 86h waits CX:DX microseconds.
- What must CX and DX be for a 0.75-second wait?
- For a 12-second wait?
- What is the longest wait, in minutes, possible with a single call to this function?
Show answer
- 750,000 µs = 000B71B0h → CX = 000Bh, DX = 71B0h.
- 12,000,000 µs = 00B71B00h → CX = 00B7h, DX = 1B00h.
- CX = DX = FFFFh: 4,294,967,295 µs ≈ 4,295 s ≈ 71.6 minutes.
Convert the decimal value to hexadecimal (repeated division by 16) and split the 32-bit result in two: the high word goes in CX, the low word in DX.
Multiple Choice · Traffic Lights
Question 7: Delay with LOOP
On a 4.77 MHz 8086, mov cx, 1000 (4 clocks) is followed by wait: loop wait (17 clocks if it jumps, 5 if it falls through). About how long is the delay?
D: The total is 4 + 17 · 999 + 5 = 17N − 8 = 16,992 clock cycles. One clock cycle is 1 / 4.77 MHz ≈ 0.2096 µs, so the time is 16,992 ÷ 4.77 ≈ 3,562 µs ≈ 3.56 ms. The 3.56 µs option and 17.0 ms mistake the clock counts for microseconds. This time depends on the clock speed; emu8086 does not model instruction timing.
Step-by-Step Trace · Stepper Motor
Question 8: Polling a Stepper Motor
Port 7 returns 00h, 80h, 00h, 00h, 80h in turn. What is DX when the program ends? Which bytes are written to the port?
BX = 0102h: CCW full-step table (data at 0102h–0105h).
IN AL, 7 → 00h.
INC DX: 1st read. TEST AL, 80h: bit 7 = 0 → ZF = 1, motor busy.
JZ: ZF = 1 → read again (polling).
IN AL, 7 → 80h: ready.
TEST: ZF = 0. DX = 2.
AL ← [BX+SI] = [0102h] = 100b.
OUT 7, AL: only magnet 2 is energized.
JB: SI = 1 < 2 → next byte. This time the port returns 00h, 00h, 80h.
The third try returned 80h: DX = 5 reads.
AL ← [0103h] = 110b.
OUT 7, AL: magnets 2 and 1 are on. Two bytes were sent; 3 reads were wasted while the motor was busy.
ORG 100h jmp start table db 100b, 110b, 011b, 000b start: mov bx, offset table mov si, 0 mov dx, 0 ; read counter next_step: wait: in al, 7 inc dx test al, 80h ; bit 7: ready jz wait mov al, [bx+si] out 7, al inc si cmp si, 2 jb next_step ret
Multiple Choice · Stepper Motor
Question 9: How Many Degrees Does It Turn?
In the emu8086 stepper motor a half step is 11.25°. The 4 bytes of the half-step table are sent for 32 rounds (4 bytes per round). How many degrees does the shaft turn in total?
C: 32 × 4 = 128 half steps; 128 × 11.25° = 1440° = 4 full turns. 2880° counts every byte as a 22.5° full step; 360° takes the 32 steps for a single turn.
Output Question · Stepper Motor
Question 10: Excitation Sequence by Rotation
Which values are written to DL in turn during the loop? Which drive mode of a real unipolar stepper motor does this sequence correspond to? What must change to reverse the motor?
ORG 100h mov al, 33h mov cx, 5 again: mov bl, al and bl, 0Fh ; low 4 bits mov dl, bl ; goes to port A rol al, 1 loop again ret
Show answer
DL: 03h, 06h, 0Ch, 09h, 03h
AL = 33h = 0011 0011b; ROL shifts the pair of 1s one place left each round: 66h, CCh, 99h, 33h … The low 4 bits always select two adjacent windings: two-phase, full-step drive (03h 06h 0Ch 09h).
Because the upper nibble is a copy of the lower nibble, an 8-bit rotate behaves like a 4-bit ring. To reverse the direction use ror al, 1 instead of rol: 03h, 09h, 0Ch, 06h.
Multiple Choice · LED Display
Question 11: −300 on the LED Display
What value must AX hold before OUT 199, AX for the LED display at port 199 to show -300?
A: The display interprets the word as a two's-complement signed number: −300 = 10000h − 012Ch = FED4h (mov ax, -300 lets the assembler do this). 812Ch is sign-magnitude, FED3h is one's complement; 012Ch is +300.
Output Question · LED Display
Question 12: Segment Code Table
What is AL after XLAT and NOT? Which segments light? Would the NOT line be needed if the display were common-cathode?
mov bx, offset segtab mov al, 6 xlat ; AL = [BX + AL] not al ; common anode mov dx, 310h ; 8255 port A out dx, al ... segtab db 3Fh,06h,5Bh,4Fh,66h db 6Dh,7Dh,07h,7Fh,6Fh
Show answer
XLAT: AL ← [segtab + 6] = 7Dh = 0111 1101b (dp g f e d c b a). NOT: AL = 82h = 1000 0010b.
With a common anode a segment lights on 0: a, c, d, e, f, g light, b and dp stay off → the digit 6.
With a common cathode a bit = 1 lights the segment, so the table value (7Dh) is sent directly; NOT is not needed.
Step-by-Step Trace · Thermometer
Question 13: Thermostat and Negative Temperatures
Port 125 returns 85, F6h and 65 in turn. Which values are sent to the heater? What would happen at −10 °C if JB/JBE/JA were written instead of JL/JLE/JG?
IN AL, 125 → 85 = 55h.
CMP AL, 60: 85 − 60 = 25 → SF = 0, OF = 0, CF = 0.
JL did not jump. CMP AL, 80: 85 − 80 = 5 → ZF = 0, SF = OF.
JLE did not jump either; JG: ZF = 0 and SF = OF → high.
OUT 127, 0: the heater turned off.
New reading: AL = F6h = −10 °C.
CMP AL, 60: −10 − 60 = −70 → SF = 1, OF = 0. Read as unsigned, 246 > 60, so CF = 0.
JL: SF ≠ OF → −10 < 60, low.
OUT 127, 1: the heater turned on.
New reading: 65 °C.
CMP AL, 60: 65 ≥ 60, JL does not jump. CMP AL, 80: 65 < 80 → SF = 1, OF = 0, ZF = 0.
JLE: jumps → ok; 60 ≤ 65 ≤ 80, the heater is left alone.
With JB/JBE/JA: −10 = F6h = 246 unsigned counts as greater than 80, JA jumps, and the heater would be turned off at −10 °C. Temperatures that can be negative are compared with signed jumps.
ORG 100h 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
Output Question · Printer
Question 14: How Many Characters to the Printer?
What value does CX get? When the print loop (mov dl, [si] · mov ah, 5 · int 21h · inc si · loop) finishes, what appears on the paper?
msg db "Note: 85", 0Dh, 0Ah
db "Date", 09h, "2026"
msg_end db 0
...
mov si, offset msg
mov cx, offset msg_end - offset msg
Show answer
8 + 2 + 4 + 1 + 4 = 19 = 13h. The difference is computed at assembly time; the 0 at msg_end is just a marker and is not printed.
Note: 85 Date 2026
CR (0Dh) returns to the start of the line, LF (0Ah) moves down one line. HT (09h) jumps to the next 8-column tab stop, i.e. column 9. To eject the page, DL = 0Ch (FF) must also be sent.
Multiple Choice · Printer
Question 15: The New-Page Code
Which code goes in DL to advance the printer to a new page with INT 21h, AH = 05h?
D: Form feed (FF, 12) advances the paper to the end of the page. LF moves down one line, CR returns to the start of the line, HT goes to the horizontal tab. A printer program sends FF at the start and the end to use a clean page.
Multiple Choice · Robot
Question 16: The Unprotected AL
In the robot program the PUSH AX / POP AX in the send path were deleted. The robot saw the wall ahead and send was reached with AL = 3 (turn right). wait_ready returned after reading port 11 as 03h and then 01h. Which command goes to port 9?
B: wait_ready reads the status byte into AL and loops until the busy bit (bit 1) is 0: on exit AL = 01h (the new-data bit). The command is lost and the status byte is sent instead: the robot tries to go forward into the wall and the error bit (bit 2) is set. That is why the command value is pushed on the stack before the routine and popped afterwards.