typestar

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

  1. [rsp] is argc; [rsp + 8] is argv[0].
  2. The scan measures the program path's length.
  3. Run bare, argc is 1: exit code 101.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Talking to Linux, step 28 of 31 in Language basics.

← Previous Next →