CIS-2010 Homework #2: Count Bits

Due: Thursday, February 16, 2017


Write an assembly language function called "count_bits" that counts the number of one bits in the eax register. Your function should use push and pop instructions to save/restore any registers it uses besides eax.

Your function should loop 32 times (once for each bit in eax) and test the least significant bit of eax, incrementing a counter if a 1 bit is seen. Use a mask together with an and instruction and an appropriate following jump to test a bit. Use the shr instruction to shift eax one bit to the right after each loop pass. This will allow the loop to ultimately examine every bit in eax.

I encourage you to write a C main program to exercise your function so you can verify that it is working. However, you only need to submit the assembly language code to Moodle (one file).

Last Revised: 2017-02-07
© Copyright 2017 by Peter C. Chapin <>