200 lines
2.8 KiB
C
200 lines
2.8 KiB
C
#include <stdio.h>
|
|
|
|
|
|
/**
|
|
* This file aims to help understanding calling conventions made by gcc
|
|
* Arch: x86_64
|
|
*
|
|
* The conclusions here are only valid for gcc compilers
|
|
*/
|
|
|
|
/***
|
|
* Calling convention intel x86
|
|
* movl eax, 0
|
|
*
|
|
* Calling convention arm arch64
|
|
* empty preamble
|
|
*/
|
|
int f00()
|
|
{
|
|
return 7;
|
|
}
|
|
|
|
void fl(char* str){
|
|
printf("%s",str);
|
|
}
|
|
|
|
/***
|
|
* Calling convention intel x86
|
|
* movl eax, 0
|
|
*
|
|
* Calling convention arm arch64
|
|
* empty preamble
|
|
*/
|
|
char f01()
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Calling convention intel x86
|
|
* movl edi, a -> f1(edi)
|
|
*
|
|
* Calling convention arm arch64
|
|
*
|
|
* subtract 16 bytes in the stack for arguments
|
|
* fetch from sp -12 (12=-16+4)
|
|
*
|
|
*/
|
|
|
|
int f1(int a)
|
|
{
|
|
return a + 1;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Calling convention intel x86
|
|
* movl edi, a -> f1(edi)
|
|
*
|
|
* Calling convention arm arch64
|
|
*
|
|
* subtract 16 bytes in the stack for arguments
|
|
* fetch from sp -8 (12=-16+8)
|
|
*
|
|
*/
|
|
long f1l(long a)
|
|
{
|
|
return a+1;
|
|
}
|
|
|
|
/**
|
|
* Calling convention
|
|
* movl esi, b
|
|
* movl edi, a
|
|
* f(edi,esi)
|
|
*/
|
|
int f2(int a, int b)
|
|
{
|
|
return a + b;
|
|
}
|
|
|
|
/**
|
|
* Calling convention
|
|
* movl edx, c
|
|
* movl esi, b
|
|
* movl edi, a
|
|
*/
|
|
int f3(int a, int b, int c)
|
|
{
|
|
return a + b - c;
|
|
}
|
|
|
|
/**
|
|
* Calling convention
|
|
*
|
|
* movl ecx, d
|
|
* movl edx, c
|
|
* movl esi, b
|
|
* movl edi, a
|
|
*/
|
|
int f4(int a, int b, int c, int d)
|
|
{
|
|
return (a + b) - (c + d);
|
|
}
|
|
|
|
/**
|
|
* Calling convention
|
|
*
|
|
* movl r8d, e
|
|
* movl ecx, d
|
|
* movl edx, c
|
|
* movl esi, b
|
|
* movl edi, a
|
|
*/
|
|
int f5(int a, int b, int c, int d, int e)
|
|
{
|
|
return (a + b) - (c + d) + e;
|
|
}
|
|
|
|
/**
|
|
* Calling convention
|
|
*
|
|
* movl r9d, f
|
|
* movl r8d, e
|
|
* movl ecx, d
|
|
* movl edx, c
|
|
* movl esi, b
|
|
* movl edi, a
|
|
*/
|
|
int f6(int a, int b, int c, int d, int e, int f)
|
|
{
|
|
int sum1 = a + b + c;
|
|
int sum2 = d + e + f;
|
|
int mult = sum1 * sum2;
|
|
return mult;
|
|
}
|
|
|
|
/**
|
|
* Calling convention
|
|
*
|
|
* push h
|
|
* movl r9d, f
|
|
* movl r8d, e
|
|
* movl ecx, d
|
|
* movl edx, c
|
|
* movl esi, b
|
|
* movl edi, a
|
|
*
|
|
* Note: After the call we will have
|
|
* addq rsp 8 --> Revert the push into stack operation of size long
|
|
*/
|
|
int f7(int a, int b, int c, int d, int e, int f, int h)
|
|
{
|
|
return a + b - c + d - e + f - h;
|
|
}
|
|
|
|
/**
|
|
* Calling convention
|
|
*
|
|
* push j
|
|
* push h
|
|
* movl r9d, f
|
|
* movl r8d, e
|
|
* movl ecx, d
|
|
* movl edx, c
|
|
* movl esi, b
|
|
* movl edi, a
|
|
*
|
|
* Note: After the call we have
|
|
* addq rsp 16 --> Revert the two push into stack operations (by size long)
|
|
*/
|
|
int f8(int a, int b, int c, int d, int e, int f, int h, int j)
|
|
{
|
|
return a + b + c - d - e - f - h + j;
|
|
}
|
|
|
|
long fl0(long int a)
|
|
{
|
|
return a * a;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
fl("Hello Master");
|
|
f00();
|
|
f01();
|
|
f1(1);
|
|
f1l(1);
|
|
f2(1, 2);
|
|
f3(1, 2, 3);
|
|
f4(1, 2, 3, 4);
|
|
f5(1, 2, 3, 4, 5);
|
|
f6(1, 2, 3, 4, 5, 6);
|
|
f7(1, 2, 3, 4, 5, 6, 7);
|
|
f8(1, 2, 3, 4, 5, 6, 7, 8);
|
|
|
|
fl0(10L);
|
|
return 0;
|
|
}
|