In this program, we will write an 8085 Program to count the number of 1’s in a register in the 8085 microprocessor with a program flow chart and explanation of the program.
8085 Program to count the number of 1’s in a register
Program Statement
Write a program in the assembly language programming of 8085 to count the number of 1’s in a byte stored in the H register and store the count in the E register. Draw flowchart
Explanation of Program
- We have a byte that is stored in the H register.
- Initialize the counter-1 = 8 i.e. total number of bits in a byte.
- Initialize counter-2 = 0 to count the number of 1’s.
- We will rotate the number in A along with carry by 1 bit to the right if there is a carry we will increment counter-2.
- Decrement counter-1. This process will continue till all the bits are checked. The counter-2 will indicate the number of 1’s present in the byte.
- The result of counter-2 is stored in B.
- e.g. : H = 0101 0010 = 52, Number of 1’s = 3
Flow Chart of Program
Assembly Language Program
Label | Instruction | Comment | Operation |
---|---|---|---|
MVI B, 00H | Initialize counter-1 = 00H | B = 00H | |
MVI C, 08H | Initialize counter-2 = 08H | C = 08H | |
MOV A, H | Load the number in the accumulator | A = 52H | |
L2: | RAR | Rotate the number along with the carry | |
JNC L1 | if no carry goto L1 | ||
INR B | increment the count | B = B + 1 | |
L1: | DCR C | decrement counter-2 | C = C -1 |
JNZ L2 | check if counter = 0? if not continue | ||
MOV E, B | Store the result in register E | E = 03H | |
HLT | terminate program execution | Stop |