Possible Duplicate:
harmonic series with x86-64 assembly
trying to do a version of a harmonic series in assembly
Current code takes 1 + 1/2 + 1/3 + 1/4 + 1/n until the value that is being summed up is greater than the value that was entered. (float value)
currently code exits out of the loop after the first loop through and it prints out .33333
is it my exit condition?
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
ABC
instruction is not doing what I expect, even though the XYZ flag has been set/cleared by the previous instruction. I've constructed a 5-line test case to demonstrate it. - Oliver Charlesworth 2012-04-05 00:47
This line seems suspicious:
fst qword [currentSum] ;pop current sum value into currentSum
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.