Menu
Coddy logo textTech

Exit Syscall (60)

Part of the Fundamentals section of Coddy's Assembly journey — lesson 32 of 45.

The most important syscall for any program is exit. Without it, your program would keep running forever or crash when it reaches the end of memory.

The exit syscall number is 60.

Syntax:

mov rax, 60    ; syscall number for exit
mov rdi, 0     ; exit code (0 = success)
syscall        ; ask the OS to exit

What happens when you execute <strong>syscall</strong> with <strong>rax = 60</strong>:

  1. The CPU jumps into the Linux kernel
  2. The kernel sees: "exit syscall with code 0"
  1. The kernel terminates your program
  2. The kernel returns control to the shell or operating system

Exit code 0 means success — the program completed without errors. Exit codes 1 through 255 indicate different errors. You can choose any number to signal what went wrong.

Why exit codes matter:

After your program exits, the shell (terminal) stores the exit code. You can check it with:

./myprogram
echo $?    ; Prints the exit code

This is useful for:

  • Scripts that check if a program succeeded
  • Debugging (different codes for different errors)
  • Chaining programs together

A complete minimal program:

section .text
    global _start

_start:
    mov rax, 60    ; exit syscall
    mov rdi, 0     ; exit code 0 (success)
    syscall

This program does nothing visible — it just exits successfully. But it is a valid, complete program.

Let's mention what global _start and _start: are.

CodeWhat it isWhy you need it
global _startTells the linker where your program startsWithout it, the linker does not know which label is the entry point
_start:A label marking the beginning of your codeThe CPU starts executing here

You do not need to memorize the details. Just know that every main.asm must have these two lines before your code. You will use them in every program.

What happens if you forget the exit syscall?

If you do not include the exit syscall, the CPU will keep executing whatever garbage data happens to be in memory after your code. This usually causes a segmentation fault (crash) or completely unexpected behavior. Your program will not end cleanly.

Always end your program with an exit syscall.

challenge icon

Challenge

Add the exit syscall with exit code 5.

The code to print the message "Exiting with code 5" is already provided for you. You do not need to understand it yet — it will be covered in Chapter 5.

Your job is only to add the exit syscall at the end.

Try it yourself

section .data
    msg db 'Exiting with code 5', 0x0a
    msgLen equ $ - msg

section .text
    global _start

_start:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg
    mov rdx, msgLen
    syscall

; TODO: Add the exit syscall with exit code 5
quiz iconTest yourself

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

All lessons in Fundamentals