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:
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]:
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. This complete program prints the first argument:
section .text global _start _start: pop rdi ; argc pop rsi ; argv[0], the program name pop rsi ; argv[1], the first argument xor rdx, rdx ; length counter len_loop: cmp byte [rsi + rdx], 0 je print inc rdx jmp len_loop print: mov rax, 1 ; sys_write mov rdi, 1 ; stdout syscall mov rax, 60 ; sys_exit xor rdi, rdi syscall
The program needs an argument, so the command gives it one:
nasm -f elf64 -o args.o args.asm
ld -o args args.o
./args hello world
echo $?
hello
0
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the kernel put at [rsp] when a Linux program starts?
Answer
argc, the number of arguments
The argument count is the first quadword on the stack; the argv pointers follow it.
-
What is argv[0]?
Answer
The name of the program
The shell passes the program name as the first entry, so the first real argument is argv[1].
-
How does the argv array end?
Answer
With a NULL pointer
A NULL pointer marks the end of argv, and the environment pointers start after it.
-
What does
pop rditake from the stack at the entry point?Answer
argc
The kernel pushes argc first, at the lowest address, so the first pop reads it.
-
What does this program print?
section .text global _start _start: pop rdi pop rsi pop rsi xor rdx, rdx len_loop: cmp byte [rsi + rdx], 0 je print inc rdx jmp len_loop print: mov rax, 1 mov rdi, 1 syscall mov rax, 60 xor rdi, rdi syscallThe program runs as./args one two.Answer
one
The third pop loads argv[1], and its string is one, which the length loop measures.
-
What does
pop rsia second time load, afterpop rdiandpop rsi?Answer
argv[1]
Each pop moves up one pointer: argc, argv[0], then argv[1].
-
Why does the program need the length loop before sys_write?
Answer
sys_write needs a pointer and a byte count
write takes the buffer in rsi and the number of bytes in rdx, so the NUL must be found first.
-
What is the exit status of the program
./args one twoon this page?Answer
0
The last syscall is sys_exit with rdi cleared, so the status is 0.
-
Which pointer starts the environment block?
Answer
The pointer right after the NULL that ends argv
The environment pointers follow the NULL terminator of argv.
-
What does
cmp byte [rsi + rdx], 0test in the length loop?Answer
Whether the next byte is the terminating NUL
A C string ends at the first zero byte, and the loop counts the bytes before it.
The short snippets on this page are fragments: they show the stack layout, not a whole program. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev