Exit Codes
Part of the Fundamentals section of Coddy's Assembly journey — lesson 33 of 45.
When your program exits, it sends a small number back to the operating system. This number is called the exit code or exit status.
Why exit codes matter:
Scripts use exit codes to check if a program succeeded. For example, a backup script might only continue if the previous command succeeded.
Exit codes also help with debugging. You can use different error codes for different problems.
You can also chain programs together so one program runs only if the previous one succeeded. The shell checks the exit code before running the next command.
The rules of exit codes:
| Exit code | Meaning |
|---|---|
| 0 | Success — everything worked |
| 1-255 | Something went wrong |
Choosing error codes:
You can use any number from 1 to 255. What each number means is up to you as the programmer.
| Exit code | What you might mean |
|---|---|
| 1 | General error |
| 2 | Missing input file |
| 3 | Permission denied |
| 4 | Network error |
What happens if you do not set an exit code?
You must always set rdi before the exit syscall. If you forget, rdi contains whatever garbage was left in the register, and your program will exit with a random code.
Challenge
Write a complete assembly program that exits with exit code 2 (to signal "file not found").
The print code is provided for you. You only need to add the exit syscall.
Try it yourself
section .data
msg db 'Backup complete', 0x0a
msgLen equ $ - msg
section .text
global _start
_start:
; Print the message (provided by test harness)
mov rax, 1
mov rdi, 1
mov rsi, msg
mov rdx, msgLen
syscall
; Student's code goes here
%include "solution.asm"This 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 Architecture