[英]call function with float in assembly x86 x87
我是匯編編程的新手,作為一個較大程序的一部分,我需要將浮點值傳遞給另一個C函數。 我有一個從測試程序到匯編函數的調用,該調用僅將參數壓入正確的堆棧,並調用第二個C函數。
我的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"
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
我認為ext_funct
期望double時,我只能推送32位。 但是我嘗試了fldl,fld1,fildl,fldl 12和16(%ebp),還有一些用於“樂趣”。
printf("%f", a); //3.140000
printf("%f", str); //3.140000
但是另一種方法則給出a
大的負數(100位數左右),以000000結尾。
32位約定使用cpu堆棧傳遞浮點參數。 它僅使用fpu堆棧來返回它們。 是的,您應該根據提供的原型將32位浮點數轉換為64位double。
請注意, ext_func
為void
,即它不返回任何內容,但是您將tester
聲明為返回double
……不清楚要返回的內容,我假設您想要返回原始d
(無論出於何種原因)。
因此,可能的實現方式可能是:
.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.