-2

可能重复:
带 x86-64 组件的谐波系列

试图在汇编中做一个谐波系列的版本

当前代码采用 1 + 1/2 + 1/3 + 1/4 + 1/n 直到求和的值大于输入的值。(浮点值)

当前代码在第一次循环后退出循环并打印出 .33333

这是我的退出条件吗?

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
addParts:
fxch    st2             ;put the current value that is in st2 into st0
fadd    qword [currentSum]      ;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
jae done                ;jump if greater than
jmp denomLoop           ;jump if less than 
4

1 回答 1

1

这条线似乎很可疑:

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

与注释相反,fst将堆栈顶部存储到内存中而不弹出它。如果你想fstp弹出它,你想要。

于 2012-04-05T00:22:02.713 回答