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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 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. – Hans Passant Feb 24 '14 at 15:37
  • @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. – Steve Jessop Feb 24 '14 at 15:43

1 Answers1

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).

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • "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. – Hans Passant Feb 24 '14 at 15:58
  • @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. – rodrigo Feb 24 '14 at 16:14
  • Thank you both for that clarification :) – Deltgen David Feb 24 '14 at 17:09