fastcall: What happens with the stack?
Asked Answered
L

1

7

I'm currently learning x64 assembly by myself and have trouble understanding what happen with the stack when calling an assembly procedure from c++.

From what I currently understand from MSDN and Intel, the first 4 integer/floating point parameters are stored in the rcx/xmm0, rdx/xmm1, r8/xmm2 and r9/xmm3 registers and all others will be placed on the stack.

I just do not understand why i have to access the 5th parameter 40 bytes from rsp [rsp+28h] instead of just 8 since the first 32 bytes are accessed in registers.

Can someone explain me what actually happens?

Thank you.

Lunation answered 24/2, 2014 at 15:29 Comment(2)
Talking about "fastcall" is not meaningful in 64-bit code. Part of the calling convention is the "shadow space", extra space in the stack frame where the RCX, RDX, R8 and R9 can be stored if necessary.Cleat
@HansPassant: but Microsoft and Intel both talk about it anyway, to highlight that the x64 calling convention is more similar to fastcall than to any of the other calling conventions used by Windows on x86.Lessen
G
3

The key is in this phrase from the linked MSDN:

The x64 Application Binary Interface (ABI) is a 4 register fast-call calling convention, with stack-backing for those registers.

That is, the registers are loaded with the first 4 arguments, but nevertheless they have its space reserved in the stack. As @HansPassant notes in the comments below, the caller does not write into this shadow space, but it is available for the callee, should it need to save the registers (for example for calling another function).

Gebhart answered 24/2, 2014 at 15:36 Comment(3)
"nevertheless stored in the stack" is fairly misleading. That only happens when the called function needs to preserve the argument values because it wants to use the registers for something better. Ideally this never happens.Cleat
@HansPassant: Ah, so the caller allocates this shadow space, but does not write into it, it is for callee use only? I'm editing my answer.Gebhart
Thank you both for that clarification :)Lunation

© 2022 - 2024 — McMap. All rights reserved.