Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are function arguments passed in C?

The only thing that I know about the mechanism of how C passes values is that it is done either through a register or the stack.

Register or Stack? Exactly how?

like image 422
Alcott Avatar asked Sep 17 '11 01:09

Alcott


2 Answers

Both. And the conventions will vary by platform.

On x86, values are usually passed by stack. On x64, passing by register is preferred.

In all cases, if you have too many parameters, some will have to be passed by stack.

Refer to x86 calling conventions

like image 186
Mysticial Avatar answered Oct 22 '22 17:10

Mysticial


Typically (some compilers will do it differently as pointed out) for normal function calls they are passed on the stack. That is usually it is a series of push instructions that just put the data onto the stack.

There are special cases such as system calls where parameters get passed via assembly instructions and registers. In hardware cases they are passed via registers or even certain interrupt signals which consequently write to registers.

On architectures with high numbers of registers they are usually passed via registers such as some RISC and 64 bit architectures.

like image 23
Jesus Ramos Avatar answered Oct 22 '22 18:10

Jesus Ramos