Saturday, April 30, 2016

8051 Program – count number of ones in a byte

; count number of 1's in a given data byte
; data: 4000H
; result: 4001H
ORG 0H
DATA_ADDR EQU 4000H
MAIN:
MOV DPTR,#DATA_ADDR ; fill memory
MOV A, #55H
MOVX @DPTR,A
LCALL COUNT
SJMP MAIN
COUNT:
MOV DPTR,#DATA_ADDR ;Load dptr with #DATA_ADDR
MOVX A,@DPTR ;move data from external memory location to a
MOV R0,#0H ;load r0 with 0
MOV R1,#8H ;load r1 with 8
CLR C ;clear carry bit
up: RLC A ;rotate a left through carry
JNC next ;if no carry, jump to label next
INC R0 ;increment r0
next: DJNZ R1,up ;decrement r1, and jump to label next, if r1>0
INC DPTR ;increment dptr
MOV A,R0 ;move data from r0 to a
MOVX @DPTR,A ;move data from a to external memory location
return:
RET
END



Related topics:
8051 Program - search an element in an array   |   8051 Program - search a byte in array and count match   |   8051 Program - square of a given number 8bit   |   8051 Program - square root of a given number 8bit   |   8051 Program - square and cube operation   |   8051 Program - count 0 and 1 in a byte   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment