The task: to calculate the sum p/q = 1-1/2+1/3-…+(-1)^(n+1)/n for a given number n. The fraction p/q must be irreducible ( p and q are natural). The lecturer recommended not using real numbers and doing the task with the help of integer operations.
Question: how to do it? I thought something like this: we should have 4 numbers a , b , c and d to represent fractions a/b and с/d respectively. Then at each step we have to calculate the sum (ad+bc)/bd .
I introduced the algorithm to something like this:
- Initial values:
a=1,b=1,c=-1,d=2. - Calculate
bdand save somewhere to not overwritebandd. - Calculate
ad, save so as not to touchd. - Calculate
bcand save so as not to touchc. - Calculate
ad+bc. a=ad+bc, b=bd- Change sign
c. - Increment
d. - Repeat 2-8
n-1times. - Calculate the gcd of the resulting p and q and divide them by this number.
Attempts to do this with all the registers that are in the asm, led here to this .
Now the question is: how to do it using only eax, ebx, ecx, edx ? Moreover, even ecx and edx , in fact, disappear, ecx stores the loop counter, and edx is the high word of the multiplication result, which is overwritten during multiplication and must be equal to 0 for further division operations. That is, there are only 2 registers.
ebp, esp, esiandediget errors. Or do you suggest making a permanent register-memory exchange? - Byulent