Assembly Language for Beginners

(nextflipdebug2) #1

6.1. ARGUMENTS PASSING METHODS (CALLING CONVENTIONS)


Forexample,wemaytakethefunctionfrom1.86onpage97andchangeitslightlybyaddinga__fastcall
modifier:


int __fastcall f3 (int a, int b, int c)
{
return a*b+c;
};


Here is how it is to be compiled:


Listing 6.5: Optimizing MSVC 2010 /Ob0

_c$ = 8 ; size = 4
@f3@12 PROC
; _a$ = ecx
; _b$ = edx
mov eax, ecx
imul eax, edx
add eax, DWORD PTR _c$[esp-4]
ret 4
@f3@12 ENDP


; ...


mov edx, 2
push 3
lea ecx, DWORD PTR [edx-1]
call @f3@12
push eax
push OFFSET $SG81390
call _printf
add esp, 8

We see that thecalleereturnsSPby using theRETNinstruction with an operand.


Which implies that the number of arguments can be deduced easily here as well.


GCC regparm


It is the evolution offastcall^2 in some sense. With the-mregparmoption it is possible to set how many
arguments are to be passed via registers (3 is the maximum). Thus, theEAX,EDXandECXregisters are to
be used.


Of course, if the number the of arguments is less than 3, not all 3 registers are to be used.


Thecallerrestores thestack pointerto its initial state.


For example, see (1.22.1 on page 306).


Watcom/OpenWatcom


Here it is called “register calling convention”. The first 4 arguments are passed via theEAX,EDX,EBXand
ECXregisters. All the rest—via the stack.


Thesefunctionshasanunderscoreappendedtothefunctionnameinordertodistinguishthemfromthose
having a different calling convention.


6.1.4 thiscall


This is passing the object’sthispointer to the function-method, in C++.


In MSVC,thisis usually passed in theECXregister.


In GCC, thethispointer is passed as the first function-method argument. Thus it will be visible that all
functions in assembly code have an extra argument, in comparison with the source code.


For an example, see (3.18.1 on page 542).


(^2) http://go.yurichev.com/17040

Free download pdf