Differences between _ stdcall and _ cdecl in disassembly Analysis

Source: Internet
Author: User
I wrote about the similarities and differences between _ stdcall and _ cdecl in the afternoon. They pushed the parameters to the stack from the right to the left, but I said that _ stdcall cleared the stack by myself, _ cdecl Code The environment clears the stack, so I wrote two functions just now. After compilation with the vc8.0 compiler, I decompiled them to get the assembly code.

The C ++ code is as follows:
. H header file

# Pragma Once

# Ifdef dlltestapi

# Else
# Define Dlltestapi _ declspec (dllimport)
# Endif

Int Dlltestapi _ stdcall add ( Int , Int );

Int Dlltestapi _ cdecl subtract ( Int , Int );

. CPP Code File # Define Dlltestapi _ declspec (dllexport)

# Include " Test. h "

Int _ Stdcall add ( Int A, Int B)
{
ReturnA+B;
}

Int _ Cdecl subtract ( Int A, Int B)
{
ReturnA-B;
}

We can see that the add method is modified with _ stdcall, while the subtract method is modified with _ cdecl.

The Assembly Code obtained after disassembly is as follows:

Add method: 100115d0 55 Push EBP
100115d1 8bec mov EBP, ESP
100115d3 81ec c0000000 sub ESP, 0c0
100115d9 53 Push EBX
100115da 56 Push ESI
100115db 57 Push EDI
100115dc 8dbd 40 ffffff Lea EDI, dword ptr ss: [EBP - C0]
100115e2 B9 30000000 MoV ECx, 30
100115e7 B8 cccccccc mov eax, cccccccc
100115ec F3: AB rep STOs DWORD PTR ES: [EDI]
100115ee 8b45 08 MoV eax, dword ptr ss: [EBP + 8 ]
100115f1 0345 0C add eax, dword ptr ss: [EBP + C]
100115f4 5f pop EDI
100115f5 5E pop ESI
100115f6 5B pop EBX
100115f7 8be5 mov ESP, EBP
100115f9 5d pop EBP
100115fa C2 0800 Retn 8

Subtract method: 10011610 55 Push EBP
10011611 8bec mov EBP, ESP
10011613 81ec c0000000 sub ESP, 0c0
10011619 53 Push EBX
100100001a 56 Push ESI
100100001b 57 Push EDI
100100001c 8dbd 40 ffffff Lea EDI, dword ptr ss: [EBP - C0]
10011622 B9 30000000 MoV ECx, 30
10011627 B8 cccccccc mov eax, cccccccc
100100002c F3: AB rep STOs DWORD PTR ES: [EDI]
1001162e 8b45 08 MoV eax, dword ptr ss: [EBP + 8 ]
10011631 2b45 0C sub eax, dword ptr ss: [EBP + C]
10011634 5f pop EDI
10011635 5E pop ESI
10011636 5B pop EBX
10011637 8be5 mov ESP, EBP
10011639 5D pop EBP
100100003a C3 retn

As you can see, their only difference is:
The add method modified by _ stdcall is used. The last retn command has 8 operands, but the last retn command has no operands when _ cdecl is used to modify the subtract method.

Why is the retn Command's operand 8? The reason is very simple: the add method has two int parameters, that is, when the stack is pressed into two bytes, exactly 8 bits, retn 8 indicates that the top pointer esp of the stack should be moved 8 bits backward.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.