# 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) je done mov %ebx, %edx # lsb of %ebx set? and $0x01, %edx jz lsb_is_zero add $1, %ecx # if so, count it lsb_is_zero: shr $1, %ebx # shift off the bit just counted jmp check_zero # try next bit done: mov %ecx, %eax ret