NASM Coding: Basics - Examples

  1. _exit(num);

    This is a C syscall for terminating the program's execution. The argument

    num
    contains a number representing the exit status (also called exit code):
    0
    means "Success" (no issues happened during the execution), while any non-zero number represents some error. In particular, every non-zero exit code stands for a different type of error (examples:
    1
    is a generic error code;
    11
    is for segmentation fault; and
    13
    is for permission denied errors - e.g., trying to open a file that you don't own.)

    The

    _exit
    syscall takes only $1$ argument, so it uses only the register
    rdi
    to put the argument in, and
    rax
    for the syscall number. Calling
    _exit
    is pretty similar to calling
    return
    inside the
    main()
    function in C.

    mov       rax, 60                 ; syscall code 60 is for 'exit'
    mov       rdi, 0                  ; exit code 0 is for 'Success'
    syscall                           ; invoke operating system to exit the program