| CODE |
| #include "stdio.h" #include "math.h" long isqrt( long nr ); unsigned char sqrt_tab[256]; void SetupSqrtTable(){ long i; for(i=0;i<256;i++) sqrt_tab[i] = 256.0 * sqrt( i/256.0 ); } void main(){ long nr,i; SetupSqrtTable(); printf("\nNegative number to quit :"); while(1){ printf("\nNumber => "); scanf("%d",&nr); if(nr<0) break; printf("\nSqrt is %d", isqrt(nr)); } } |
| CODE |
| .386 _TEXT SEGMENT BYTE PUBLIC USE32 'CODE' ASSUME cs:_TEXT extern _sqrt_tab : BYTE ; ; In : ; eax - integer value to root ; ; Out: ; eax - root ( only bits 15..0 may be ones. ) ; ; No registers modified, no stack usage ; public isqrt_ isqrt_ proc near cmp eax,10000h jb c_15_0 cmp eax,1000000h jb c_23_16 ; bit 31..24 cmp eax,10000000h jb c_27_24 cmp eax,40000000h jb c_29_28 shr eax,24 mov al, [_sqrt_tab+eax] shl eax,8 ret c_29_28: shr eax,22 mov al, [_sqrt_tab+eax] shl eax,7 ret c_27_24: cmp eax,4000000h jb c_25_24 shr eax,20 mov al, [_sqrt_tab+eax] shl eax,6 ret c_25_24: shr eax,18 mov al, [_sqrt_tab+eax] shl eax,5 ret ; bit 23..16 c_23_16: cmp eax,100000h jb c_19_16 cmp eax,400000h jb c_21_20 shr eax,16 mov al, [_sqrt_tab+eax] shl eax,4 ret c_21_20: shr eax,14 mov al, [_sqrt_tab+eax] shl eax,3 ret c_19_16: cmp eax,40000h jb c_17_16 shr eax,12 mov al, [_sqrt_tab+eax] shl eax,2 ret c_17_16: shr eax,10 mov al, [_sqrt_tab+eax] shl eax,1 ret c_15_0: cmp eax,100h jb c_7_0 ; bit 15..8 cmp eax,1000h jb c_11_8 cmp eax,4000h jb c_13_12 shr eax,8 mov al, [_sqrt_tab+eax] ret c_13_12: shr eax,6 mov al, [_sqrt_tab+eax] shr eax,1 ret c_11_8: cmp eax,400h jb c_9_8 shr eax,4 mov al, [_sqrt_tab+eax] shr eax,2 ret c_9_8: shr eax,2 mov al, [_sqrt_tab+eax] shr eax,3 ret ;bit 7..0 c_7_0: cmp eax,10h jb c_3_0 cmp eax,40h jb c_5_4 mov al, [_sqrt_tab+eax] shr eax,4 ret c_5_4: shl eax,2 mov al, [_sqrt_tab+eax] shr eax,5 ret c_3_0: cmp eax,4h jb c_1_0 shl eax,4 mov al, [_sqrt_tab+eax] shr eax,6 ret c_1_0: shl eax,6 mov al, [_sqrt_tab+eax] shr eax,7 ret isqrt_ endp _TEXT ENDS END |