/* * looking at a loop in x86 assembler * (based on 05-machine-control lecture slides from * http://www.cs.cmu.edu/afs/cs/academic/class/15213-f10/www/schedule.html) * * $ gcc -m32 -O1 -o loop loop.c * $ ./loop * There are 5 bits that are 1 in 0x4321 . * * $ gdb loop * (gdb) disas main * (gdb) disas pcount * */ #include int pcount(unsigned x){ int result = 0; while (x){ result += x & 0x1; x >>= 1; } return result; } int main(){ unsigned data = 0x4321; int ones = pcount(data); printf("There are %i bits that are 1 in 0x%x .\n", ones, data); return 0; }