调用函数与漂浮在装配86的x87 [英] call function with float in assembly x86 x87
问题描述
我是新来的汇编编程,作为一个更大的计划我需要传递浮点值到另一个C函数的一部分。我从我的测试程序,以我的汇编函数的调用,只推右栈上的参数,并调用第二个C函数。
I'm new to assembly programming and, as a part of a bigger program I have need to pass floating point values to another C-function. I have a call from my test program to my assembly function, that only pushes the parameters on the right stack, and calls a second C function.
我的C测试功能:
extern void ext_func(char *result, double d); // C function
extern double tester(char *str, float d);
double a = tester(str, 3.14)
printf("%s\n", str); // Resulting in '0.000000'
// doing some fancy stuff with the float value and puts in result
ext_func(str, 3.14); // gives str = "3.140000"
86,GCC -m32:
x86, gcc -m32:
.globl tester
tester:
pushl %ebp # Standard
movl %esp, %ebp #
flds 12(%ebp) # Push second parameter on stack
pushl 8(%ebp)
call ext_func
addl $4, %esp
leave
ret
我觉得孤单一个问题,我只推32位时, ext_funct
预期的两倍。但我试图fldl,FLD1,fildl,fldl 12和16(%EBP),和其他一些为好玩。
I think theres a problem with me only pushing 32 bit when ext_funct
expecting double. But I tried to the fldl, fld1, fildl, fldl 12 and 16(%ebp), and some of the other for "fun".
- 我的第一个问题是,ext_func缺少浮动堆栈(ST)上的一些数据,因此不能够使浮点值?(我知道你没有被调用函数,但事情的功能是什么犯规? )
- 二,该编译器永诺去到F-栈获得浮点值,如果它希望它们,或者是否有可能从memorystack读?
-
三,是否有其他seomething我在这里丢失?如果我
- My first question is, are ext_func missing some data on the float stack(ST), and is therefore not able to make the float value?(I understand you dont have the callee function, but doesnt matter what the function does?)
- Second, does the compiler allways go to to the f-stack to get float values if it expects them, or is it possible to read them from the memorystack?
Third, is there seomething else I'm missing here? If I
的printf(%F,一); //3.140000结果
的printf(%F,STR); //3.140000
printf("%f", a); //3.140000
printf("%f", str); //3.140000
但其他的方式 A
给出了000000大结束数是负面的(100位左右)。
but the other way a
gives big negativ number(100 digits or so) ended by 000000.
推荐答案
32位的约定使用CPU堆栈传递浮点参数。它仅使用FPU堆栈返回他们。是的,你应该将32位浮点转换为64位双,按照原型你提供。
The 32 bit convention uses the cpu stack to pass floating point arguments. It only uses the fpu stack for returning them. Yes, you should convert your 32 bit float to a 64 bit double, as per the prototypes you provided.
注意 ext_func
是无效
,即它不返回任何东西,但你申报测试
为返回双击
...现在还不清楚你想回来的,我会假设你希望原始 ð
回(无论何种原因)。
Note that ext_func
is void
, that is it doesn't return anything, but you declared tester
as returning double
... it's unclear what you want returned, I will assume you want the original d
back (for whatever reason).
这样,一个可能的实现可以是:
As such, a possible implementation could be:
.globl tester
tester:
subl $12, %esp # allocate space for outgoing arguments
movl 16(%esp), %eax # fetch our first argument (str)
movl %eax, (%esp) # store as first outgoing argument
flds 20(%esp) # Fetch our second argument as float
fstpl 4(%esp) # store it as second outgoing argument as double
call ext_func
flds 20(%esp) # load d as return value
addl $12, %esp # cleanup stack
ret
这篇关于调用函数与漂浮在装配86的x87的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!