8051 program to count the number of 1’s & 0’s in a number

By


Write a program to count the number of 1’s & 0’s in a number


                 ORH 0000H
                 CLR  PSW 3
                CLR   PSW   4
    MOV R0, #00H
    MOV R, #00H
    MOV DTPR, #3000H
    MOV A, @DPTR
L1:           RLC A
                JNZ  L2         
                INC R0
L2:           DJNZ R0, L1
                MOV  A, #08H
                CLR C
                MOV R, A
AGAIN : SJMP AGAIN


RESULT:-
INPUT                                   OUTPUT
11010010-D2H                       4-(1’S)
                                                4-(0’S)

6 comments:

  1. your program does not run in mcu. It shows a lot of errors.
    NOT GOOD

    ReplyDelete
    Replies
    1. mov r1,#08h
      mov r0,#00h
      mov dptr,#8100h
      movx a,@dptr
      l2:rlc a
      jnc l1
      inc r0
      l1:djnz r1,l2
      inc dptr
      mov a,r0
      movx @dptr,a
      again: sjmp again

      Delete
  2. input number should be in 8100h location
    this program is to count 1s in a byte only
    output result should be in 8101 location
    please check any one

    ReplyDelete
  3. ORG 0000H
    MOV R6,#00H
    MOV R7,#00H
    MOV R2,#08H
    MOV R0,#30H
    MOV A,@R0
    CLR C
    BACK: RLC A
    JNC ZEROCNT
    INC R7
    DJNZ R2,BACK
    SJMP STOP
    ZEROCNT: INC R6
    DJNZ R2,BACK
    STOP: SJMP STOP
    END

    ReplyDelete
  4. Write an ALP to count number of 0's (Zeros) of the content of the register B and Store the count at the memory location 8051H.

    ReplyDelete

If you have any questions or droughts feel free to ask here.