C语言 C 函数的返回值给 ASM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6171172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return value of a C function to ASM
提问by Juan Pablo
I'm trying to call a function from within ASM. I know how to call it, but i'm having trouble finding how to get the return value of this function. An example follows:
我正在尝试从 ASM 内部调用一个函数。我知道如何调用它,但我无法找到如何获取此函数的返回值。一个例子如下:
C code:
代码:
int dummy() {
return 5;
}
(N)ASM code:
(N)ASM 代码:
dummyFunction:
call dummy
;grab return into eax
inc eax ; eax should be 6 now
ret
Any ideas?
有任何想法吗?
回答by R.. GitHub STOP HELPING ICE
The return value is in eax. If you've called a C function from asm, you can read the return value from eax. If you're trying to return from an asm function to C, store the intended return value in eax.
返回值在eax. 如果你已经从 asm 调用了一个 C 函数,你可以从eax. 如果您尝试从 asm 函数返回到 C,请将预期的返回值存储在eax.
Things get a little bit more complicated for returning floating point values, long longvalues, or structures, so ask if you need that and someone (maybe me) will help you.
返回浮点值、long long值或结构的事情变得有点复杂,所以问问你是否需要它,有人(也许是我)会帮助你。
回答by legends2k
Although the answers are sufficient to answer the OP's question, here's an extract covering most cases from DJPP's manpagefor completeness:
尽管答案足以回答 OP 的问题,但为了完整性,以下是DJPP 联机帮助页中涵盖大多数情况的摘录:
Return Value
- Integers (of any size up to 32 bits) and pointers are returned in the
%eaxregister.- Floating point values are returned in the 387 top-of-stack register,
st(0).- Return values of type
long long intare returned in%edx:%eax(the most significant word in%edxand the least significant in%eax).- Returning a structure is complicated and rarely useful; try to avoid it. (Note that this is different from returning a pointer to a structure.)
If your function returns
void(e.g. no value), the contents of these registers are not used.
返回值
- 整数(最多 32 位的任何大小)和指针在
%eax寄存器中返回。- 浮点值在 387 栈顶寄存器中返回
st(0)。- 类型的返回值
long long int在返回%edx:%eax(在最显著字%edx和最低显著的%eax)。- 返回一个结构很复杂,很少有用;尽量避免它。(请注意,这与返回指向结构的指针不同。)
如果您的函数返回
void(例如没有值),则不会使用这些寄存器的内容。
回答by Christian Rau
It depends on the platform and the calling convention, but usually, the return value should already be returned in eaxif it's a primitive type or pointer and in the floating point register st(0)if it's a floating point type, I think.
这取决于平台和调用约定,但通常,eax如果它是原始类型或指针,返回值应该已经返回,如果它st(0)是浮点类型,则在浮点寄存器中,我认为。

