OUT Instruction

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 the DX register.
  • source: The register containing the data to be written to the I/O port. This can be AL, AX, or EAX, depending on the size of the data.

-: Table of registers allowed for the source :-

Data SizeSource Register
8-bitAL
16-bitAX
32-bitEAX

-: Table of Source and Destination Values :-

Source RegisterDestination (Port Address)
AL, AX, EAXImmediate Value
AL, AX, EAXDX

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 the DX register to the port address 0x64.
  • mov al, 0xED loads the AL register with the command 0xED.
  • out dx, al sends the command in the AL register to the port specified in DX.

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 the DX register to the port address 0x1F0.
  • mov ax, 0x1234 loads the AX register with the data 0x1234.
  • out dx, ax sends the data in the AX register to the port specified in DX.

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 RegisterDestination (Port Address)Explicit Size
ALImmediate Valuebyte
AXImmediate Valueword
EAXImmediate Valuedword
ALDXbyte
AXDXword
EAXDXdword