简体   繁体   English

函数参数如何在C中传递?

[英]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. 我所知道的关于C如何传递值的机制的唯一事情是它通过寄存器或堆栈来完成。

Register or Stack? 注册还是堆叠? Exactly how? 究竟怎么样?

Both. 都。 And the conventions will vary by platform. 这些惯例因平台而异。

On x86, values are usually passed by stack. 在x86上,值通常由堆栈传递。 On x64, passing by register is preferred. 在x64上,首选寄存器是首选。

In all cases, if you have too many parameters, some will have to be passed by stack. 在所有情况下,如果您有太多参数,则必须通过堆栈传递一些参数。

Refer to x86 calling conventions 请参阅x86调用约定

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. 在具有大量寄存器的架构上,它们通常通过诸如RISC和64位架构之类的寄存器传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM