OUT sends the accumulator to an I/O port: AL as a byte, or AX as a word, in which case AL goes to port p and AH to port p+1. The port comes first, as in every destination-first 8086 instruction. It is an immediate from 0 to FFh or, for any port up to FFFFh, the value in DX. No other register can be the source, and no flags change.

A beep

Timer channel 2 of the 8253/8254 drives the PC speaker. Port 43h selects the channel and mode, port 42h takes the divisor of the 1193182 Hz input clock (low byte, then high byte), and bits 0 and 1 of port 61h connect the timer to the speaker. Port 61h holds other control bits too, so the program reads it, changes two bits, and writes it back.

A short beep; its length depends on the emulated CPU speed.
MOV AL, 0B6h
OUT 43h, AL ; timer 2, low then high byte, square wave
MOV AX, 1193 ; 1193182 / 1193: about 1000 Hz
OUT 42h, AL ; low byte of the divisor
MOV AL, AH
OUT 42h, AL ; high byte
IN AL, 61h
OR AL, 3 ; gate the timer, enable the speaker
OUT 61h, AL ; sound on
MOV BX, 20
DELAY1:
MOV CX, 0FFFFh
DELAY2:
LOOP DELAY2
DEC BX
JNZ DELAY1
IN AL, 61h
AND AL, 0FCh ; clear bits 0 and 1
OUT 61h, AL ; sound off

Ports above FFh

Video card registers sit at 3B0h-3DFh, so they need DX. The VGA palette takes a colour number at 3C8h, then three values (red, green, blue, 0 to 63 each) at 3C9h. INC DX moves from one to the next.

MOV DX, 3C8h ; VGA DAC write index
MOV AL, 1 ; colour 1
OUT DX, AL
INC DX ; 3C9h: the data port
MOV AL, 63
OUT DX, AL ; red
MOV AL, 32
OUT DX, AL ; green
MOV AL, 0
OUT DX, AL ; blue: colour 1 is now orange

Pitfalls

Writing to a port tells hardware to do something, and a wrong value can hang a device or, on real machines, damage data: write only to ports you know. Keep the other bits of shared control ports such as 61h. Some old devices need a short pause between two writes to the same chip. Multi-step protocols (index, then data) break if an interrupt handler touches the same device in between; such sequences are often wrapped in CLI / STI.

Later CPUs

The 80186 adds OUTSB and OUTSW to write a block from DS:SI. The 80386 writes 32 bits with OUT DX, EAX, and protected mode checks the I/O privilege level before allowing the write.