0

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?

4

1 回答 1

2

您正在做fld a,而您应该做fild a的是将整数加载到浮点堆栈(将其转换为浮点数);因为这些变量是整数,而不是浮点数。

例如,作为浮点数加载的整数 2 是一个大小约为 2^-140 的非正规。这乘以另一个小数为零。

然后最后一个fdivp将 0.0 除以 0.0,即定义为 NaN。

于 2013-04-20T06:09:58.083 回答