Question

I had errors with compiling earlier but after working on them, got no errors/warnings. Altho the result given is NaN.

Here is code:

C:

iclude <stdio.h>

extern float wynik1 (int a, int b, int c);
extern float wynik2 (int a, int b, int c);

int a = 2;
int b = 2;
int c = 2;

int main ()
{

float licz1 = 0;
float licz2 = 0;

licz1 = wynik1(a, b, c);
licz2 = wynik2(a, b, c);

printf("Roots : %f oraz %f \n", licz1, licz2);

return 0;
}

ASM1:

SYSEXIT = 1
SYSREAD = 3
SYSWRITE = 4
STDOUT = 1
EXIT_SUCCESS = 0
STDIN = 0

.align 32
.data

a: .int 2
b: .int 2
c: .int 2
buf: .float 0
x1: .float 0

cztery: .float 4
dwa: .float 2

.text
.global wynik1

wynik1:

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, a
movl %ebx, b
movl %ecx, c

finit 
fld a
fld c

deltaa1:
fmulp
fld cztery
fmulp
fld b
fld b
fmulp
fsubp

pierwiastek1:
fsqrt
fld b
fchs
fsubp
fstp buf
fld a
fld dwa
fmulp
fld buf
fdivp
fstp x1

movl x1, %eax

movl %ebp, %esp
popl %ebp
ret

ASM2:

SYSEXIT = 1
SYSREAD = 3
SYSWRITE = 4
STDOUT = 1
EXIT_SUCCESS = 0
STDIN = 0

.align 32
.data

a: .int 2
b: .int 2
c: .int 2
buf: .float 0
x2: .float 0
cztery: .float 4
dwa: .float 2

.text
.global wynik2

wynik2:

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, a
movl %ebx, b
movl %ecx, c

finit 
fld a
fld c

deltaa2:
fmulp
fld cztery
fmulp
fld b
fld b
fmulp
fsubp

pierwiastek2:
fsqrt
fld b
fchs
faddp
fstp buf
fld a
fld dwa
fmulp
fld buf
fdivp
fstp x2


movl %ebp, %esp
popl %ebp
ret

So yeah. The return in C code is "- nan and - -nan". I have no idea hod to overcome this after some hours of checking registers and changing code.

Code in C = intput and output.

ASM1 is couting root 1 and ASM2 is counting root 2.

Any ideas?

Was it helpful?

Solution

You are doing fld a, while you should be doing fild a to load an integer to the floating point stack (converting it to float); as those variables are integers, not floats.

E.g. integer 2 loaded as float is a denormal with magnitude of about 2^-140. This multiplied with another small number makes zero.

Then the last fdivp divides 0.0 by 0.0, which is by definition a NaN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top