-2

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 
9
  • You should step through your code in a debugger, rather than asking strangers to spot the problem. Apr 5, 2012 at 0:19
  • oh thanks for your input, I thought a message board was to help collaborate and help each other? you think I haven't already tried to debug this myself? Apr 5, 2012 at 0:34
  • 1
    Stack Overflow is not a message board; it's not a place to get others to debug your code for you. You've given no indication of the results of any debugging effort on your part. If you had stepped through this in a debugger, you would be able to answer your own question. Apr 5, 2012 at 0:37
  • 1
    Seriously, you would be able to precisely answer the question "is it my exit condition?" on your own if you had stepped through the code in the debugger, as you would be able to observe the exact control flow, and the values of variables, etc. Stack Overflow is not the place to just dump all your code and ask "what's wrong?". An appropriate question would be something like "the 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." Apr 5, 2012 at 0:47
  • 1
    @user1050632 No, stackoverflow is not a remote debugging service. Expecting others to take the effort and understand your code and your poorly written question, where you self don't even make an effort to find the error is just rude. Apr 5, 2012 at 7:58

1 Answer 1

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.

1
  • fstp will pop it off and leave nothing in st0, thats why I used fst, so it just copies it into the variable and I can still use the value in st0, my comment needs to get updated Apr 5, 2012 at 0:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.