• Uma “calling convention” é um método especifico utilizado pelo compilador para estabelecer o acesso à uma subrotinha, especificando como os argumentos vão ser passados para a função e como os valores vão ser retornados.
  • Determina como a função é invocada, e como são criados e manipulados os stack frames.
  • É a forma como a chamada da função é traduzida para a assembly

A “fastcall” é a calling convention mais proeminente em sistemas x64, especialmente no windows.

int __fastcall sub(int a, int b){
	return  a - b;
}

Nesse caso é utilizado a keyword “__fastcall” para declarar explicitamente a convenção de chamada

Convenção de chamada x64 (m$)

https://docs.microsoft.com/pt-br/cpp/build/x64-calling-convention?view=msvc-160#parameter-passing

“Os argumentos inteiros são passados em Registers RCX, RDX, R8 e R9. Argumentos de ponto flutuante são passados em XMM0L, XMM1L, XMM2L e XMM3L. argumentos de 16 bytes são passados por referência. A passagem de parâmetro é descrita”.

func1(int a, int b, int c, int d, int e, int f);
// a in RCX, b in RDX, c in R8, d in R9, f then e pushed on stack
func3(int a, double b, int c, float d, int e, float f);
// a in RCX, b in XMM1, c in R8, d in XMM3, f then e pushed on stack

Application Binary Interface:

Essa “calling convention” não é padrão para todos os compiladores, alguns podem usar diferentes método para passar argumentos para função ou manipular stack frames. Para isso existem a “Application Binary Interface (ABI)“.

“An application binary interface, ABI, is a the interface between a program and the OS/platform. It provides a set of conventions and details such as data types, their size, alignment requirements; calling conventions, object file format, etc. The ABI is platform dependent meaning it can vary some degree from compiler to compiler. The ABI is a primary component used in how the generated assembler operates meaning that the code generation (part of the compilation process) must know the standards of the ABI. What we’re going to be considering from the ABI today is the layout of the stack frame for a function call, how arguments are passed, and how stack cleanup is performed. This is all implemented by the assembly instructions that reserve space, store certain registers to create a “frame”, copy values into that reserved space. If this is new to you, don’t sweat it. When you’re writing in a high-level language such as C or C++ you don’t really need to know about the ABI. However, when you begin to work and analyze assembly it’s important to use the correct ABI or be able to identify the ABI for the components of interest”


🌱 Back to Garden