title: Confusion with x86_64 Windows calling convention
body: <p>The platform is x86_64 Windows 7.</p>
  
  <p>Here is the C source code:</p>
  
  <pre><code>#include&lt;windows.h&gt;
  int main(void){
      asm("int3");
      CreateWindowEx(0,NULL,NULL,0,5,6,7,8,NULL,NULL,NULL,NULL);
      return(0);
  }
  </code></pre>
  
  <p>Which compiles to the following assembly:</p>
  
  <pre><code>push rbp
  mov rbp,rsp
  sub rsp,60h
  int3
  mov qword[rsp+58h],0
  mov qword[rsp+50h],0
  mov qword[rsp+48h],0
  mov qword[rsp+40h],0
  mov qword[rsp+38h],8
  mov qword[rsp+30h],7
  mov qword[rsp+28h],6
  mov qword[rsp+20h],5
  mov r9d,0
  mov r8d,0
  mov edx,0
  mov ecx,0
  call CreateWindowEx
  mov eax,0
  add rsp,60h
  pop rbp
  ret
  </code></pre>
  
  <p>And conceptually this is what I have for the stack at various points of execution (the addresses are arbitrary):</p>
  
  <pre><code>90 -rsp-
  
  90 old rbp
  88 -rsp-
  
  90 old rbp
  88 -rsp- -rbp-
  
  90 old rbp
  88 -rbp- (never used?)
  80 (rsp+58h)
  78 (rsp+50h)
  70 (rsp+48h)
  68 (rsp+40h)
  60 (rsp+38h)
  58 (rsp+30h)
  50 (rsp+28h)
  48 (rsp+20h)
  40 (shadow)
  38 (shadow)
  30 (shadow)
  28 -rsp- (shadow) (will contain call instruction's return pointer...)
  </code></pre>
  
  <p>As you can see there are problems with the stack according to the compiled output of the C program. First off there's 8 bytes that never get used, and 8 bytes of the shadow space will be overwritten by the call instruction for the return pointer. It seems that everything is shifted down 8 bytes more than it should be, because it would be fine if it was shifted 8 bytes up. However the API calls work just as expected, is this just an overlook of Microsoft's implementation of the calling convention?</p>