harmonic series with x86-64 assembly

Go To StackoverFlow.com

0

Trying to compute a harmonic series.

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

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

here is my code

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 

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?

2012-04-03 22:27
by user1050632
The final jmp is (logically) jump if less than OR equal. There's no good reason to use 387 code on x86-64. SSE is more orthogonal than the clumsy, stack-based ISA - and makes things like compares, etc., much easier - Brett Hale 2012-04-04 05:06
possible duplicate of summation in assembly languageOliver Charlesworth 2012-04-05 01:08


1

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.

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.

2012-10-09 19:55
by Chris Dodd
Ads