sadasfsdafas fgasgasd
sadasfsdafas fgasgasd

Reputation: 41

ASM compiling with C, and the return value into C from ASM

I am learning using programming from the ground up to learn asm, but I cannot find any help about my current problem.

So I made 3 files (2 in asm, 1 in c).

I want my file in C to print output of 2 other programs. File in 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;
}

Files in ASM are the almost the same, file number 1 produce x1, and file number 2 produce x2. File number 1 got every variable ended with 1 (e.x. a1, x1), and file number 2 in asm ends with 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

And I would like this file to have x1(x2) as a return value which will be shown in C file in printf. Instead of this I get -nan. How can I do this?

Upvotes: 1

Views: 240

Answers (1)

Quonux
Quonux

Reputation: 2991

To return a float Value from the function you need to leave the to returning value on the FPU stack.

Like this

 addf ...
 ; leave the value one the FPU stack, don't fstp

Upvotes: 1

Related Questions