IN Instruction

What is the IN Instruction?

The IN instruction is an I/O operation instruction used to read data from an I/O port into a CPU register. This capability is crucial for interacting with hardware components, such as keyboards, disk drives, network interfaces, and other peripherals.

Syntax of the IN Instruction

The basic syntax of the IN instruction is as follows:

IN destination, port (source)
  • destination: The register where the data read from the I/O port will be stored. Typically, this is the AL, AX, or EAX register, depending on the size of the data.
  • port (source): The I/O port address from which the data will be read. This can be specified directly as an immediate value or indirectly via the DX register.

-: Table of registers allowed for the destination :-

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

-: Table For the Source and Destination Register :-

Source could be immediate Value like 0x1F0 or value in DX register like:

mov dx, 0x1F0

And then this DX is used as the source port address.

Source (Port Address)Destination Register
Immediate Value e.g., 0x1F0AL, AX, EAX
DX = 0x1F0AL, AX, EAX

Examples of the IN Instruction

Let's explore some examples to illustrate how the IN instruction is used in assembly language.

Reading a Byte from an I/O Port:

To read a byte from an I/O port (for example, a keyboard data port) and store it in the AL register, you can use the following instruction:

mov dx, 0x60  ; I/O port address (keyboard data port)
in al, dx     ; Read a byte from the port into the AL
			  ; register

In this example:

  • mov dx, 0x60 sets the DX register to the port address 0x60.
  • in al, dx reads a byte from the port specified in DX and stores it in the AL register.

We can also provide the port address directly to the in instruction:

; We can also provide the port (source) as the immediate port address but it is not commonly used.
in al, 0x60		; Read a byte from port 0x60 into AL
				; Should be avoided.

Reading a Word from an I/O Port

To read a word (2 bytes) from an I/O port and store it in the AX register:

mov dx, 0x1F0  ; I/O port address (typically used for ATA devices)
in ax, dx      ; Read a word from the port into the AX register

Here:

  • mov dx, 0x1F0 sets the DX register to the port address 0x1F0.
  • in ax, dx reads a word from the port specified in DX and stores it in the AX register.

Note:

For the IN instruction, some assembler do complain if we don't specify the operation data size explicitly. For it, you can explicitly specify the data size using byte, word, or dword to indicate 8-bit, 16-bit, or 32-bit operations, respectively.

;; Normal way where assembler should understand
;; the operation data size from the destination
;; register, like if al is used, it means 8 bit
;; data to read etc.

in al, dx          ; 8-bit read
in ax, dx          ; 16-bit read
in eax, dx         ; 32-bit read

;; Explicitly specify the operation data size
in byte al, dx          ; 8-bit read
in word ax, dx          ; 16-bit read
in dword eax, dx         ; 32-bit read
Source (Port Address)Destination RegisterExplicit Size
Immediate ValueALbyte
Immediate ValueAXword
Immediate ValueEAXdword
DXALbyte
DXAXword
DXEAXdword