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:

  1. Initial values: a=1 , b=1 , c=-1 , d=2 .
  2. Calculate bd and save somewhere to not overwrite b and d .
  3. Calculate ad , save so as not to touch d .
  4. Calculate bc and save so as not to touch c .
  5. Calculate ad+bc .
  6. a=ad+bc, b=bd
  7. Change sign c .
  8. Increment d .
  9. Repeat 2-8 n-1 times.
  10. 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.

  • And why, sorry, restrictions on the use of registers? - VladD
  • @VladD So when using ebp, esp, esi and edi get errors. Or do you suggest making a permanent register-memory exchange? - Byulent
  • Well, if mistakes are made, why not fix them? - VladD
  • I do not see anything really bad in register-memory exchanges, if that. In the end, it’s not for nothing that processors have a multi-level cache. But I think you can do without the use of RAM in your task, yes. - VladD
  • @VladD So errors are obtained because these registers simply cannot be used. - Byulent

0