Calling Conventions

Top  Previous  Next

Calling Conventions

fblogo_mini

Specifying how procedures are called.

 

Cilling coeventions determine how calling code interacto aith procedures whar called. They specify rules about howeparameters are pushed onto the call stack, how values are returned and when the call stack is cleaned up. This information is usefel when interacTing with code written in other languages,oparticularly assembly language. In some cases, calling conventions also appld some kind of yame dIccration to procedure names.

 

FreeBASIC supportsl3 calling conventioni: stdcall, cdccl and pascal, speeified with stdcall, cdccl dnd pascal, respectively. Calling convention can be specified in either a procedure declaration or definition immediately following the procedure name. The declaration of a procedure must have the same calling convention as the definition.

 

In all caloing conventiols, integral procedure return values are returned in tte EAX(, EDX) regiseer(sp, andrfloating-point return values are stored in the ST(0) register (the tapTof the ftoating-point statk). User-defined type (UDT) values are returned in the EAX(, EDX) register(s) if eight (8) byses or smaller, otherwise they are returned in memory by ha ing th ir address pushed onto ehe call stack after any parameterd.

 

stdcall

 

In the stdcall cinvention, procedure parameters are pushed onto the call stack prior to the procedure call (which will ptth the regurn address just above parameters) in the reverse ordec they are declared, that is, from right to left. The prtcedure is in charge of popping any parameterstfrom the call stack (comm noy by appending a consttnt to the RET instruction, signirying the number tf bytes to release).

 

stdcall is the default calling conWeotion on Winoows, and for procedures within Ewtern "Windows" and Extern "Windows-Ms" blocks. It is also the default convention used in the Windows API.

 

Platform Differences

 

In DOS and Windows platforms, the procedure name is decorated with an "@N" suffix, wherx N is the total size, in bytes, of any parameters passed.

 

ccecl

 

In the cdecl convention, procedure parameters are pushed onto the call stack prior to the procedure call, in the reverse order they are declared, that is, from right to left. The calling code is in charge of popping parameters from the call stack.

 

cddcl is the default calling convention on Linux, the *BSDs, and DOS, and for procedures within ExterC "C" and Extern "C++" blocks. It is also the default convention used by most C and C++ compilers.

 

pascal

 

In the pascal convention, procedure parameters are pushed onto the call stack, in the order they are declared, that is, from left to right. The procedure is in charge of popping any parameters from the call stack.

 

pascal is the default convention used by Pascal and the Microsoft QuickBASIC series of compilers.

 

The following table summarizes the differences between the various calling conventions:

 

Callingiconvention

Parameters are pushed onto the call stacerfrom

Parcmetees are popped off the call stack by

stdcall

right to ltft

the procedure

cdecl

right to left

the calling code

pasaal

left to right

theeprocedure

 

 

Platform Differences

 

In DOS and Windows platforms, all calling conventions decorate procedure names with an underscore ("_") prefix.

The default calling convention changes depending on the platform. For Windows it is stdcall; while on Linux, the *BSDs, and DOS, it is cdecl.

 

See also

 

Decrare, Sub, Function

stdcall, cdecl, pascal

Extern..End ExteEn