What is the OUT Instruction?
The OUT
instruction is an I/O operation used to write data from a CPU register to an I/O port. This enables the CPU to send commands and data to external devices, making it a fundamental instruction for system-level programming.
Syntax of the OUT Instruction
The basic syntax of the OUT
instruction is as follows:
OUT port (destination), source
port (destination)
: The I/O port address where the data will be sent. This is typically specified in theDX
register.source
: The register containing the data to be written to the I/O port. This can beAL
,AX
, orEAX
, depending on the size of the data.
-: Table of registers allowed for the source :-
Data Size | Source Register |
---|---|
8-bit | AL |
16-bit | AX |
32-bit | EAX |
-: Table of Source and Destination Values :-
Source Register | Destination (Port Address) |
---|---|
AL, AX, EAX | Immediate Value |
AL, AX, EAX | DX |
Examples of the OUT Instruction
Let's delve into some examples to understand how the OUT
instruction operates in different scenarios.
Writing a Byte to an I/O Port
To write a byte to an I/O port, for example, sending a command to the keyboard controller, you can use the following instruction:
mov dx, 0x64 ; I/O port address (keyboard controller command port)
mov al, 0xED ; Command to be sent to the port
out dx, al ; Write the byte in AL to the port
In this example:
mov dx, 0x64
sets theDX
register to the port address0x64
.mov al, 0xED
loads theAL
register with the command0xED
.out dx, al
sends the command in theAL
register to the port specified inDX
.
Writing a Word to an I/O Port
To write a word (2 bytes) to an I/O port, for example, sending data to a disk controller:
mov dx, 0x1F0 ; I/O port address (disk data port)
mov ax, 0x1234 ; Data to be sent to the port
out dx, ax ; Write the word in AX to the port
Here:
mov dx, 0x1F0
sets theDX
register to the port address0x1F0
.mov ax, 0x1234
loads theAX
register with the data0x1234
.out dx, ax
sends the data in theAX
register to the port specified inDX
.
Note:
For the OUT
instruction, you also explicitly specify the data size using byte
, word
, or dword
. Some assembler give error if you don't specify the operation data size explicitly. That's why it is advised to specify the data size of the operation.
;; This is default way, here assembler understand
;; the operation data size from the source register
;; used.
out dx, al ; 8-bit write
out dx, ax ; 16-bit write
out dx, eax ; 32-bit write
;; Explicitly specify operation data size
out byte dx, al ; 8-bit write
out word dx, ax ; 16-bit write
out dword dx, eax ; 32-bit write
Source Register | Destination (Port Address) | Explicit Size |
---|---|---|
AL | Immediate Value | byte |
AX | Immediate Value | word |
EAX | Immediate Value | dword |
AL | DX | byte |
AX | DX | word |
EAX | DX | dword |