0

I'm writing a compiler and I'm trying to implement a C interface. So, I wrote a simple C program and saw the assembly code generated by GCC:

    ...

    mov rdi, rax
    call    puts@PL

    ....

I was expecting C to pass arguments to puts through the stack, but it uses the di register to do so. I tested other libc's functions such as sleep and calloc and they all use registers as well.

So, is there a pattern for this? i.e. Do all libc's functions receive arguments like syscalls? And, where does C use the stack for function arguments? Is it only for user defined functions?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    The [wiki page](https://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_calling_conventions) goes over it. – Carcigenicate Dec 27 '20 at 16:22
  • And it is different on Linux and on Windows or on FreeBSD. For inspiration, study the source code of https://en.wikipedia.org/wiki/TinyCC. **BTW did you consider [*generating C code*](https://softwareengineering.stackexchange.com/a/257873/40065) in your compiler, or using [libgccjit](https://gcc.gnu.org/onlinedocs/jit/) or [LLVM](https://llvm.org/) or [libjit](https://www.gnu.org/software/libjit/) as the backend of your compiler ?** – Basile Starynkevitch Dec 27 '20 at 16:24

0 Answers0