<s>
This	O
article	O
describes	O
the	O
calling	O
conventions	O
used	O
when	O
programming	O
x86	B-Operating_System
architecture	I-Operating_System
microprocessors	B-Architecture
.	O
</s>
<s>
Another	O
closely	O
related	O
topic	O
is	O
name	B-Language
mangling	I-Language
,	O
which	O
determines	O
how	O
symbol	O
names	O
in	O
the	O
code	O
are	O
mapped	O
to	O
symbol	O
names	O
used	O
by	O
the	O
linker	O
.	O
</s>
<s>
Calling	O
conventions	O
,	O
type	O
representations	O
,	O
and	O
name	B-Language
mangling	I-Language
are	O
all	O
part	O
of	O
what	O
is	O
known	O
as	O
an	O
application	B-Operating_System
binary	I-Operating_System
interface	I-Operating_System
(	O
ABI	B-Operating_System
)	O
.	O
</s>
<s>
There	O
are	O
subtle	O
differences	O
in	O
how	O
various	O
compilers	B-Language
implement	O
these	O
conventions	O
,	O
so	O
it	O
is	O
often	O
difficult	O
to	O
interface	O
code	O
which	O
is	O
compiled	B-Language
by	O
different	O
compilers	B-Language
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
conventions	O
which	O
are	O
used	O
as	O
an	O
API	B-General_Concept
standard	O
(	O
such	O
as	O
stdcall	O
)	O
are	O
very	O
uniformly	O
implemented	O
.	O
</s>
<s>
Prior	O
to	O
microcomputers	B-Architecture
,	O
the	O
machine	O
manufacturer	O
generally	O
provided	O
an	O
operating	B-General_Concept
system	I-General_Concept
and	O
compilers	B-Language
for	O
several	O
programming	O
languages	O
.	O
</s>
<s>
Early	O
microcomputers	B-Architecture
before	O
the	O
Commodore	O
Pet	O
and	O
Apple	B-Device
II	I-Device
generally	O
came	O
without	O
an	O
OS	O
or	O
compilers	B-Language
.	O
</s>
<s>
The	O
IBM	B-Device
PC	I-Device
came	O
with	O
Microsoft	O
's	O
fore-runner	O
to	O
Windows	B-Application
,	O
the	O
Disk	O
Operating	B-General_Concept
System	I-General_Concept
(	O
DOS	B-Device
)	O
,	O
but	O
it	O
did	O
not	O
come	O
with	O
a	O
compiler	B-Language
.	O
</s>
<s>
The	O
only	O
hardware	O
standard	O
for	O
IBM	O
PC-compatible	O
machines	O
was	O
defined	O
by	O
the	O
Intel	B-Device
processors	I-Device
(	O
8086	B-General_Concept
,	O
80386	O
)	O
and	O
the	O
literal	O
hardware	O
IBM	O
shipped	O
.	O
</s>
<s>
Hardware	O
extensions	O
and	O
all	O
software	O
standards	O
(	O
save	O
for	O
a	O
BIOS	B-Operating_System
calling	O
convention	O
)	O
were	O
thrown	O
open	O
to	O
market	O
competition	O
.	O
</s>
<s>
A	O
multitude	O
of	O
independent	O
software	O
firms	O
offered	O
operating	B-General_Concept
systems	I-General_Concept
,	O
compilers	B-Language
for	O
many	O
programming	O
languages	O
,	O
and	O
applications	O
.	O
</s>
<s>
After	O
the	O
IBM-compatible	O
market	O
shakeout	O
,	O
Microsoft	O
operating	B-General_Concept
systems	I-General_Concept
and	O
programming	O
tools	O
(	O
with	O
differing	O
conventions	O
)	O
predominated	O
,	O
while	O
second-tier	O
firms	O
like	O
Borland	O
and	O
Novell	O
,	O
and	O
open-source	O
projects	O
like	O
GCC	B-Application
,	O
still	O
maintained	O
their	O
own	O
standards	O
.	O
</s>
<s>
In	O
these	O
conventions	O
,	O
the	O
caller	O
cleans	O
the	O
arguments	O
from	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
The	O
cdecl	O
(	O
which	O
stands	O
for	O
C	B-Language
declaration	O
)	O
is	O
a	O
calling	O
convention	O
for	O
the	O
C	B-Language
programming	I-Language
language	I-Language
and	O
is	O
used	O
by	O
many	O
C	B-Language
compilers	B-Language
for	O
the	O
x86	B-Operating_System
architecture	I-Operating_System
.	O
</s>
<s>
In	O
cdecl	O
,	O
subroutine	O
arguments	O
are	O
passed	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
Integer	O
values	O
and	O
memory	O
addresses	O
are	O
returned	O
in	O
the	O
EAX	O
register	B-General_Concept
,	O
floating	O
point	O
values	O
in	O
the	O
ST0	O
x87	B-Application
register	B-General_Concept
.	O
</s>
<s>
The	O
x87	B-Application
floating	O
point	O
registers	O
ST0	O
to	O
ST7	O
must	O
be	O
empty	O
(	O
popped	O
or	O
freed	O
)	O
when	O
calling	O
a	O
new	O
function	O
,	O
and	O
ST1	O
to	O
ST7	O
must	O
be	O
empty	O
on	O
exiting	O
a	O
function	O
.	O
</s>
<s>
In	O
the	O
context	O
of	O
the	O
C	B-Language
programming	I-Language
language	I-Language
,	O
function	O
arguments	O
are	O
pushed	O
on	O
the	O
stack	B-General_Concept
in	O
the	O
right-to-left	O
order	O
,	O
i.e.	O
</s>
<s>
Consider	O
the	O
following	O
C	B-Language
source	O
code	O
snippet	O
:	O
</s>
<s>
On	O
x86	B-Operating_System
,	O
it	O
might	O
produce	O
the	O
following	O
assembly	B-Language
code	I-Language
(	O
Intel	O
syntax	O
)	O
:	O
</s>
<s>
The	O
caller	O
cleans	O
the	O
stack	B-General_Concept
after	O
the	O
function	O
call	O
returns	O
.	O
</s>
<s>
The	O
cdecl	O
calling	O
convention	O
is	O
usually	O
the	O
default	O
calling	O
convention	O
for	O
x86	B-Operating_System
C	B-Language
compilers	B-Language
,	O
although	O
many	O
compilers	B-Language
provide	O
options	O
to	O
automatically	O
change	O
the	O
calling	O
conventions	O
used	O
.	O
</s>
<s>
As	O
a	O
result	O
,	O
x86	B-Operating_System
programs	O
compiled	B-Language
for	O
different	O
operating	B-General_Concept
system	I-General_Concept
platforms	O
and/or	O
by	O
different	O
compilers	B-Language
can	O
be	O
incompatible	O
,	O
even	O
if	O
they	O
both	O
use	O
the	O
"	O
cdecl	O
"	O
convention	O
and	O
do	O
not	O
call	O
out	O
to	O
the	O
underlying	O
environment	O
.	O
</s>
<s>
In	O
regard	O
to	O
how	O
to	O
return	O
values	O
,	O
some	O
compilers	B-Language
return	O
simple	O
data	O
structures	O
with	O
a	O
length	O
of	O
2	O
registers	O
or	O
less	O
in	O
the	O
register	B-General_Concept
pair	O
EAX:EDX	O
,	O
and	O
larger	O
structures	O
and	O
class	O
objects	O
requiring	O
special	O
treatment	O
by	O
the	O
exception	O
handler	O
(	O
e.g.	O
,	O
a	O
defined	O
constructor	O
,	O
destructor	O
,	O
or	O
assignment	O
)	O
are	O
returned	O
in	O
memory	O
.	O
</s>
<s>
In	O
Linux	B-Application
,	O
GCC	B-Application
sets	O
the	O
de	O
facto	O
standard	O
for	O
calling	O
conventions	O
.	O
</s>
<s>
Since	O
GCC	B-Application
version	O
4.5	O
,	O
the	O
stack	B-General_Concept
must	O
be	O
aligned	O
to	O
a	O
16-byte	O
boundary	O
when	O
calling	O
a	O
function	O
(	O
previous	O
versions	O
only	O
required	O
a	O
4-byte	O
alignment	O
)	O
.	O
</s>
<s>
A	O
version	O
of	O
cdecl	O
is	O
described	O
in	O
System	B-Operating_System
V	I-Operating_System
ABI	B-Operating_System
for	O
i386	O
systems	O
.	O
</s>
<s>
Syscall	O
is	O
the	O
standard	O
calling	O
convention	O
for	O
32	O
bit	O
OS/2	B-Application
API	I-Application
.	O
</s>
<s>
The	O
three	O
first	O
(	O
leftmost	O
)	O
arguments	O
are	O
passed	O
in	O
EAX	O
,	O
EDX	O
,	O
and	O
ECX	O
and	O
up	O
to	O
four	O
floating-point	O
arguments	O
are	O
passed	O
in	O
ST0	O
through	O
ST3	O
,	O
although	O
space	O
for	O
them	O
is	O
reserved	O
in	O
the	O
argument	O
list	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
Optlink	O
is	O
used	O
by	O
the	O
IBM	B-Language
VisualAge	I-Language
compilers	B-Language
.	O
</s>
<s>
In	O
these	O
conventions	O
,	O
the	O
callee	O
cleans	O
up	O
the	O
arguments	O
from	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
Functions	O
which	O
use	O
these	O
conventions	O
are	O
easy	O
to	O
recognize	O
in	O
ASM	O
code	O
because	O
they	O
will	O
unwind	O
the	O
stack	B-General_Concept
after	O
returning	O
.	O
</s>
<s>
The	O
x86	B-Operating_System
instruction	O
allows	O
an	O
optional	O
16-bit	O
parameter	O
that	O
specifies	O
the	O
number	O
of	O
stack	B-General_Concept
bytes	O
to	O
release	O
after	O
returning	O
to	O
the	O
caller	O
.	O
</s>
<s>
Conventions	O
entitled	O
fastcall	O
or	O
register	B-General_Concept
have	O
not	O
been	O
standardized	O
,	O
and	O
have	O
been	O
implemented	O
differently	O
,	O
depending	O
on	O
the	O
compiler	B-Language
vendor	O
.	O
</s>
<s>
Typically	O
register	B-General_Concept
based	O
calling	O
conventions	O
pass	O
one	O
or	O
more	O
arguments	O
in	O
registers	O
which	O
reduces	O
the	O
number	O
of	O
memory	O
accesses	O
required	O
for	O
the	O
call	O
and	O
thus	O
make	O
them	O
usually	O
faster	O
.	O
</s>
<s>
for	O
removing	O
them	O
from	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
This	O
calling	O
convention	O
was	O
common	O
in	O
the	O
following	O
16-bit	O
APIs	B-General_Concept
:	O
OS/2	B-Application
1.x	O
,	O
Microsoft	B-Application
Windows	I-Application
3.x	O
,	O
and	O
Borland	B-Language
Delphi	I-Language
version	O
1.x	O
.	O
</s>
<s>
Modern	O
versions	O
of	O
the	O
Windows	B-Library
API	I-Library
use	O
stdcall	O
,	O
which	O
still	O
has	O
the	O
callee	O
restoring	O
the	O
stack	B-General_Concept
as	O
in	O
the	O
Pascal	B-Application
convention	O
,	O
but	O
the	O
parameters	O
are	O
now	O
pushed	O
right	O
to	O
left	O
.	O
</s>
<s>
The	O
stdcall	O
calling	O
convention	O
is	O
a	O
variation	O
on	O
the	O
Pascal	B-Application
calling	O
convention	O
in	O
which	O
the	O
callee	O
is	O
responsible	O
for	O
cleaning	O
up	O
the	O
stack	B-General_Concept
,	O
but	O
the	O
parameters	O
are	O
pushed	O
onto	O
the	O
stack	B-General_Concept
in	O
right-to-left	O
order	O
,	O
as	O
in	O
the	O
_cdecl	O
calling	O
convention	O
.	O
</s>
<s>
Return	O
values	O
are	O
stored	O
in	O
the	O
EAX	O
register	B-General_Concept
.	O
</s>
<s>
stdcall	O
is	O
the	O
standard	O
calling	O
convention	O
for	O
the	O
Microsoft	O
Win32	B-Library
API	I-Library
and	O
for	O
Open	B-Application
Watcom	I-Application
C++	I-Application
.	O
</s>
<s>
Remaining	O
arguments	O
are	O
pushed	O
onto	O
the	O
stack	B-General_Concept
from	O
right	O
to	O
left	O
.	O
</s>
<s>
When	O
the	O
compiler	B-Language
compiles	B-Language
for	O
IA64	B-General_Concept
or	O
AMD64	B-Device
,	O
it	O
ignores	O
the	O
keyword	O
and	O
uses	O
the	O
one	O
64-bit	O
calling	O
convention	O
instead	O
.	O
</s>
<s>
Other	O
compilers	B-Language
like	O
GCC	B-Application
,	O
Clang	B-Application
,	O
and	O
ICC	B-Language
provide	O
similar	O
"	O
fastcall	O
"	O
calling	O
conventions	O
,	O
although	O
they	O
are	O
not	O
necessarily	O
compatible	O
with	O
each	O
other	O
or	O
with	O
Microsoft	O
fastcall	O
.	O
</s>
<s>
Consider	O
the	O
following	O
C	B-Language
snippet:x86	O
decompilation	O
of	O
the	O
main	O
function	O
will	O
look	O
like	O
(	O
in	O
Intel	O
syntax	O
)	O
:	O
</s>
<s>
The	O
first	O
two	O
arguments	O
are	O
passed	O
in	O
the	O
left	O
to	O
right	O
order	O
,	O
and	O
the	O
third	O
argument	O
is	O
pushed	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
There	O
is	O
no	O
stack	B-General_Concept
cleanup	O
,	O
as	O
stack	B-General_Concept
cleanup	O
is	O
performed	O
by	O
the	O
callee	O
.	O
</s>
<s>
As	O
the	O
two	O
arguments	O
were	O
passed	O
through	O
the	O
registers	O
and	O
only	O
one	O
parameter	O
was	O
pushed	O
in	O
the	O
stack	B-General_Concept
,	O
the	O
pushed	O
value	O
is	O
being	O
cleared	O
by	O
the	O
retn	O
instruction	O
,	O
as	O
int	O
is	O
4	O
bytes	O
in	O
size	O
in	O
x86	B-Operating_System
systems	O
.	O
</s>
<s>
In	O
Visual	B-Application
Studio	I-Application
2013	O
,	O
Microsoft	O
introduced	O
the	O
calling	O
convention	O
in	O
response	O
to	O
efficiency	O
concerns	O
from	O
game	O
,	O
graphic	O
,	O
video/audio	O
,	O
and	O
codec	O
developers	O
.	O
</s>
<s>
The	O
scheme	O
allows	O
for	O
larger	O
vector	O
types	O
(	O
,	O
,	O
,	O
)	O
to	O
be	O
passed	O
in	O
registers	O
as	O
opposed	O
to	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
For	O
IA-32	B-Device
and	O
x64	B-Device
code	O
,	O
is	O
similar	O
to	O
and	O
the	O
original	O
x64	B-Device
calling	O
conventions	O
respectively	O
,	O
but	O
extends	O
them	O
to	O
support	O
passing	O
vector	O
arguments	O
using	O
SIMD	B-Device
registers	O
.	O
</s>
<s>
In	O
IA-32	B-Device
,	O
the	O
integer	O
values	O
are	O
passed	O
as	O
usual	O
,	O
and	O
the	O
first	O
six	O
SIMD	B-Device
(	O
XMM/YMM0	O
-5	O
)	O
registers	O
hold	O
up	O
to	O
six	O
floating-point	O
,	O
vector	O
,	O
or	O
HVA	O
values	O
sequentially	O
from	O
left	O
to	O
right	O
,	O
regardless	O
of	O
actual	O
positions	O
caused	O
by	O
,	O
e.g.	O
</s>
<s>
In	O
x64	B-Device
,	O
however	O
,	O
the	O
rule	O
from	O
the	O
original	O
x64	B-Device
convention	O
still	O
apply	O
,	O
so	O
that	O
XMM/YMM0	O
-5	O
only	O
hold	O
floating-point	O
,	O
vector	O
,	O
or	O
HVA	O
arguments	O
when	O
they	O
happen	O
to	O
be	O
the	O
first	O
through	O
the	O
sixth	O
.	O
</s>
<s>
The	O
Clang	B-Application
compiler	B-Language
and	O
the	O
Intel	B-Language
C++	I-Language
Compiler	I-Language
also	O
implement	O
vectorcall	O
.	O
</s>
<s>
ICC	B-Language
has	O
a	O
similar	O
,	O
earlier	O
convention	O
called	O
;	O
it	O
is	O
also	O
supported	O
by	O
Clang	B-Application
.	O
</s>
<s>
Remaining	O
arguments	O
are	O
pushed	O
onto	O
the	O
stack	B-General_Concept
,	O
also	O
left	O
to	O
right	O
.	O
</s>
<s>
It	O
is	O
the	O
default	O
calling	O
convention	O
of	O
the	O
32-bit	O
compiler	B-Language
of	O
Delphi	B-Language
,	O
where	O
it	O
is	O
known	O
as	O
register	B-General_Concept
.	O
</s>
<s>
This	O
calling	O
convention	O
is	O
also	O
used	O
by	O
Embarcadero	O
's	O
C++Builder	O
,	O
where	O
it	O
is	O
called	O
__fastcall	O
.	O
</s>
<s>
In	O
this	O
compiler	B-Language
,	O
Microsoft	O
's	O
fastcall	O
can	O
be	O
used	O
as	O
__msfastcall	O
.	O
</s>
<s>
GCC	B-Application
and	O
Clang	B-Application
can	O
be	O
made	O
to	O
use	O
a	O
similar	O
calling	O
convention	O
by	O
using	O
with	O
the	O
function	O
attribute	O
or	O
the	O
switch	O
.	O
</s>
<s>
(	O
The	O
stack	B-General_Concept
order	O
is	O
inverted	O
.	O
)	O
</s>
<s>
A	O
-based	O
version	O
is	O
used	O
by	O
the	O
Linux	B-Application
kernel	O
on	O
i386	O
since	O
version	O
2.6.20	O
(	O
released	O
February	O
2007	O
)	O
.	O
</s>
<s>
The	O
register	B-General_Concept
calling	O
convention	O
may	O
be	O
selected	O
by	O
command	O
line	O
switch	O
.	O
</s>
<s>
(	O
However	O
,	O
IDA	B-Application
uses	O
__fastcall	O
anyway	O
for	O
uniformity	O
.	O
)	O
</s>
<s>
If	O
any	O
argument	O
cannot	O
be	O
assigned	O
to	O
a	O
register	B-General_Concept
(	O
say	O
it	O
is	O
too	O
large	O
)	O
it	O
,	O
and	O
all	O
subsequent	O
arguments	O
,	O
are	O
assigned	O
to	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
Arguments	O
assigned	O
to	O
the	O
stack	B-General_Concept
are	O
pushed	O
from	O
right	O
to	O
left	O
.	O
</s>
<s>
Variadic	B-Language
functions	I-Language
fall	O
back	O
to	O
the	O
Watcom	O
stack	B-General_Concept
based	O
calling	O
convention	O
.	O
</s>
<s>
The	O
Watcom	B-Application
C/C	I-Application
++	I-Application
compiler	I-Application
also	O
uses	O
the	O
directive	O
that	O
allows	O
the	O
user	O
to	O
specify	O
their	O
own	O
calling	O
convention	O
.	O
</s>
<s>
Floating	O
point	O
parameters	O
are	O
passed	O
on	O
the	O
floating	O
point	O
stack	B-General_Concept
–	O
registers	O
st0	O
,	O
st1	O
,	O
st2	O
,	O
st3	O
,	O
st4	O
,	O
st5	O
and	O
st6	O
.	O
</s>
<s>
Structure	O
parameters	O
are	O
always	O
passed	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
Additional	O
parameters	O
are	O
passed	O
on	O
the	O
stack	B-General_Concept
after	O
registers	O
are	O
exhausted	O
.	O
</s>
<s>
In	O
Delphi	B-Language
and	O
Free	B-Operating_System
Pascal	I-Operating_System
on	O
Microsoft	B-Application
Windows	I-Application
,	O
the	O
safecall	O
calling	O
convention	O
encapsulates	O
COM	O
(	O
Component	B-Application
Object	I-Application
Model	I-Application
)	O
error	O
handling	O
,	O
thus	O
exceptions	O
are	O
n't	O
leaked	O
out	O
to	O
the	O
caller	O
,	O
but	O
are	O
reported	O
in	O
the	O
HRESULT	O
return	O
value	O
,	O
as	O
required	O
by	O
COM/OLE	O
.	O
</s>
<s>
When	O
calling	O
a	O
safecall	O
function	O
from	O
Delphi	B-Language
code	I-Language
,	O
Delphi	B-Language
also	O
automatically	O
checks	O
the	O
returned	O
HRESULT	O
and	O
raises	O
an	O
exception	O
if	O
necessary	O
.	O
</s>
<s>
The	O
safecall	O
calling	O
convention	O
is	O
the	O
same	O
as	O
the	O
stdcall	O
calling	O
convention	O
,	O
except	O
that	O
exceptions	O
are	O
passed	O
back	O
to	O
the	O
caller	O
in	O
EAX	O
as	O
a	O
HResult	O
(	O
instead	O
of	O
in	O
FS:[0]	O
)	O
,	O
while	O
the	O
function	O
result	O
is	O
passed	O
by	O
reference	O
on	O
the	O
stack	B-General_Concept
as	O
though	O
it	O
were	O
a	O
final	O
"	O
out	O
"	O
parameter	O
.	O
</s>
<s>
When	O
calling	O
a	O
Delphi	B-Language
function	O
from	O
Delphi	B-Language
this	O
calling	O
convention	O
will	O
appear	O
just	O
like	O
any	O
other	O
calling	O
convention	O
,	O
because	O
although	O
exceptions	O
are	O
passed	O
back	O
in	O
EAX	O
,	O
they	O
are	O
automatically	O
converted	O
back	O
to	O
proper	O
exceptions	O
by	O
the	O
caller	O
.	O
</s>
<s>
When	O
using	O
COM	B-Application
objects	I-Application
created	O
in	O
other	O
languages	O
,	O
the	O
HResults	O
will	O
be	O
automatically	O
raised	O
as	O
exceptions	O
,	O
and	O
the	O
result	O
for	O
Get	O
functions	O
is	O
in	O
the	O
result	O
rather	O
than	O
a	O
parameter	O
.	O
</s>
<s>
When	O
creating	O
COM	B-Application
objects	I-Application
in	O
Delphi	B-Language
with	O
safecall	O
,	O
there	O
is	O
no	O
need	O
to	O
worry	O
about	O
HResults	O
,	O
as	O
exceptions	O
can	O
be	O
raised	O
as	O
normal	O
but	O
will	O
be	O
seen	O
as	O
HResults	O
in	O
other	O
languages	O
.	O
</s>
<s>
Returns	O
a	O
result	O
and	O
raises	O
exceptions	O
like	O
a	O
normal	O
Delphi	B-Language
function	O
,	O
but	O
it	O
passes	O
values	O
and	O
exceptions	O
as	O
though	O
it	O
was	O
:	O
</s>
<s>
This	O
calling	O
convention	O
is	O
used	O
for	O
calling	O
C++	O
non-static	O
member	O
functions	O
.	O
</s>
<s>
There	O
are	O
two	O
primary	O
versions	O
of	O
thiscall	O
used	O
depending	O
on	O
the	O
compiler	B-Language
and	O
whether	O
or	O
not	O
the	O
function	O
uses	O
a	O
variable	O
number	O
of	O
arguments	O
.	O
</s>
<s>
For	O
the	O
GCC	B-Application
compiler	I-Application
,	O
thiscall	O
is	O
almost	O
identical	O
to	O
cdecl	O
:	O
The	O
caller	O
cleans	O
the	O
stack	B-General_Concept
,	O
and	O
the	O
parameters	O
are	O
passed	O
in	O
right-to-left	O
order	O
.	O
</s>
<s>
The	O
difference	O
is	O
the	O
addition	O
of	O
the	O
this	B-Application
pointer	I-Application
,	O
which	O
is	O
pushed	O
onto	O
the	O
stack	B-General_Concept
last	O
,	O
as	O
if	O
it	O
were	O
the	O
first	O
parameter	O
in	O
the	O
function	O
prototype	O
.	O
</s>
<s>
On	O
the	O
Microsoft	B-Application
Visual	I-Application
C++	I-Application
compiler	B-Language
,	O
the	O
this	B-Application
pointer	I-Application
is	O
passed	O
in	O
ECX	O
and	O
it	O
is	O
the	O
callee	O
that	O
cleans	O
the	O
stack	B-General_Concept
,	O
mirroring	O
the	O
stdcall	O
convention	O
used	O
in	O
C	B-Language
for	O
this	O
compiler	B-Language
and	O
in	O
Windows	B-Library
API	I-Library
functions	O
.	O
</s>
<s>
When	O
functions	O
use	O
a	O
variable	O
number	O
of	O
arguments	O
,	O
it	O
is	O
the	O
caller	O
that	O
cleans	O
the	O
stack	B-General_Concept
(	O
cf	O
.	O
</s>
<s>
The	O
thiscall	O
calling	O
convention	O
can	O
only	O
be	O
explicitly	O
specified	O
on	O
Microsoft	B-Application
Visual	I-Application
C++	I-Application
2005	O
and	O
later	O
.	O
</s>
<s>
On	O
any	O
other	O
compiler	B-Language
thiscall	O
is	O
not	O
a	O
keyword	O
.	O
</s>
<s>
(	O
However	O
,	O
disassemblers	O
,	O
such	O
as	O
IDA	B-Application
,	O
must	O
specify	O
it	O
.	O
</s>
<s>
So	O
IDA	B-Application
uses	O
keyword	O
__thiscall	O
for	O
this	O
.	O
)	O
</s>
<s>
According	O
to	O
the	O
Intel	O
ABI	B-Operating_System
to	O
which	O
the	O
vast	O
majority	O
of	O
compilers	B-Language
conform	O
,	O
the	O
EAX	O
,	O
EDX	O
,	O
and	O
ECX	O
are	O
to	O
be	O
free	O
for	O
use	O
within	O
a	O
procedure	O
or	O
function	O
,	O
and	O
need	O
not	O
be	O
preserved	O
.	O
</s>
<s>
Therefore	O
,	O
it	O
is	O
the	O
caller	O
's	O
responsibility	O
to	O
push	O
each	O
of	O
these	O
registers	O
onto	O
the	O
stack	B-General_Concept
,	O
if	O
it	O
would	O
like	O
to	O
restore	O
their	O
values	O
after	O
a	O
subroutine	O
call	O
.	O
</s>
<s>
x86-64	B-Device
calling	O
conventions	O
take	O
advantage	O
of	O
the	O
additional	O
register	B-General_Concept
space	O
to	O
pass	O
more	O
arguments	O
in	O
registers	O
.	O
</s>
<s>
The	O
Microsoft	O
x64	B-Device
calling	O
convention	O
is	O
followed	O
on	O
Windows	B-Application
and	O
pre-boot	O
UEFI	B-Architecture
(	O
for	O
long	B-Application
mode	I-Application
on	O
x86-64	B-Device
)	O
.	O
</s>
<s>
Additional	O
arguments	O
are	O
pushed	O
onto	O
the	O
stack	B-General_Concept
(	O
right	O
to	O
left	O
)	O
.	O
</s>
<s>
Integer	O
return	O
values	O
(	O
similar	O
to	O
x86	B-Operating_System
)	O
are	O
returned	O
in	O
RAX	O
if	O
64	O
bits	O
or	O
less	O
.	O
</s>
<s>
When	O
compiling	B-Language
for	O
the	O
x64	B-Device
architecture	I-Device
in	O
a	O
Windows	B-Application
context	O
(	O
whether	O
using	O
Microsoft	O
or	O
non-Microsoft	O
tools	O
)	O
,	O
stdcall	O
,	O
thiscall	O
,	O
cdecl	O
,	O
and	O
fastcall	O
all	O
resolve	O
to	O
using	O
this	O
convention	O
.	O
</s>
<s>
In	O
the	O
Microsoft	O
x64	B-Device
calling	O
convention	O
,	O
it	O
is	O
the	O
caller	O
's	O
responsibility	O
to	O
allocate	O
32	O
bytes	O
of	O
"	O
shadow	O
space	O
"	O
on	O
the	O
stack	B-General_Concept
right	O
before	O
calling	O
the	O
function	O
(	O
regardless	O
of	O
the	O
actual	O
number	O
of	O
parameters	O
used	O
)	O
,	O
and	O
to	O
pop	O
the	O
stack	B-General_Concept
after	O
the	O
call	O
.	O
</s>
<s>
So	O
when	O
the	O
called	O
function	O
is	O
entered	O
,	O
the	O
stack	B-General_Concept
will	O
be	O
composed	O
of	O
(	O
in	O
ascending	O
order	O
)	O
the	O
return	O
address	O
,	O
followed	O
by	O
the	O
shadow	O
space	O
(	O
32	O
bytes	O
)	O
followed	O
by	O
the	O
fifth	O
parameter	O
.	O
</s>
<s>
In	O
x86-64	B-Device
,	O
Visual	B-Application
Studio	I-Application
2008	I-Application
stores	O
floating	O
point	O
numbers	O
in	O
XMM6	O
and	O
XMM7	O
(	O
as	O
well	O
as	O
XMM8	O
through	O
XMM15	O
)	O
;	O
consequently	O
,	O
for	O
x86-64	B-Device
,	O
user-written	O
assembly	O
language	O
routines	O
must	O
preserve	O
XMM6	O
and	O
XMM7	O
(	O
as	O
compared	O
to	O
x86	B-Operating_System
wherein	O
user-written	O
assembly	O
language	O
routines	O
did	O
not	O
need	O
to	O
preserve	O
XMM6	O
and	O
XMM7	O
)	O
.	O
</s>
<s>
In	O
other	O
words	O
,	O
user-written	O
assembly	O
language	O
routines	O
must	O
be	O
updated	O
to	O
save/restore	O
XMM6	O
and	O
XMM7	O
before/after	O
the	O
function	O
when	O
being	O
ported	O
from	O
x86	B-Operating_System
to	O
x86-64	B-Device
.	O
</s>
<s>
Starting	O
with	O
Visual	B-Application
Studio	I-Application
2013	O
,	O
Microsoft	O
introduced	O
the	O
calling	O
convention	O
which	O
extends	O
the	O
x64	B-Device
convention	O
.	O
</s>
<s>
The	O
calling	O
convention	O
of	O
the	O
System	B-Operating_System
V	I-Operating_System
AMD64	B-Device
ABI	B-Operating_System
is	O
followed	O
on	O
Solaris	B-Application
,	O
Linux	B-Application
,	O
FreeBSD	B-Operating_System
,	O
macOS	B-Application
,	O
and	O
is	O
the	O
de	O
facto	O
standard	O
among	O
Unix	O
and	O
Unix-like	O
operating	B-General_Concept
systems	I-General_Concept
.	O
</s>
<s>
The	O
OpenVMS	B-Operating_System
Calling	O
Standard	O
on	O
x86-64	B-Device
is	O
based	O
on	O
the	O
System	B-Operating_System
V	I-Operating_System
ABI	B-Operating_System
with	O
some	O
extensions	O
needed	O
for	O
backwards	O
compatibility	O
.	O
</s>
<s>
As	O
in	O
the	O
Microsoft	O
x64	B-Device
calling	O
convention	O
,	O
additional	O
arguments	O
are	O
passed	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
For	O
leaf-node	O
functions	O
(	O
functions	O
which	O
do	O
not	O
call	O
any	O
other	O
function(s )	O
)	O
,	O
a	O
128-byte	O
space	O
is	O
stored	O
just	O
beneath	O
the	O
stack	B-General_Concept
pointer	O
of	O
the	O
function	O
.	O
</s>
<s>
The	O
space	O
is	O
called	O
the	O
red	B-Application
zone	I-Application
.	O
</s>
<s>
Compilers	B-Language
can	O
thus	O
use	O
this	O
zone	O
to	O
save	O
local	O
variables	O
.	O
</s>
<s>
Compilers	B-Language
may	O
omit	O
some	O
instructions	O
at	O
the	O
starting	O
of	O
the	O
function	O
(	O
adjustment	O
of	O
RSP	O
,	O
RBP	O
)	O
by	O
using	O
this	O
zone	O
.	O
</s>
<s>
If	O
the	O
callee	O
is	O
a	O
variadic	B-Language
function	I-Language
,	O
then	O
the	O
number	O
of	O
floating	O
point	O
arguments	O
passed	O
to	O
the	O
function	O
in	O
vector	O
registers	O
must	O
be	O
provided	O
by	O
the	O
caller	O
in	O
the	O
AL	O
register	B-General_Concept
.	O
</s>
<s>
Unlike	O
the	O
Microsoft	O
calling	O
convention	O
,	O
a	O
shadow	O
space	O
is	O
not	O
provided	O
;	O
on	O
function	O
entry	O
,	O
the	O
return	O
address	O
is	O
adjacent	O
to	O
the	O
seventh	O
integer	O
argument	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
This	O
is	O
a	O
list	O
of	O
x86	B-Architecture
calling	I-Architecture
conventions	I-Architecture
.	O
</s>
<s>
These	O
are	O
conventions	O
primarily	O
intended	O
for	O
C/C	O
++	O
compilers	B-Language
(	O
especially	O
the	O
64-bit	O
part	O
below	O
)	O
,	O
and	O
thus	O
largely	O
special	O
cases	O
.	O
</s>
<s>
Archi­tecture	O
Name	O
Operating	B-General_Concept
system	I-General_Concept
,	O
compiler	B-Language
Parameters	O
Stack	B-General_Concept
cleanup	O
Notes	O
Registers	O
8086	B-General_Concept
cdecl	O
RTL	O
(	O
C	B-Language
)	O
Caller	O
Pascal	B-Application
LTR	O
(	O
Pascal	B-Application
)	O
Callee	O
fastcall	O
(	O
non-member	O
)	O
Microsoft	O
AX	O
,	O
DX	O
,	O
BX	O
LTR	O
(	O
Pascal	B-Application
)	O
Callee	O
Return	B-General_Concept
pointer	I-General_Concept
in	O
BX	O
.	O
</s>
<s>
fastcall	O
(	O
member	O
function	O
)	O
Microsoft	O
AX	O
,	O
DX	O
LTR	O
(	O
Pascal	B-Application
)	O
Callee	O
on	O
stack	B-General_Concept
low	O
address	O
.	O
</s>
<s>
Return	B-General_Concept
pointer	I-General_Concept
in	O
AX	O
.	O
</s>
<s>
fastcall	O
Turbo	B-Application
C	I-Application
AX	O
,	O
DX	O
,	O
BX	O
LTR	O
(	O
Pascal	B-Application
)	O
Callee	O
on	O
stack	B-General_Concept
low	O
address	O
.	O
</s>
<s>
Return	B-General_Concept
pointer	I-General_Concept
on	O
stack	B-General_Concept
high	O
address	O
.	O
</s>
<s>
Watcom	O
AX	O
,	O
DX	O
,	O
BX	O
,	O
CX	O
RTL	O
(	O
C	B-Language
)	O
Callee	O
Return	B-General_Concept
pointer	I-General_Concept
in	O
SI	O
.	O
</s>
<s>
IA-32	B-Device
cdecl	O
Unix-like	O
(	O
GCC	B-Application
)	O
RTL	O
(	O
C	B-Language
)	O
Caller	O
When	O
returning	O
struct/class	O
,	O
the	O
calling	O
code	O
allocates	O
space	O
and	O
passes	O
a	O
pointer	O
to	O
this	O
space	O
via	O
a	O
hidden	O
parameter	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
Stack	B-General_Concept
aligned	O
on	O
16-byte	O
boundary	O
due	O
to	O
a	O
bug	O
.	O
</s>
<s>
cdecl	O
Microsoft	O
RTL	O
(	O
C	B-Language
)	O
Caller	O
When	O
returning	O
struct/class	O
,	O
</s>
<s>
Non-POD	O
return	O
values	O
or	O
values	O
larger	O
than	O
64-bits	O
,	O
the	O
calling	O
code	O
will	O
allocate	O
space	O
and	O
passes	O
a	O
pointer	O
to	O
this	O
space	O
via	O
a	O
hidden	O
parameter	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
Stack	B-General_Concept
aligned	O
on	O
4-byte	O
boundary	O
.	O
</s>
<s>
stdcall	O
Microsoft	O
RTL	O
(	O
C	B-Language
)	O
Callee	O
Also	O
supported	O
by	O
GCC	B-Application
.	O
</s>
<s>
fastcall	O
Microsoft	O
ECX	O
,	O
EDX	O
RTL	O
(	O
C	B-Language
)	O
Callee	O
Return	B-General_Concept
pointer	I-General_Concept
on	O
stack	B-General_Concept
if	O
not	O
member	O
function	O
.	O
</s>
<s>
Also	O
supported	O
by	O
GCC	B-Application
.	O
</s>
<s>
register	B-General_Concept
Delphi	B-Language
and	O
Free	B-Operating_System
Pascal	I-Operating_System
EAX	O
,	O
EDX	O
,	O
ECX	O
LTR	O
(	O
Pascal	B-Application
)	O
Callee	O
thiscall	O
Windows	B-Application
(	O
Microsoft	B-Application
Visual	I-Application
C++	I-Application
)	O
ECX	O
RTL	O
(	O
C	B-Language
)	O
Callee	O
Default	O
for	O
member	O
functions	O
.	O
</s>
<s>
vectorcall	O
Windows	B-Application
(	O
Microsoft	B-Application
Visual	I-Application
C++	I-Application
)	O
ECX	O
,	O
EDX	O
,	O
[XY]MM0–5	O
RTL	O
(	O
C	B-Language
)	O
Callee	O
Extended	O
from	O
fastcall	O
.	O
</s>
<s>
Also	O
supported	O
by	O
ICC	B-Language
and	O
Clang	B-Application
.	O
</s>
<s>
Watcom	O
compiler	B-Language
EAX	O
,	O
EDX	O
,	O
EBX	O
,	O
ECX	O
RTL	O
(	O
C	B-Language
)	O
Callee	O
Return	B-General_Concept
pointer	I-General_Concept
in	O
ESI	O
.	O
</s>
<s>
x86-64	B-Device
Microsoft	O
x64	B-Device
calling	O
convention	O
Windows	B-Application
(	O
Microsoft	B-Application
Visual	I-Application
C++	I-Application
,	O
GCC	B-Application
,	O
Intel	B-Language
C++	I-Language
Compiler	I-Language
,	O
Delphi	B-Language
)	O
,	O
UEFI	B-Architecture
RCX/XMM0	O
,	O
RDX/XMM1	O
,	O
R8/XMM2	O
,	O
R9/XMM3	O
RTL	O
(	O
C	B-Language
)	O
Caller	O
Stack	B-General_Concept
aligned	O
on	O
16	O
bytes	O
.	O
</s>
<s>
32	O
bytes	O
shadow	O
space	O
on	O
stack	B-General_Concept
.	O
</s>
<s>
For	O
C++	O
classes	O
,	O
the	O
hidden	O
parameter	O
is	O
the	O
first	O
parameter	O
,	O
and	O
is	O
passed	O
in	O
RCX	O
.	O
</s>
<s>
vectorcall	O
Windows	B-Application
(	O
Microsoft	B-Application
Visual	I-Application
C++	I-Application
,	O
Clang	B-Application
,	O
ICC	B-Language
)	O
RCX/[XY]MM0,	O
RDX/[XY]MM1,	O
R8/[XY]MM2,	O
R9/[XY]MM3	O
+	O
[XY]MM4–5	O
RTL	O
(	O
C	B-Language
)	O
Caller	O
Extended	O
from	O
MS	O
x64	B-Device
.	O
</s>
<s>
System	B-Operating_System
V	I-Operating_System
AMD64	B-Device
ABI	B-Operating_System
Solaris	B-Application
,	O
Linux	B-Application
,	O
BSD	B-Operating_System
,	O
macOS	B-Application
,	O
OpenVMS	B-Operating_System
(	O
GCC	B-Application
,	O
Intel	B-Language
C++	I-Language
Compiler	I-Language
,	O
Clang	B-Application
,	O
Delphi	B-Language
)	O
RDI	O
,	O
RSI	O
,	O
RDX	O
,	O
RCX	O
,	O
R8	O
,	O
R9	O
,	O
[XYZ]MM0–7	O
RTL	O
(	O
C	B-Language
)	O
Caller	O
Stack	B-General_Concept
aligned	O
on	O
16	O
bytes	O
boundary	O
.	O
</s>
<s>
128	O
bytes	O
red	B-Application
zone	I-Application
below	O
stack	B-General_Concept
.	O
</s>
<s>
In	O
C++	O
,	O
is	O
the	O
first	O
parameter	O
.	O
</s>
