home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1987 / 12 / mariella / mariella.exp < prev    next >
Text File  |  1987-12-08  |  2KB  |  65 lines

  1.  
  2. ; the 32 bit integer N is in DI:SI
  3. ; initial guess X0 is in BX
  4. ;
  5. ISQRT:     MOV  DX,DI             ;prepare for division
  6.            MOV  AX,SI             ;DX:AX / BX
  7.            DIV  BX                ;N/X0
  8.            SUB  AX,BX             ;error term
  9.            CMP  AX,1              ;check if > +1
  10.            JG   ISQ1              ;if above 1, keep on
  11.            CMP  AX,\sc0\1         ;check for \sc0\1,0,+1
  12.            JGE  done              ;if OK, get out
  13. ISQ1:      SAR  AX,1              ;(N/X0 \sc0\X0)/2
  14.            ADD  BX,AX             ;(N/X0 +X0)/2 = X1
  15.            JMP short ISQRT        ;use X1 as X0
  16.  
  17. Example 1: A code fragment for calculating square roots of 32-bit integers on t6
  18.  
  19.  
  20.  
  21. /*  ROOT.C a square root algorithm by RPM  */
  22. /*   long integers, single pass of Newton  */
  23.  
  24.    #include <stdio.h>
  25.  
  26.    main ()
  27.    {
  28.  
  29.    long int N, guess2, sqrrt;
  30.    register int infi, guess1;
  31.  
  32.         printf ( "\n square root of what number " );
  33.         scanf ("%ld",&N);
  34.      {  guess1 = infi =1;
  35.             guess2 = N;
  36.     logit:  infi <<= 1;
  37.             if ( infi < guess2 )
  38.             {  guess2 >>= 1;            /* div by 2 */
  39.                guess1 = infi;
  40.                goto logit;
  41.              }
  42.              guess1 += guess2;          /* sum */
  43.              guess1 >>= 1;              /* avg */
  44.          /* newton's method  */
  45.              infi = N / guess1;
  46.              sqrrt = infi + guess1;
  47.              sqrrt >>= 1;
  48.          }
  49.              printf ( " square root = %ld", sqrrt);
  50.      }
  51. Example 2: C code to calculate integer square roots using a single pass
  52.  
  53.  
  54.  
  55. ; the 32 bit integer N is in DI:SI
  56. ; initial guess X0 is in BX
  57. ;
  58. NEWTON:    MOV  DX,DI             ;prepare for division
  59.            MOV  AX,SI             ;DX:AX / BX
  60.            DIV  BX                ;N/X0è           ADD  BX,AX             ;(N/X0 +X0)/2 = X1
  61.            RCR  AX,1              ;(N/X0 \sc0\X0)/2
  62.  
  63. Example 3: Assembly-language code for one pass of Newton's method
  64.  
  65.