Menu
Coddy logo textTech

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 descriptorNameWhere data goes
0stdinKeyboard input (read data from user)
1stdoutScreen output (normal program output)
2stderrScreen 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 useResult
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 numberThe kernel does not recognize it — the syscall fails
challenge icon

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
    syscall
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals