@instruction<EOL><INDENT>def POPCNT(cpu, dest, src):<DEDENT>
count = <NUM_LIT:0><EOL>source = src.read()<EOL>for i in range(src.size):<EOL><INDENT>count += Operators.ITEBV(dest.size, (source >> i) & <NUM_LIT:1> == <NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:0>)<EOL><DEDENT>dest.write(count)<EOL>cpu.OF = False<EOL>cpu.SF = False<EOL>cpu.AF = False<EOL>cpu.CF = False<EOL>cpu.PF = False<EOL>cpu.ZF = source == <NUM_LIT:0><EOL>
This instruction calculates of number of bits set to 1 in the second
operand (source) and returns the count in the first operand (a destination
register).
Count = 0;
For (i=0; i < OperandSize; i++) {
    IF (SRC[ i] = 1) // i'th bit
        THEN Count++;
    FI;
}
DEST = Count;
Flags Affected
OF, SF, ZF, AF, CF, PF are all cleared.
ZF is set if SRC = 0, otherwise ZF is cleared