title: My stdcall assembler code is unbalancing the stack
body: <p>The _AddPS function unbalances the stack, and is intended to be StdCAll. I have no more information than that, and this is only my second piece of Assembler code (my first was a test .exe that simulated a cdecl function call).</p>
  
  <pre><code>.386
  .MODEL FLAT, STDCALL
  option casemap :none    ; case sensitive
  
  .CODE
  
  _LibMain proc instance:dword, reason:dword, unused:dword
      mov     EAX, 1      ;
      ret
  _LibMain endp
  
  _AddPS proc a:dword, b:dword
      push    ebp         ;
      mov     ebp, esp    ;   // ebp &lt;&lt; esp
      push    EAX         ;
      push    EBX         ;
      mov EAX, [ebp+8]    ;
      mov EBX, [ebp+4]    ;
      add     EAX, EBX    ;
      pop     EBX         ;
      pop     EAX         ;
      mov     esp, ebp    ;
      pop ebp     ;
      RET 8
  _AddPS endp
  END _LibMain
  </code></pre>
  
  <p>Compiled using MASM and the following command line:</p>
  
  <pre><code>c:\Asm\Test.asm /coff /Gz /FeC:\Asm\Test.dll /FoC:\Asm\Test.obj /link /SUBSYSTEM:WINDOWS /link /DLL
  </code></pre>
  
  <p>Edit: The following code works, but I'm hazy on why:</p>
  
  <pre><code>_Test proc a:dword, b:dword
      push    ebp     ; Store the stack frame.
      push    EAX     ; Store EAX. Probably not needed. 
      mov EAX, a      ; Move a into EAX.
      add EAX, b      ; Add b to EAX.
      add esp, 4      ; Move past the stored EAX without popping.
      pop ebp     ; pop the stack frame.
      RET 8 // dword is 4 bytes in .386. We return past the parameters.
  _Test endp // Marks where to stop compiling the function.
  </code></pre>