home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
drdobbs
/
1987
/
12
/
mariella
/
mariella.exp
< prev
next >
Wrap
Text File
|
1987-12-08
|
2KB
|
65 lines
; the 32 bit integer N is in DI:SI
; initial guess X0 is in BX
;
ISQRT: MOV DX,DI ;prepare for division
MOV AX,SI ;DX:AX / BX
DIV BX ;N/X0
SUB AX,BX ;error term
CMP AX,1 ;check if > +1
JG ISQ1 ;if above 1, keep on
CMP AX,\sc0\1 ;check for \sc0\1,0,+1
JGE done ;if OK, get out
ISQ1: SAR AX,1 ;(N/X0 \sc0\X0)/2
ADD BX,AX ;(N/X0 +X0)/2 = X1
JMP short ISQRT ;use X1 as X0
Example 1: A code fragment for calculating square roots of 32-bit integers on t6
/* ROOT.C a square root algorithm by RPM */
/* long integers, single pass of Newton */
#include <stdio.h>
main ()
{
long int N, guess2, sqrrt;
register int infi, guess1;
printf ( "\n square root of what number " );
scanf ("%ld",&N);
{ guess1 = infi =1;
guess2 = N;
logit: infi <<= 1;
if ( infi < guess2 )
{ guess2 >>= 1; /* div by 2 */
guess1 = infi;
goto logit;
}
guess1 += guess2; /* sum */
guess1 >>= 1; /* avg */
/* newton's method */
infi = N / guess1;
sqrrt = infi + guess1;
sqrrt >>= 1;
}
printf ( " square root = %ld", sqrrt);
}
Example 2: C code to calculate integer square roots using a single pass
; the 32 bit integer N is in DI:SI
; initial guess X0 is in BX
;
NEWTON: MOV DX,DI ;prepare for division
MOV AX,SI ;DX:AX / BX
DIV BX ;N/X0è ADD BX,AX ;(N/X0 +X0)/2 = X1
RCR AX,1 ;(N/X0 \sc0\X0)/2
Example 3: Assembly-language code for one pass of Newton's method