Friday, 8 March 2019

Assembly: Calculating powers of 2 using left shifts


program powersOfTwo;
// Phil Gardner

#include( "stdlib.hhf" )

begin powersOfTwo;

    // Start at 1, then double repeatedly
    mov( 1, eax );

    mov( 1, bl );

    display:

    stdout.put( "Col no. " );
    stdout.puti8( bl );
    stdout.put( ": " );

    mov( bl, cl );
    dec( cl );
    stdout.put( " 2 to the power of " );
    stdout.puti8( cl );
    stdout.put( " is " );

    stdout.puti32( eax );
    stdout.put( nl );

    shl( 1, eax );

    inc( bl );
    cmp( bl, 16 );

    jle display;

end powersOfTwo;