Sunday 3 March 2019

Assembly: Factors and primes



program factors;
// Phil Gardner

#include( "stdlib.hhf" )

begin factors;

    // The value to test as prime or not
    mov( 2, bl );

    testPrime:

    // Count of how many factors found for the value
    mov( 0, dx );

    // The divisor to test as a factor
    mov( 1, cl );

    tryFactor:



        mov( bl, al );
        cbw();

        // Is this a factor? Check remainder
        div( cl );
        cmp( ah, 0 );
        jz isFactor;

    nextFactor:


        inc( cl );

        cmp( cl, bl );
        jle tryFactor;
        jg finishedSingleTest;

    isFactor:

        inc( dl );

        stdout.puti8( cl );
        stdout.put( " goes into " );
        stdout.puti8( bl );
        stdout.put( " " );
        stdout.puti8( al );
        stdout.put( " times", nl );


        jmp nextFactor;


    finishedSingleTest:

        cmp( dl, 2 );
        jz reportAsPrime;

        jmp underline;



    reportAsPrime:

        stdout.puti8( bl );
        stdout.put( " IS A PRIME", nl );  
  

    underline:

        stdout.put( "- - - - - -", nl );


    readyForNextTest:

        inc( bl );

        cmp( bl, 100 );
        jle testPrime;

    stdout.put( nl, "Finished testing.", nl );

end factors;