INFO: Calling Conventions the 32-Bit Compiler Supports

Last reviewed: September 30, 1997
Article ID: Q100832

The information in this article applies to:
  • Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 4.0, 5.0

SUMMARY

Visual C++,32-bit Edition, supports three calling conventions: C (_cdecl), standard (_stdcall), and fastcall (_fastcall). The compiler does not support the Pascal calling convention (_pascal) provided by Microsoft compilers for MS-DOS and 16-bit Windows.

The following table summarizes the calling conventions:

                          _cdecl         _stdcall       _fastcall
   -------------------------------------------------------------------
   Arguments              Pushed R to L  Pushed R to L  Note 3
   Stack cleaned up by    Caller         Called         Called
   Naming convention      Prepend "_"    Note 1         Note 2

      Note 1: The _stdcall calling convention decorates each function
      name by prepending an underscore character (as the _cdecl
      convention does) and appending an at sign "@" and the decimal
      representation of the number of bytes of stack space required.
      Each argument is widened to a multiple of four bytes.

      Note 2: The _fastcall calling convention decorates each function
      name by prepending an at sign "@" and appending an at sign and
      the decimal representation of the number of bytes of stack space
      required. Each argument is widened to a multiple of four bytes.

      Note 3: The first two function arguments that require four or
      fewer bytes are placed into registers. The caller pushes the
      remainder of the parameters onto the stack from right to left.
      This behavior may change in future versions.

The default calling convention is _cdecl. If an application declares a function that accepts a variable number of arguments with the _fastcall or _stdcall calling convention, the compiler uses the _cdecl calling convention for that function.

MORE INFORMATION

The following code examples illustrate the code generated in the calling function and in the called function to support the C calling convention:

   int _cdecl CFunc(int a, int b);

      calling function    called function
      -------------------------------------------

      push   b            _CFunc PROC NEAR
      push   a                    .
      call   _CFunc               .
      add    esp,8                .
       .                         RET
       .                  _CFunc ENDP
       .

   int _cdecl CVarFunc(int a, ...);

      calling function    called function
      -------------------------------------------

      push   ...          _CVarFunc PROC NEAR
      push   a                       .
      call   _CVarFunc               .
      add    esp,4+...               .
       .                            RET
       .                  _CVarFunc ENDP
       .

The following code example illustrates the code generated in the calling function and in the called function to support the standard calling convention:

   int _stdcall StdFunc(int a, int b);

      calling function    called function
      -------------------------------------------

      push   b            _StdFunc@8 PROC NEAR
      push   a                        .
      call   _StdFunc@8               .
       .                              .
       .                             RET  8
       .                  _StdFunc@8 ENDP


The following code example illustrates the code generated in the calling function and in the called function to support the fastcall calling convention:

   int _fastcall FastFunc(int a, int b);

      calling function    called function
      -------------------------------------------

      mov edx, b          @FastFunc@8 PROC NEAR
      mov ecx, a                       .
      call @FastFunc@8                 .
       .                               .
       .                              RET 8
       .                  @FastFunc@8 ENDP
Keywords          : CLIss kbfasttip
Version           : WINDOWS NT:1.0,2.0,4.0,5.0;
Platform          : NT WINDOWS
Issue type        : kbinfo


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.