C 中函数参数是如何传递的?
关于 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个都。并且约定会因平台而异。
在 x86 上,值通常通过堆栈传递。在 x64 上,首选通过寄存器传递。
在所有情况下,如果参数太多,则某些参数必须通过堆栈传递。
请参阅x86 调用约定
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
通常(某些编译器会按照指出的方式进行不同的操作)对于普通函数调用,它们在堆栈上传递。这通常是一系列压入指令,只是将数据放入堆栈。
有一些特殊情况,例如系统调用,其中参数通过汇编指令和寄存器传递。在硬件情况下,它们通过寄存器甚至某些中断信号传递,从而写入寄存器。
在具有大量寄存器的体系结构上,它们通常通过寄存器传递,例如某些 RISC 和 64 位体系结构。
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.