cassemblyx86calling-conventionstdcall

How does this asm for a stdcall function clean args from the stack?


I have simple code. StdCall is __stdcall and CdeclCall is __cdecl.

#include <stdio.h>

int __stdcall StdCall(int a,int b)
{
    return a + b;
}
 
int __cdecl CdeclCall(int a,int b)
{
    return a + b;
}

int main(int argc, char **argv) {

    StdCall(10,20);
    CdeclCall(10,20);
    printf("Done");
    return 0;

}

Part of Disassabmbly of main() for StdCall (Main does not clear stack for StdCall)

push    20                  ; 00000014H
push    10                  ; 0000000aH
call    ?StdCall@@YGHHH@Z           ; StdCall

Part of Disassabmbly of main() for CdeclCall (Main does clear stack for CdeclCall)

push    20                  ; 00000014H
push    10                  ; 0000000aH
call    ?CdeclCall@@YAHHH@Z         ; CdeclCall
add esp, 8   ; Stack cleared here

Now, it is responsibility of StdCall to remove its args from the stack, but disassembly does not show any code which pops the ags to clear / clean the stack.

Disassembly for StdCall

    push    ebp
    mov ebp, esp
    sub esp, 192                ; 000000c0H
    push    ebx
    push    esi
    push    edi
    lea edi, DWORD PTR [ebp-192]
    mov ecx, 48                 ; 00000030H
    mov eax, -858993460             ; ccccccccH
    rep stosd

    mov eax, DWORD PTR _a$[ebp]
    add eax, DWORD PTR _b$[ebp]

    pop edi
    pop esi
    pop ebx
    mov esp, ebp
    pop ebp
    ret 8

Is it runtime activity to generate clearing stack code for __stdcall or I have taken concept wrongly?


Solution

  • It's part of the ret 8 instruction - 8 is the number of bytes to add to the stack pointer after popping the return address.

    https://www.felixcloutier.com/x86/ret

    See also What is the meaning and usage of __stdcall? for more about the @ decorations on C functions names in Windows.