Assembly Language for Beginners

(nextflipdebug2) #1

Chapter 6


OS-specific


6.1 Arguments passing methods (calling conventions)


6.1.1 cdecl


This is the most popular method for passing arguments to functions in the C/C++ languages.


Theglscalleralsomustreturnthevalueofthestackpointer(ESP)toitsinitialstateafterthecalleefunction
exits.


Listing 6.1: cdecl

push arg3
push arg2
push arg1
call function
add esp, 12 ; returns ESP


6.1.2 stdcall


It’s almost the same ascdecl, with the exception that thecalleemust setESPto the initial state by
executing theRET xinstruction instead ofRET,
wherex = arguments number * sizeof(int)^1. Thecalleris not adjusting thestack pointer, there are
noadd esp, xinstruction.


Listing 6.2: stdcall

push arg3
push arg2
push arg1
call function


function:
... do something ...
ret 12


The method is ubiquitous in win32 standard libraries, but not in win64 (see below about win64).


Forexample,wecantakethefunctionfrom1.86onpage97andchangeitslightlybyaddingthe__stdcall
modifier:


int __stdcall f2 (int a, int b, int c)
{
return a*b+c;
};


It is to be compiled in almost the same way as1.87 on page 97, but you will seeRET 12instead ofRET.
SPis not updated in thecaller.


(^1) The size of aninttype variable is 4 in x86 systems and 8 in x64 systems

Free download pdf