Why parameters stored in registers and not on the stack in x86-64 Assembly?

元气小坏坏 提交于 2019-12-02 07:06:21

问题


In x86-32 assembly, parameters are stored on the stack but in x86-64, parameters stored in registers. What is the reason for this?


回答1:


It is (a lot) faster to access CPU registers than to access RAM.

Since 64bit CPU have a lot more general purpose registers (has nothing to do with being 64bit, it's just because they are newer/bigger), it makes sense to make use of them.




回答2:


Store/reload round trips take instructions and cost ~6 cycles of store-forwarding latency, so modern calling conventions use a a more efficient design. This also saves instructions in some cases, since the caller can just generate the arg in a register and not push it. (And not have to pop the stack after return).

Since x86-64 is a new mode, it didn't have any requirements for backwards compat, so a brand new ABI with no legacy baggage could be designed. See this answer for some history about how the x86-64 SysV calling convention was designed, and why it's more efficient than the Windows x86-64 calling convention. (red zone, more arg-passing registers.) It is more complex than the windows convention, especially for varargs functions.


Passing the first couple args in registers is more efficient in 32-bit code, too, but introducing new calling conventions breaks backwards compat with libraries.

Even so, MS did that with __fastcall / __vectorcall, which use two call-clobbered registers (ecx and edx) for arg-passing even in 32-bit mode. The 64-bit versions of those calling conventions use more arg-passing registers, since x86-64 has more GP registers.

Unix/Linux hasn't tried to introduce a 32-bit new calling convention, basically just giving up on 32-bit as obsolete legacy code that's stuck being slow. (Although the 32-bit SysV ABI was extended with rules for passing / returning 16B SSE and 32B AVX vectors in vector regs, not on the stack).

See the x86 tag wiki for links to calling convention docs, and performance links for more details about store-forwarding latency.



来源:https://stackoverflow.com/questions/38955064/why-parameters-stored-in-registers-and-not-on-the-stack-in-x86-64-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!