我正在尝试实现平方根的牛顿算法。我已经包括了完整的C程序,所以你可以看到我正在尝试做什么。我正在尝试编写汇编中包含的C程序。现在我只是想正确地编写while循环。我使用命令将0.0001推到堆栈上
fld accuracy
.下面是我的注册表截图。ST0应为0.0001,但应为9.999。如何更正此值,以便与0.0001(而不是9.999)进行比较。。?
.586
.MODEL FLAT
.STACK 4096
.data
PUBLIC _newton
upper REAL4 9.0
accuracy REAL4 0.00001
lower REAL4 1.0
.code
_newton PROC
finit ;initialize fpu
fld upper
fld lower
fsub
fld accuracy
fcom
;if less than call code for the rest of the algorithm.
mov eax, 0
ret
_newton ENDP
END
我正在实现的算法的C版本:
double sqrtX (double x)
{
const double ACCURACY=0.00001;
double lower, upper, guess;
double number;
number = x;
lower = 1;
upper = number;
while ((upper-lower) > ACCURACY)
{
guess = (lower + upper) / 2;
if((guess * guess) > number)
{
upper = guess;
}
else
{
lower = guess;
}
}
return (lower + upper)/2;
}