The FPU stack isn't endless. In your loop you push (fld) more values than you pop (fstp). So the stack becomes full and every new push produces a NaN.
Taylor series expotential function in assembly
-
18-07-2023 - |
سؤال
I am trying to implement expotential function algorithm in assembly according to this equation: http://upload.wikimedia.org/math/4/5/9/4597c1e758b3aeb83adcb03d3f75d00e.png
My code in assembly:
.data
x:
.space 8
result:
.space 8
counter:
.space 8
factorial:
.space 8
n:
.space 8
xn:
.space 8
.text
.global expot
expot:
pushl %ebp
movl %esp, %ebp
flds 8(%ebp) # wczytaj x
fstps x
flds 12(%ebp) # wczytaj precyzje
fstps n
fldz
fstps result
fld1
fstps factorial
fld1
fstps counter
fld1
fstps xn
loop:
flds factorial
flds xn
fdiv %st(1)
fldl result
fadd %st(1), %st(0)
fstps result
flds counter
fcom n
je end
flds xn
fmul x
fstps xn
fld1
flds counter
fadd %st(1)
fstps counter
flds counter
flds factorial
fmul %st(1)
fstps factorial
jmp loop
end:
leave
ret
My code in C:
#include <stdio.h>
extern float expot (float x, float n);
int main(void)
{
float x = expot (1, 4);
printf ("%f\n", x);
return 0;
}
My problem is that this function doesn't return anything. When I am debugging it with gdb I see that in second iteration, when it should calculate x^n, the result is -nan and then when it should calculate 2! it returns -nan again. I completly don't know what is wrong with this code.
Thanks for all your responses.
المحلول
نصائح أخرى
Your function is not returning any value.
The x86 cdecl
calling convention indicates:
Integer values and memory addresses are returned in the EAX register, floating point values in the ST0 x87 register.