汇编语言的总结

时间:2012-04-05 00:17:45

标签: loops x86-64 nasm

  

可能重复:
  harmonic series with x86-64 assembly

尝试在程序集中执行谐波系列的版本

当前代码取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 

1 个答案:

答案 0 :(得分:1)

这条线似乎很可疑:

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

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