Search code examples
cargumentsstacknasm

Passing command line options from NASM to C


I have an assembly file that calls a C function that takes the arguments int argc, char *argv[], with the assumption that what should already be on the stack (the command line arguments) will be passed to the function. But instead, it segfaults the moment the function is called. Any wisdom?

Assembly:

extern func
; stuff over here
_start:
        call func ; nothing manually pushed into stack.
; stuff over here

C:

struct of_my_creation func(int argc, char *argv[])
{
  printf("check"); // never runs; SIGSEGV comes first.
}

OS: Parrot Linux

Compiler: GCC 10

Architecture: x86-64

Thanks.


Solution

  • Thanks to A. Loiseau

    Passing the arguments into rdi and rsi instead of stack worked.

    New assembly:

    extern func
    ; stuff over here
    _start:
            pop rdi
            lea rsi, [rsp]
            call func
    ; stuff over here