Newton's square root
Find the square root of the number in the stdin box without the fsqrt instruction, using the rule Isaac Newton's method gives for square roots: if guess is a guess at the root of n, then
next = (guess + n / guess) / 2
is a better one. Start at or above the root (n itself, or 1.0 when n is below 1) and every step lands closer, always from above. In exact arithmetic that goes on forever; in floating point the guesses soon stop getting smaller, because a double holds only so many digits. So repeat the rule, compare each next with guess using fcmp, and stop as soon as next is not smaller. guess is then the answer. Do not count steps: a big n needs far more of them than a small one.
Print the root with the %.6f format in .data, which reads a double from d0. Two inputs need care before the loop:
- a negative number has no real square root: print
no real square rootand return 1 - the root of 0 is 0; left to the loop, the guess shrinks to zero and
0 / 0is not a number, so the loop never ends
What is checked
- the root printed for the number in the stdin box, and for more numbers you cannot see
- the exit status (0, or 1 for a negative number)
- the loop is yours:
fdivandfcmppresent, nofsqrt
specification
fdivfcmpfsqrtWe run your program on the input above and on the hidden ones, and compare what it does. Nothing is matched against a stored solution.