本文介绍了x86-64组件的谐波系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图计算谐波序列.

现在我要输入要加法的数字.

Right now I'm entering the number I want the addition to go up to.

当我输入1.2之类的小数字时,该程序只会停止,不会崩溃,似乎正在执行计算.

When I enter a small number like 1.2, the program just stops, doesn't crash, it seems to be doing calculations.

但是它永远不会完成程序

BUt it never finishes the program

这是我的代码

denominator:
xor r14,r14             ;zero out r14 register
add r14, 2              ;start counter at 2
fld1                    ;load 1 into st0
fxch    st2
denomLoop:
fld1
mov [divisor], r14              ;put 1 into st0
fidiv   dword [divisor]         ;divide st0 by r14
inc r14             ;increment r14
fst qword [currentSum]      ;pop current sum value into currentSum
jmp addParts
addParts:
fld qword [currentSum]
fadd    st2     ;add result of first division to 1
fxch    st2             ;place result of addition into st2
fld qword [realNumber]          ;place real number into st0
;compare to see if greater than inputed value
fcom    st2             ;compare st0 with st2
fstsw   ax              ;needed to do floating point comparisons on FPU
sahf                    ;needed to do floating point comaprisons on FPU
jg  done                ;jump if greater than
jmp denomLoop           ;jump if less than

代码基本上是计算1/2或1/3或1/4并将其加到一个连续的总和中,然后进行比较以查看我是否已达到输入值之上的值,一旦输入该值便应退出循环

The code is basically computing the 1/2 or 1/3 or 1/4 and adding it to a running sum, then compares to see if i've reached a value above what I entered, once it has it should exit the loop

你们看到我的错误了吗?

do you guys see my error?

推荐答案

此行似乎可疑:

fst qword [currentSum]      ;pop current sum value into currentSum

与注释相反,fst将堆栈的顶部存储到内存中,而不会弹出.如果要弹出,则需要fstp.

contrary to the comment, fst stores the top of the stack into memory WITHOUT popping it. You want fstp if you want to pop it.

总的来说,程序的堆栈行为似乎很可疑-它会将各种东西压入fp堆栈,但从不弹出任何东西.经过几次迭代后,堆栈将溢出并环绕.根据您的设置,如果没有启用例外,则将获得例外或获取虚假值.

Overall, the stack behavior of your program seems suspicious -- it pushes various things onto the fp stack but never pops anything. After a couple of iterations, the stack will overflow and wrap around. Depending on your settings, you'll then either get an exception or get bogus values if you don't have exceptions enabled.

这篇关于x86-64组件的谐波系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:14