argc and argv in Assembly
At _start the stack holds argc, then the argv pointers.
; At _start the stack holds argc, then the argv pointers.
global _start
section .text
_start:
mov rdi, [rsp] ; argc: 1 when run bare
mov rsi, [rsp + 8] ; argv[0], the program path
; count argv[0]'s bytes into rcx
xor rcx, rcx
.scan:
cmp byte [rsi + rcx], 0
je .done
inc rcx
jmp .scan
.done:
add rdi, 100 ; argc + 100 = 101
mov rax, 60 ; exit code 101
syscall
How it works
[rsp]is argc;[rsp + 8]is argv[0].- The scan measures the program path's length.
- Run bare, argc is 1: exit code 101.
Keywords and builtins used here
addbytecmpglobalincjejmpmovraxrcxrdirsirspsectionsyscallxor
The run, in numbers
- Lines
- 19
- Characters to type
- 397
- Tokens
- 54
- Three-star pace
- 55 tpm
At the three-star pace of 55 tokens a minute, this run takes about 59 seconds.
Step 3 of 3 in Talking to Linux, step 28 of 31 in Language basics.