Calling Conventions
by: G.E. Ozz Nixon Jr.
Published: May 2009
©opyright 2009 by Friends of FPC
When compiling your software to link with external libraries (any OS), or
producing your own libraries, there are conventions to calling external libraries. These calling conventions
define to the compiler how data is passed between the application and library layers. The calling conventions
also define which layer is responsible for cleaning up the stack if used for passing information. My research
notes on name mangling explains how these calling conventions modify the name the exported methods to avoid
conflicts.
Calling Convention
Order of Allocation
Stack or Register
Who clean-up
cdecl
right to left
stack
caller
syscall
right to left
stack
caller
optlink
right to left
stack
caller
pascal
left to right
stack
callee
register
left to right
both
callee
stdcall
right to left
stack
callee
Borland fastcall
left to right
both
callee
Microsofti/GCC fastcall
both directions
both
callee
Borland safecall
right to left
stack
callee
The above chart may be misleading at first, as it looks like there are only a
few combination with just different names/aliases. That assumption is incorrect, the above chart should be
used to know which layer cleans-up the stack and the order in which the parameters are passed. Below I actually
document the internal methods used for these calling conventions, and you will quickly see each one works
at least a little bit different from each other - some are drastically different.
cdecl
The cdecl calling convention is used by many "C" languages for the Intel CPU.
Parameters are pushed on the stack in right-to-left order. Result values are returned using the EAX register
(except for floating point values, which are returned in the x87 register ST0). Registers EBX, ECX and EDX
are available for use in the called routine.
function external_function(a,b,c:Integer):Integer;
external; cdecl;
{...}
d := external_function(a, b, c);
in assembly routine look like:
G.E. Ozz Nixon Jr.