The IN and OUT instructions

Input and output of data through I/O ports.

The IN instruction format

InstructionDescription
IN AL, imm8Input a byte from port imm8 into AL.
IN AX, imm8Input a word from port imm8 into AX.
IN AL, DXInput a byte from the port whose address is in DX into AL.
IN AX, DXInput a word from the port whose address is in DX into AX.

Copies data from the I/O port whose address is specified in the second operand into the AL or AX register (the first operand). The port address can be specified directly (imm8 for ports 0-255) or through the DX register (for ports 0-65535).

The OUT instruction format

InstructionDescription
OUT imm8, ALOutput a byte from AL to port imm8.
OUT imm8, AXOutput a word from AX to port imm8.
OUT DX, ALOutput a byte from AL to the port whose address is in DX.
OUT DX, AXOutput a word from AX to the port whose address is in DX.

Copies data from the AL or AX register (the second operand) to the I/O port whose address is specified in the first operand.

Usage example

Reading a scan code from the keyboard port (60h) and outputting the value to the sound generator port (42h).

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

code segment public        ; Start of the code segment
    assume cs:code

    main proc              ; Start of the main procedure
        mov     dx, 60h    ; Load the keyboard port address into DX
        in      al, dx     ; Read a byte from port 60h (scan code) into AL

        mov     dx, 42h    ; Load the timer port address (channel 2) into DX
        out     dx, al     ; Output the byte read into port 42h

        mov     ax, 4C00h  ; Copy the process termination function number into ah and the value into al
        int     21h        ; Call the interrupt
    main endp              ; End of the main procedure

code ends                  ; End of the code segment
end main                   ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Processor status flags

CF
No
PF
No
AF
No
ZF
No
SF
No
TF
No
IF
No
DF
No
OF
No