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)

3 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

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