ASM使用C编译,并将返回值从ASM返回到C.

时间:2013-04-20 12:27:41

标签: assembly floating-point x86

我正在学习使用编程从头开始学习asm,但我找不到任何有关当前问题的帮助。

所以我制作了3个文件(2个在asm中,1个在c中)。

我希望我的文件在C中打印其他2个程序的输出。 C中的文件:

iclude <stdio.h>

float a = 1;
float b = 2;
float c = 3;

extern float asm1 (float a, float b, float c);
extern float asm2 (float a, float b, float c);


int main ()
{

float count1= 0;
float count2= 0;

count1= asm1 (a, b, c);
count2= asm2 (a, b, c);

printf("Output : %f and %f \n", count1, count2);

return 0;
}

ASM中的文件几乎相同,文件编号1生成x1,文件编号2生成x2。文件号1使每个变量以1(e.x. a1,x1)结束,而asm中的文件号2以2结尾(e.x. a2,x2)。

.align 32
.data

a1: .float 0
b1: .float 0
c1: .float 0
buf1: .float 0
x1: .float 0

four1: .float 4
two1: .float 2

.text
.global asm1

asm1:

xor %eax, %eax
xor %ebx, %ebx
xor %ecx, %ecx

pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx
movl 12(%ebp), %ebx
movl 16(%ebp), %eax
pushl %eax
pushl %ebx
pushl %ecx

movl %eax, c1
movl %ebx, b1
movl %ecx, a1

finit 
fld a1
fld c1

**more code**

fstp x1

movl %ebp, %esp
popl %ebp
ret

我希望这个文件有x1(x2)作为返回值,它将显示在printf的C文件中。而不是这个我得到-nan。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

要从函数返回浮点值,需要在FPU堆栈上保留返回值。

喜欢这个

 addf ...
 ; leave the value one the FPU stack, don't fstp
相关问题