2

As I understand the x64 calling convention in Windows (based on this and this):

  • The first 4 arguments are passed in registers, although 32 bytes of shadow size is reserved in the stack.
  • The overall stack must be 16-byte aligned (although individual arguments don't have to be).
  • Arguments that are 1, 2, 4, or 8 bytes can go on the stack. All other arguments must be passed by reference.

How are individual arguments aligned? Since I couldn't find anything that specifically addressed this, and based on how I thought alignment worked in x86, I assumed that 32-bit ints could be aligned at 4-byte boundaries. So, for example, if function arguments 5 and 6 were both 32-bit ints, I would have expected them to be at stack offsets 32 and 36. However, from looking at my compiler's assembly output, they're at offsets 32 and 40 (i.e., they're 8-byte aligned, even though they take up 4 bytes).

In other words, why does the following function call work in x64, even though it's passing 64-bit uint64_ts when 32-bit ints are expected?

int i = 1;
uint64_t p = 6;
double v = 2.5;
printf("%i %.*f\n%i %.*f\n", i, p, v, i, p, v);
8
  • The should be an ABI or PCS specifying this. Sep 16, 2015 at 16:30
  • Already covered well in many places, like this one. Sep 16, 2015 at 16:38
  • @HansPassant - Thanks. I've read that page and others, but I can't find anything that explicitly states whether 1/2/4-byte values are always 8-byte aligned on the stack. What am I missing or misunderstanding? Sep 16, 2015 at 16:47
  • @HansPassant - Chen's description seems to take it for granted that they are, but I'm trying to find confirmation that this is the case and/or an explanation of why it's taken for granted, since I seem to be missing that. Sep 16, 2015 at 16:48
  • 1
    "Parameters that are smaller than 64 bits are not zero-extended; the upper bits are garbage". That means they are 64 bits. Sep 16, 2015 at 16:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.