Question 5: (back)

Write a "CntBits" program that counts the number of one bits in a 16-bit integer value input from the user. Do not use any built-in functions in HLA's library to count these bits for you. Use the shift or rotate instructions to extract each bit in the value.

program Question5;

#include( "stdlib.hhf" );

static
    a:      uns16;
    cnt:    uns16;
    tot:    uns8;

begin Question5;
    stdout.put( "Please Enter a Number: ", nl );
    stdin.get( a );

    mov( a, ax );
    mov( 0, cnt );
    mov( 0, tot );
    while( cnt < 16 ) do
        stdout.put( ax, nl );

        shr( 1, ax );        
        if( @c ) then
            add( 1, tot );
        endif;

        add( 1, cnt );
    endwhile;

    stdout.put( "Total: ", tot, nl );
end Question5;