A simple assembly program to count the number of ones in a register
For some more advanced ideas, check out https://stackoverflow.com/a/109025/2189500.

But expanding on CWallach's code, how about:

mov edx, 255 ; setup edx with test value
xor eax, eax ; count

start_loop:
bt edx, 0 ; sets carry flag if bit 0 is set
adc al, 0 ; add with carry
shr edx, 1 ; drop off bit
jnz start_loop ; exit if edx is now 0



xor is a smaller instruction for setting a register to zero.
bt sets the carry flag for use in
adc adds 0 to al, but also includes the contents of the carry flag.


And once you've got your head wrapped around that, how about:

mov edx, 255 ; setup edx with test value
xor eax, eax ; count

shr edx, 1 ; move rightmost bit to carry flag
start_loop:
adc al, 0 ; add with carry
shr edx, 1 ; move rightmost bit to carry flag
jnz start_loop ; exit if edx is now 0
adc al, 0 ; count last bit


This takes advantage of the fact that when shr shifts, it moves the shifted bit into the carry flag.  And while there are more instructions overall (5 vs 6), there are fewer within the loop (4 vs 3).