简体   繁体   English

调用 C 中的 function 进行组装

[英]Call function in C to Assembly

If we have the following function:如果我们有以下function:

int func(int n, float f, char* s, double* d);

The call to the function in Assembly will be like that:Assembly中对 function 的调用将是这样的:

movl <n>, %ecx
movl <s>, %edx
pushl <d>
pushl <f>
call func

Is this correct?这个对吗? Why it's in this order?为什么是这个顺序? It's because of the size of each type?是因为每种类型的大小吗?

For known arguments, there is not a strong compelling reason to push right-to-left or the other way around, so it's just a convention to follow.对于已知的 arguments,没有强有力的理由来推动从右到左或相反的方向,所以这只是一个要遵循的约定。

For variable arguments it's a benefit to push them in the reversed order because the callee doesn't know how many arguments were passed.对于变量 arguments 以相反的顺序推送它们是有好处的,因为被调用者不知道传递了多少 arguments。 but the first argument is in a known position, ie right after the return address.但第一个参数在一个已知的 position 中,即在返回地址之后。

So if you consider the example of printf :因此,如果您考虑printf的示例:

 printf(FormatString, a1, a2, a3, a4);

So FormatString is now in a known position and can easily find the other parameters going through the stack.因此FormatString现在位于已知的 position 中,并且可以轻松找到通过堆栈的其他参数。

If it were the other way around, then the callee would need to get some additional information in order to find the first argument, Either a pointer, or a counter or something, that tells it, where the formatstring is found.如果反过来,那么被调用者需要获取一些额外的信息才能找到第一个参数,无论是指针,还是计数器或其他东西,它告诉它在哪里找到格式字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM