# counting_bits.asm .data .text .global main main: mov $0xA3, %ebx # value to count bits in mov $0, %ecx # bit counter check_zero: cmp $0, %ebx # done? (that is, no more bits) jz done mov %ecx, %edx # lsb of %ecx set? and $0x01, %edx jz lsb_is_zero add %1, %ecx # if so, count it lsb_is_zero: shr %ebx, $0x01 # shift off the bit just counted jmp check_zero # try next bit done: mov %ecx, %eax ret