x86 汇编牛顿平方根算法寄存器为 1#IND
x86 Assembly Newtons square root algorithm registers are 1#IND
我正在尝试编写下面的 C 程序的汇编版本。它意味着取 9 的平方根。一切似乎都在工作,直到第 3 次通过我的循环,然后我的 upper 和 lower 值在推送到堆栈时变为 1#IND。我不知道为什么,因为它正确地打印在标签 whileLoop 下方,但是我第二次得到错误的值,它直接在标签 notAccurateEnough[= 之后打印27=],即使这两个标签之间的值没有变化,并且它在之前的迭代中运行良好。
下面的屏幕截图显示了它如何成功打印一次,然后在同一次迭代中第二次打印失败。
汇编代码:
.586
.MODEL FLAT
.STACK 4096
n=550
.data
PUBLIC _sqrtX
upper REAL4 9.0
accuracy REAL4 0.00001
lower REAL4 1.0
guess REAL4 1.0
multValue REAL4 1.0
num REAL4 9.0
two DWORD 2
.code
_sqrtX PROC
finit ;initialize fpu
whileLoop:
fld upper ;push upper onto stack
fld lower ;push lower onto stack
fsub ;subtract upper-lower
fld accuracy ;push accuracy onto stack
fcom ;compare accuracy with upper-lower
fstsw ax ;copy condition code bits to AX
sahf ;shift condition code bits to flags
jna notAccurateEnough ;jump if accuracy is greater
jmp endnewt ;end algorith, is less than accuracy
notAccurateEnough:
;**************************
;************ Logic to implement: guess = (lower + upper) / 2;
;**************************
fld upper ;push upper onto stack
fld lower ;push lower onto stack
fadd ;(lower + upper)
fidiv two ;divide by 2
fstp guess ;set guess to first stack element. guess = (lower + upper) / 2
;**************************
;**************************
;************ Logic to implement: if((guess * guess) > number)
;**************************
fld guess ;push guess onto stack as first element
fld guess ;push guess onto stack again, it is now first and second element
fmul ;multiply first and second element
fcom num ;compare 9 to product of guess*guess:
fstsw ax ;copy condition code bits to AX
sahf ;shift condition code bits to flags
jna SetLower ;if guess is less than num, call set lower and set lower = guess;
;*** else, guess > num
fld guess ;push guess onto the stack
fstp upper ;set upper equal to top of stack: upper = guess
jmp whileLoop
;**************************
setLower:
fld guess ;push guess onto stack
fstp lower ;store guess in lower
jmp whileLoop
endnewt:
mov eax, 0
ret
_sqrtX 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;
}
发生堆栈溢出。使用 finit
每次迭代后清除堆栈,现在它可以工作了。
我正在尝试编写下面的 C 程序的汇编版本。它意味着取 9 的平方根。一切似乎都在工作,直到第 3 次通过我的循环,然后我的 upper 和 lower 值在推送到堆栈时变为 1#IND。我不知道为什么,因为它正确地打印在标签 whileLoop 下方,但是我第二次得到错误的值,它直接在标签 notAccurateEnough[= 之后打印27=],即使这两个标签之间的值没有变化,并且它在之前的迭代中运行良好。
下面的屏幕截图显示了它如何成功打印一次,然后在同一次迭代中第二次打印失败。
汇编代码:
.586
.MODEL FLAT
.STACK 4096
n=550
.data
PUBLIC _sqrtX
upper REAL4 9.0
accuracy REAL4 0.00001
lower REAL4 1.0
guess REAL4 1.0
multValue REAL4 1.0
num REAL4 9.0
two DWORD 2
.code
_sqrtX PROC
finit ;initialize fpu
whileLoop:
fld upper ;push upper onto stack
fld lower ;push lower onto stack
fsub ;subtract upper-lower
fld accuracy ;push accuracy onto stack
fcom ;compare accuracy with upper-lower
fstsw ax ;copy condition code bits to AX
sahf ;shift condition code bits to flags
jna notAccurateEnough ;jump if accuracy is greater
jmp endnewt ;end algorith, is less than accuracy
notAccurateEnough:
;**************************
;************ Logic to implement: guess = (lower + upper) / 2;
;**************************
fld upper ;push upper onto stack
fld lower ;push lower onto stack
fadd ;(lower + upper)
fidiv two ;divide by 2
fstp guess ;set guess to first stack element. guess = (lower + upper) / 2
;**************************
;**************************
;************ Logic to implement: if((guess * guess) > number)
;**************************
fld guess ;push guess onto stack as first element
fld guess ;push guess onto stack again, it is now first and second element
fmul ;multiply first and second element
fcom num ;compare 9 to product of guess*guess:
fstsw ax ;copy condition code bits to AX
sahf ;shift condition code bits to flags
jna SetLower ;if guess is less than num, call set lower and set lower = guess;
;*** else, guess > num
fld guess ;push guess onto the stack
fstp upper ;set upper equal to top of stack: upper = guess
jmp whileLoop
;**************************
setLower:
fld guess ;push guess onto stack
fstp lower ;store guess in lower
jmp whileLoop
endnewt:
mov eax, 0
ret
_sqrtX 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;
}
发生堆栈溢出。使用 finit
每次迭代后清除堆栈,现在它可以工作了。