home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / DIVERSEN / DOS32V3B / LIB / SQRT.ASM < prev    next >
Assembly Source File  |  1995-01-21  |  2KB  |  63 lines

  1. ;▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  2. ;▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  3. ;   Writen by Adam Seychell
  4.  
  5.  
  6. .386
  7.  
  8. .model flat
  9.  
  10.             .CODE
  11.  
  12.  
  13. comment $
  14. ╔═════════════════════════════════════════════════════════════════╗
  15. ║ returns EAX with the SQUARE ROOT of EAX.                        ║
  16. ║                                                                 ║
  17. ║ Uses newtons method:    Xn+1 = Xn -  f(Xn) / f'(Xn)             ║
  18. ║    f'(x) is the derivitive of f(x)                              ║
  19. ║                                                                 ║
  20. ║ For square routes:  f(x) = x*x - ( Root value  i.e EAX)         ║
  21. ║  so     f'(x) = 2x                                              ║
  22. ║                                                                 ║
  23. ║ For cube routes:  f(x) = x*x*x - ( Root value )                 ║
  24. ║  so     f'(x) = 3x                                              ║
  25. ╚═════════════════════════════════════════════════════════════════╝$
  26. Public  SQRT
  27.  
  28. SQRT    Proc PASCAL USES EDI ECX EBP EDX EBX
  29.  
  30.         mov edi,eax
  31. ;----- Get Estimated Root Value ( ebp = rough value of the sqrt of EAX ) ---
  32.                 bsr ecx,edi     ; Get bit number of highest NZ bit
  33.                 shr cl,1        ; Half this highest bit number
  34.         mov ebp,edi     ; Rotate Root Value by
  35.                 shr ebp,cl      ;  the bit number to get a estimate root.
  36.  
  37.  
  38. ;----- Main Loop that makes EBP converge to the square root of EDI ------
  39.  calc_sqrt_Loop:
  40.         push EBP        ; save x  so can check if x has converged
  41.         mov  EBX,EBP
  42.         shl  EBX,1        ; ebx = f'(x) = 2x
  43.  
  44.         mov  EAX,EBP
  45.         mul EBP
  46.         sub  EAX,EDI        ; eax = f(x) = X*X - Root_value
  47.         sbb  EDX,0        ; use 64bit subtract
  48.  
  49.         idiv  EBX               ; eax = f(Xn) / f'(Xn)
  50.  
  51.     sub  EBP,EAX        ; ebp =   Xn+1   =   Xn - f(Xn) / f'(Xn)
  52.         pop  EAX        ; look at old value
  53.         cmp  EBP,EAX        ; loop only if new value is different
  54.       jne calc_sqrt_Loop
  55. ;-------------------------- End of Loop --------------------------------
  56.  
  57.         mov eax,ebp     ; EBP now contians the square root of EDI
  58.     ret
  59. ;────────────────────────────────────────────────────────────────────────────
  60. SQRT    Endp
  61.  
  62.         End
  63.