Input/Output Instructions in Assembly Language

Input/output instructions in assembly language facilitate communication between the program and external devices, such as reading from or writing to files, displaying output, or receiving input.

Print String

Displays a string of characters to the output (e.g., console).

section .data hello db 'Hello, World!', 0 section .text global _start _start: mov rax, 1 ; syscall: write mov rdi, 1 ; file descriptor: STDOUT mov rsi, hello ; string address mov rdx, 13 ; string length syscall mov rax, 60 ; syscall: exit xor rdi, rdi ; status: 0 syscall
Output: Hello, World!

Read String

Reads a string of characters from the input (e.g., keyboard).

section .data buffer resb 256 ; buffer to store input section .text global _start _start: mov rax, 0 ; syscall: read mov rdi, 0 ; file descriptor: STDIN mov rsi, buffer ; buffer address mov rdx, 256 ; buffer size syscall mov rax, 60 ; syscall: exit xor rdi, rdi ; status: 0 syscall

Print Integer

Displays an integer value to the output.

section .data format db '%d', 0 ; format specifier value dd 42 ; integer value to print section .text global _start _start: mov rdi, format ; format specifier address mov rsi, value ; value to print address mov rax, 1 ; syscall: write mov rdx, 4 ; length of the integer (4 bytes) syscall mov rax, 60 ; syscall: exit xor rdi, rdi ; status: 0 syscall

Read Integer

Reads an integer value from the input.

section .data format db '%d', 0 ; format specifier value resd 1 ; integer value to read section .text global _start _start: mov rdi, format ; format specifier address mov rsi, value ; value to read address mov rax, 0 ; syscall: read mov rdx, 4 ; length of the integer (4 bytes) syscall mov rax, 60 ; syscall: exit xor rdi, rdi ; status: 0 syscall

Open and Close Files

Opens and closes files for input/output operations.

Example (Open):
section .data filename db 'example.txt', 0 mode db 'r', 0 section .text global _start _start: mov rdi, filename ; filename address mov rsi, mode ; mode address mov rax, 2 ; syscall: open syscall ; File descriptor is returned in RAX ; ... mov rax, 60 ; syscall: exit xor rdi, rdi ; status: 0 syscall
Example (Close)
section .data file_descriptor dd 3 ; replace with actual file descriptor section .text global _start _start: mov rdi, file_descriptor ; file descriptor mov rax, 3 ; syscall: close syscall ; ... mov rax, 60 ; syscall: exit xor rdi, rdi ; status: 0 syscall

How to use I/O instructions

To use I/O instructions in assembly language, the programmer must first identify the device that they want to communicate with. Once the device has been identified, the programmer can use the appropriate I/O instruction to read or write data to the device.

For example, the following assembly language instruction will read a character from the keyboard and store it in the register AL:

in al, 0x60

The in instruction is a generic I/O instruction that is used to read data from a device. The first operand of the in instruction is the register where the data will be stored. The second operand of the in instruction is the port address of the device. The port address is a unique identifier for the device.

To write a character to the monitor, the programmer can use the following assembly language instruction:

out 0x60, al

The out instruction is a generic I/O instruction that is used to write data to a device. The first operand of the out instruction is the port address of the device. The second operand of the out instruction is the register where the data to be written is stored.

Conclusion

Input/output instructions enable communication between a program and external devices, such as displaying output or reading input. These instructions, often involving syscalls and register manipulation, facilitate interactions with files, strings, integers, and other data sources in low-level programming.