File Descriptor (rdi)
Part of the Fundamentals section of Coddy's Assembly journey — lesson 36 of 45.
In the previous lesson, you used mov rdi, 1 as part of the write syscall. That number 1 is called a file descriptor.
A file descriptor is a number that tells the operating system where to send the data.
The three standard file descriptors in Linux:
| File descriptor | Name | Where data goes |
|---|---|---|
| 0 | stdin | Keyboard input (read data from user) |
| 1 | stdout | Screen output (normal program output) |
| 2 | stderr | Screen output (error messages) |
Why have two outputs for the screen?
Normal output (stdout) and error output (stderr) go to the same screen, but they are treated separately. This allows you to:
- Redirect normal output to a file while still seeing errors on the screen
- Show errors even when normal output is hidden
For printing in this course:
You will always use file descriptor 1 (stdout) for printing messages. You do not need stderr yet.
Example:
mov rdi, 1 ; send output to stdout (the screen)What happens if you use the wrong file descriptor?
| If you use | Result |
|---|---|
| 0 (stdin) | The write syscall expects input, not output — will fail or do nothing |
| 2 (stderr) | Works like stdout (prints to screen), but is meant for errors |
| Any other number | The kernel does not recognize it — the syscall fails |
Challenge
You are given a string called message with the value 'OK' followed by a newline. Write the line of code that sets the file descriptor to print to the screen (stdout). Only write the mov instruction for rdi.
Try it yourself
section .data
message db 'OK', 0x0a
msgLen equ $ - message
section .text
global _start
_start:
; Student sets file descriptor here
%include "solution.asm"
mov rax, 1
; rdi should already be set by student
mov rsi, message
mov rdx, msgLen
syscall
mov rax, 60
mov rdi, 0
syscallThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
1The Machine
What is CPUMemoryRegisters vs MemoryHow instructions workTwo-File PatternThe x86-64 Architecture3First instruction - MOV
What is MOV?MOV ImmediateMOV Register to RegisterMOV Memory to RegisterMOV Register to MemoryLEA - Load AddressMOV with Different SizesPractice: Swap RegistersRecap: MOV Challenge6Write Syscall
Write Syscall (1)File Descriptor (rdi)Buffer Address (rsi)Byte Count (rdx)Putting It TogetherPrinting Two StringsNewline MattersRecap: Write Setup