Understanding of the function calling convention

Learn something sure to ask why.

Why do we need a function calling convention?

Imagine such a problem, when you call a function written by someone else, you need to pay attention to what the problem is.

Xiaoming students prepared a function foo (int a, int b, int c);

When you call foo (1,2,3), how to pass parameters, what is the order parameter passing, (of course, will involve a number of other issues)

You have three different colors of bricks (red, green, blue)

  1. You can (how to pass parameters) through his hands were red, green, blue and passed to the other side
  2. You can (how to pass parameters) through his hands were blue, green, red passed to the other side
  3. You can (how to pass parameters) by car, respectively red, green, blue and passed to the other side

No matter by what means, how to deliver the order. The other side of the argument are received, but we both agreed to the rules of good agreement (pass additional order specifications, etc.), so the recipient would not be wrong to understand what I mean

In any case, said reading here, you should know, there is a function call conventions, not messing around (although this problem when you write code, you do not directly deal with, but after all, this problem exists)

 

 Made a table, there are basically these incomplete, Wikipedia calling convention of classification

When I made the table, when the test case, I found the same calling conventions are handled differently on different platforms, so the notes simply can not do, the only way to do that , to understand what function to call the case, then the actual to sum up the situation, focus, focus


Explains so much, you may not understand, I take examples to illustrate

fastcall

 

  float  __fastcall foo(int a,int b,int c,int d) {
      return 1.f;
}

 When circumstances x86

 

Using two registers and memory (stack area) transmission parameters

 When circumstances x64

 

使用4个寄存器传递参数 ,这里还有大于4个参数时的处理方式。

上面就是fastcall在不同的平台有不同的处理,这个问题也好理解,倒退5000年生火是一个问题,现在这个问题只需要你买一个打火机。

对应的问题就是,以前寄存器(一种容器,能存储少量的数据)不多,参数基本都采用内存(栈区)来传递,现在直接使用寄存器。注:(寄存器造价比内存高,意思就是寄存器读取数据快,内存相对寄存器慢)

 

所以不管你如何记忆,如何整理。我的理解来说一点意义都没,我只需要知道调用函数时有什么问题,不懂该约定时,那么直接看汇编。脑补画面(哦,原来VS X86,cdecl是 右到左传递参数,由调用者释放栈。这样做的好处是支持多参数)

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/binaryAnt/p/11124115.html