Hello everyone. 1
In this lesson, we will talk about calling conventions. Calling convention is a set of rules about procedures 2
which specifies how and where parameters are passed, which registers must be preserved, where the return value are stored 3
and who is responsible for cleaning up the stack and so on 4
. 5
Suppose you want to call a function and you know the calling convention this function uses, 6
then you can call it and be sure about its effect, as long as you follow the convention. 7
And if you want to write procedures which are used by other programs, you can choose a calling convention 8
. 9
Then other programs can call your procedures by using this convention. 10
So it's like a contract between procedures 11
In x86-64 architecture, we have Microsoft x64 calling convention and System V calling convention. 12
On windows, Microsoft x64 calling convention is used. 13
Whereas System V calling convention is followed on other platforms such as Linux and Mac OS. 14
In this lesson, we will learn Microsoft calling convention. Let’s see what the requirements are. 15
The first one is related to passing parameters. 16
The first four parameters are passed in registers. The first one in rcx, the second one in rdx and so on 17
. 18
If you pass more than four parameters, other parameters are passed on the stack. 19
The return value is stored in rax register. The third one is about registers. 20
The term caller is simply a procedure which calls other procedure. These registers are considered volatile 21
which means they can be altered by procedures we called. 22
So if you want to preserve their value, you need to save them on the stack before calling procedure 23
and then pop them from the stack afterwards. Next group of registers are considered nonvolatile 24
which means they must be preserved by procedures. The term callee is a procedure which is called from other procedure. 25
If a procedure uses these registers, it needs to save the registers and restore them before it returns. 26
When calling a procedure, rsp register must be aligned on a 16-byte boundary. 27
And 32 bytes of shadow space for register parameters need to be allocated on the stack by the caller 28
before it calls procedure. 29
Note that we must provide 32 bytes on the stack before calling, even if the called procedure 30
has fewer than 4 parameters. The last one is that it is the caller’s responsibility to clean up the stack. 31
So the caller should remove the space allocated for parameters and shadow space. 32
Here is an example showing what the stack frame looks like. The space above the dotted line 33
is maintained by calling procedure, the space below is maintained by the current procedure. 34
In this example, calling function passed 6 parameters. As you see, the first four parameters are in registers. 35
The fifth and sixth parameters are pushed on the stack. 36
Then the 32 bytes of shadow space is allocated before the procedure call. When we call a procedure, 37
the return address is pushed on the stack. Rbp value is also saved on the stack, since we want to use it as base pointer 38
or frame pointer to access parameters and local variables. Note that the frame pointer is not always used, 39
rsp register will be used to access parameters and variables in that case. The next part is saved registers. 40
If we want to use any value in volatile registers in the procedure, we need to save them in the stack 41
and restore them before the procedure returns. Then allocate storage for local variables. 42
When we want to call other procedures, simply push parameters on the stack stack if we have more than 4 four parameters to pass, 43
then allocate 32 bytes of shadow space and call the procedure. Ok this is the end of the lesson.