Command-line arguments

The stack at _start

At the Linux entry point, the kernel has already pushed the argument block onto the stack: argc at [rsp], then the argv pointers, then a NULL, then the environment pointers:

_start: pop rdi ; argc ; rsp now points at argv[0]

argc and argv

argv is an array of pointers: each entry points to one argument string, and the array ends with a NULL pointer. argv[0] is the program name, so the first real argument is argv[1]:

_start: pop rdi ; argc pop rsi ; argv[0], the program name pop rsi ; argv[1], the first argument

Walking the array

To walk every argument, keep the count in a register and load each pointer with an offset from the stack pointer. Each string is a normal NUL-terminated byte sequence, printable with sys_write after you find its length.

Next: Environment variables

Article author: Arthur Isaev