Advertisement
Il_Voza

3.5 Compute square root

May 12th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;A way to calculate the approximate square root of a int number is to count the amount of odd numbers that can be subtracted from starting number.
  2. ;The proposed solution in C language is:
  3. ;   main()
  4. ;    {
  5. ;        int num, sqr=0, disp=1;
  6. ;        ...
  7. ;        num--;
  8. ;        while (num >= 0)
  9. ;        { sqr++;
  10. ;          disp += 2;
  11. ;          num -= disp; }
  12. ;        ...
  13. ;    }
  14.  
  15. ;Realize a program that computes the approximate square root of a positive int (16 bit)
  16.  
  17. .MODEL small
  18. .STACK
  19. .DATA
  20. NUM DW 121 ;Number of which I would compute square root
  21. disp DW 1
  22. SQRT DW 0
  23. .CODE
  24. .STARTUP
  25. MOV CX,2
  26. DEC NUM
  27. ciclo:
  28.       INC SQRT    ;In the end, the square root is stored in SQRT
  29.       ADD disp,CX
  30.       MOV AX,disp
  31.       SUB num,AX
  32.       JNS ciclo  ;I use JNS, that is: if the number is unsigned, then continue to cycle
  33.  
  34. .EXIT
  35. END
  36.  
  37. ;I simply translated in Assembly the C code given above
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement