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.
MOV AL, 0B6hOUT 43h, AL ; timer 2, low then high byte, square waveMOV AX, 1193 ; 1193182 / 1193: about 1000 HzOUT 42h, AL ; low byte of the divisorMOV AL, AHOUT 42h, AL ; high byteIN AL, 61hOR AL, 3 ; gate the timer, enable the speakerOUT 61h, AL ; sound onMOV BX, 20DELAY1:MOV CX, 0FFFFhDELAY2:LOOP DELAY2DEC BXJNZ DELAY1IN AL, 61hAND AL, 0FCh ; clear bits 0 and 1OUT 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 indexMOV AL, 1 ; colour 1OUT DX, ALINC DX ; 3C9h: the data portMOV AL, 63OUT DX, AL ; redMOV AL, 32OUT DX, AL ; greenMOV AL, 0OUT 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.