Make your own free website on Tripod.com

MKoD - D Programming Language

isPrime() number assembly example - code-name isprime.d

Very Kool! isPrime() number example:

// isprime.d
/+
 ' A assembly example for returning whether any  
 ' number between 0 to 4294967295 is prime number.
 +/
module primeutil;

private import std.stdio;

// comment out the below statement to hide the main() function.
debug = isprime;

bool isPrime( in uint uiNum )
{
    uint uiIsPrime = false;
    
    asm
    {
        // load in the uint number to check
        mov   EAX,[ uiNum ];
        
        // 0 and 1 are valid values, 
        // but neither are a prime number.
        mov   EDX,EAX;
        cmp   EDX,1;
        jbe    _notprime;    // if 0 or 1 return false
        
        // since 2 will be used as the divisor below
        cmp   EDX,2;       
        je    _prime;        // if EDX == 2 then goto _prime (true)
        mov   EDX,0;         // reset EDX = 0

        // setup the loop counter
        mov   ECX,EAX;       // ECX = EAX
        shr   ECX,1;         // ECX = ECX / 2
        mov   EBX,2;         // first divisor is EDX = 2

    _next:
        // start block, is EAX divisible by EBX? 
        push  EAX;   
        push  EDX;    
        mov   EDX,0;
        div   EBX;
        cmp   EDX,0;        
        pop   EDX;        
        pop   EAX;
        // end block, is EAX divisible by EBX? 
    
        jz   _notprime;      // if EDX == 0 then (EAX is divisible by EBX) goto _notprime
        inc   EBX;           // next divisor equals EBX = EBX + 1
       
        // start loopd block
        dec   ECX;           // ECX = ECX - 1
        cmp   ECX,0;
        jnz  _next;          // if ECX (counter) <> 0 the loop back to _next
        // end loopd block
        
    _prime: // is a prime, so set EAX to true (1).
        mov   EAX,1;
        jmp   _exit;

    _notprime: // is not a prime, so set EAX to false (0).
        mov   EAX,0;
        jmp   _exit;
    
    _exit:
        mov   [ uiIsPrime ],EAX;
    } 
    
    return ( uiIsPrime == 0U ? false : true );
}    

debug( isprime )
{
int main()
{
    uint uiy = 0U;

    writefln( "All the numbers listed below between 0 to 1000 are Primes!" );

    for ( uint uix = 0U; uix <= 1000U; uix++ )
    {
        if ( isPrime( uix ) == true )
        {
            writef( "%4d, ", uix );
            uiy++;

            if ( ( uiy % 10U ) == 0U )
            {
                uiy = 0U;
                writefln();  
            }
        }
    }

    return 0;
    
} // end int main()
}

C:\dmd\MKOD_ex>..\bin\dmd isprime.d
C:\dmd\bin\..\..\dm\bin\link.exe isprime,,,user32+kernel32/noi;

C:\dmd\MKOD_ex>isprime
All the numbers listed below between 0 to 1000 are Primes!
   2,    3,    5,    7,   11,   13,   17,   19,   23,   29,
  31,   37,   41,   43,   47,   53,   59,   61,   67,   71,
  73,   79,   83,   89,   97,  101,  103,  107,  109,  113,
 127,  131,  137,  139,  149,  151,  157,  163,  167,  173,
 179,  181,  191,  193,  197,  199,  211,  223,  227,  229,
 233,  239,  241,  251,  257,  263,  269,  271,  277,  281,
 283,  293,  307,  311,  313,  317,  331,  337,  347,  349,
 353,  359,  367,  373,  379,  383,  389,  397,  401,  409,
 419,  421,  431,  433,  439,  443,  449,  457,  461,  463,
 467,  479,  487,  491,  499,  503,  509,  521,  523,  541,
 547,  557,  563,  569,  571,  577,  587,  593,  599,  601,
 607,  613,  617,  619,  631,  641,  643,  647,  653,  659,
 661,  673,  677,  683,  691,  701,  709,  719,  727,  733,
 739,  743,  751,  757,  761,  769,  773,  787,  797,  809,
 811,  821,  823,  827,  829,  839,  853,  857,  859,  863,
 877,  881,  883,  887,  907,  911,  919,  929,  937,  941,
 947,  953,  967,  971,  977,  983,  991,  997,
C:\dmd\MKOD_ex>
Mars: fourth Rock from the Sun.