summation in assembly language

Go To StackoverFlow.com

-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 
2012-04-05 00:17
by user1050632
You should step through your code in a debugger, rather than asking strangers to spot the problem - Oliver Charlesworth 2012-04-05 00: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 - user1050632 2012-04-05 00:34
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 - Oliver Charlesworth 2012-04-05 00:37
ok smart boy, what is stackoverflow? Are you telling me people don't come on here to get help in finding answers to questions? If people where able to "step through this in a debugger and find your own answer" there wouldn't be a need to use stackoverflow. Why don't you make yourself useful and not comment if it's going to be negativ - user1050632 2012-04-05 00:43
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. - Oliver Charlesworth 2012-04-05 00:47
by looking at your history of questions you've asked, sir, i realize that the only difference between you and me, is that you add a bunch of comments and then ask "So my question is, how does one do this properly? - user1050632 2012-04-05 00:57
by looking at your history of questions you've asked, sir, i realize that the only difference between you and me, is that you add a bunch of comments and then ask "So my question is, how does one do this properly? - user1050632 2012-04-05 00:57
It's hardly relevant, but you will notice that none of the questions I've asked are of the form "Here is all my code. It does not work when I run it. Why not?" - Oliver Charlesworth 2012-04-05 01:04
@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 - Gunther Piez 2012-04-05 07:58


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.

2012-04-05 00:22
by Chris Dodd
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 update - user1050632 2012-04-05 00:32
Ads