Count bits - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Count bits

Hi there,

I am trying write a program which count number of 1 in the 8-bit. I am storing input number in decimal and trying to find out how many 1 is there in binary 1. But not getting how to do. Please find my code as follow:

[code]
data segment
n db 7 ;Define input number data ends



code segment
assume cs:code, ds:data, ss:stack1

start:

mov ax, data
mov ds, ax
mov al, 0
mov bl, n
;and bl, 00000001b

inc al

mov ah, 4ch
int 21h
code ends

end start
[/code]

Is any one can help me?

Thanking You,

Comments

  • skaa7skaa7 Posts: 33Member
    EBX will equal to number of 1-bits.
    [code] mov al,[n]
    mov ebx,0
    mov ecx,8
    mk:
    push eax
    and al,1
    cmp al,1
    jne msk
    inc ebx
    msk:
    pop eax
    shr al,1
    loop mk
    [/code]
  • kasparov29kasparov29 Posts: 4Member
    Thanks for your help.

    Best Regards,

  • jack21jack21 usaPosts: 2Member
    sub ax,ax
    mov al, 'your 8 bit value'
    mov cx,8
    

    L1:
    shr al,1
    jnc zero
    inc ah
    zero:
    dec cx
    jnz L1

    the # of set bits set will be in ah.

  • jack21jack21 usaPosts: 2Member
    sub ax,ax
        mov al,"your 8 bit value"
    mov cx,8
    

    L1: shr al,1
    jnc zero
    inc ah
    Zero:
    dec cx
    jnz L1

    the # of set bits will be in ah
    
Sign In or Register to comment.