6

I've read Microsoft's documentation, but the scheme is so awkward, I thought I'd double-check to make sure I'm understanding it correctly...

My understanding is the generic method by which parameters are passed is this:

--- bottom of stack ---
(return address)
[shadow space for arg 1]
[shadow space for arg 2]
[shadow space for arg 3]
[shadow space for arg 4]
arg N
arg N - 1
arg N - 2
...
arg 6
arg 5
---- top of stack -----

It seems so awkward when implementing va_arg and such... is this actually correct?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • That can't be right: the shadow space for args 1-4 is adjacent to the return address, according to http://msdn.microsoft.com/en-us/library/ew5tede7 – Harry Johnston Aug 23 '12 at 05:05
  • Also the return address is at the bottom of the stack, not the top. – Harry Johnston Aug 23 '12 at 05:06
  • @HarryJohnston: Oh my bad, you're right, thanks... not sure what I was thinking there; fixed. Are the rest correct? – user541686 Aug 23 '12 at 05:09
  • The documentation doesn't seem to say which way around the stack parameters are ordered, but my guess is that it is in the same order as in x86, i.e., with arg 5 next to arg 4 and arg N at the top. It would certainly make more sense. – Harry Johnston Aug 23 '12 at 05:24
  • @HarryJohnston: Yeah that's why I asked, but at the same time [Wikipedia says](http://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention) the parameters after the 5th are pushed "right to left", so it seems to be saying it's like what I have here... that's why I'm confused, it just seems awkward. – user541686 Aug 23 '12 at 05:34
  • No, pushing right to left puts the rightmost argument (arg N) at the top of the stack. The stack grows downwards, remember. – Harry Johnston Aug 23 '12 at 05:46
  • @HarryJohnston: Looks like Raymond got it. Seems like the return address was correct after all... interesting. – user541686 Aug 23 '12 at 05:47
  • What you're calling the top of the stack I was calling the bottom of the stack. I did wonder why your diagram was upside down ... – Harry Johnston Aug 23 '12 at 05:59
  • My mistake. Sorry for the confusion. – Harry Johnston Aug 23 '12 at 06:04

1 Answers1

7

The correct diagram is

--- Bottom of stack ---    RSP + size     (higher addresses)
arg N
arg N - 1
arg N - 2
...
arg 6
arg 5
[shadow space for arg 4]
[shadow space for arg 3]
[shadow space for arg 2]
[shadow space for arg 1]
(return address)
---- Top of stack -----    RSP            (lower addresses)
[grows downward]

The return address is at the top of the stack (most recently pushed), followed by shadow space for the first four parameters, followed by parameters 5 and onward.

The parameters are pushed right to left: The last parameter (N) is pushed first, so it is closest to the bottom of the stack.

user541686
  • 205,094
  • 128
  • 528
  • 886
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • Hold on - the stack grows downwards, doesn't it, so the first item to be pushed is closest to the *top*? As in the second sentence here: http://msdn.microsoft.com/en-us/library/ew5tede7 – Harry Johnston Aug 23 '12 at 05:50
  • @HarryJohnston Since stacks grow downward, the item at the top of the stack (most recently pushed) has the lowest address. It is at the bottom of the stack frame after you subtract space for local variables. Stack frame = parameters [bottom], return address [middle], locals [top]. See the diagram. You're confusing the parameter space with the entire stack frame. – Raymond Chen Aug 23 '12 at 05:56
  • Yes, I definitely had my terminology confused. I'm still slightly puzzled about the meaning of the sentence in the referenced MSDN article - does it mean that the parameter area is at the bottom of the stack *from the point of view of the callee*? – Harry Johnston Aug 23 '12 at 06:09
  • @HarryJohnston Right. After the callee sets up the frame, the parameters are at the bottom, followed by the return address, then the local variables, and then at the top is the parameter space for functions to be called. – Raymond Chen Aug 23 '12 at 07:32