# 8085 program to count number of ones in the given 8-bit number

Problem â€“ Write a program to count number of ones in the given 8-bit number use register B to display the count of ones where starting address is 2000 and the number is stored at 3000 memory address and store result into 3001 memory address. Example â€“ Algorithm â€“

1. Move 00 to register B immediately for count
2. Move 08 to register C immediately for shifting
3. Load the data of memory [3000] into accumulator
4. Rotate ‘A’ right with carry
5. Jump if no carry to step-7
6. Otherwise increase register B by 1
7. Decrease register C by 1
8. Jump if not zero to step-4
9. Move content of register B into accumulator
10. Store content of accumulator into memory [3001] (number of count)
11. Stop

Program â€“

Explanation â€“ Registers A, B and C are used for general purpose.

1. MVI is used to load an 8-bit given register immediately (2 Byte instruction)
2. LDA is used to load accumulator direct using 16-bit address (3 Byte instruction)
3. MOV is used to transfer the data from accumulator to register(any) or register(any) to accumulator (1 Byte)
4. RAR is used to shift ‘A’ right with carry (1 Byte instruction)
5. STA is used to store data from accumulator into memory direct using 16-bit address (3 Byte instruction)
6. INR is used to increase given register by 1 (1 Byte instruction)
7. JNC is used to jump to the given step if there is no carry (3 Byte instruction)
8. JNZ is used to jump to the given step if there is not zero (3 Byte instruction)
9. DCR is used to decrease given register by 1 (1 Byte instruction)
10. HLT is used to halt the program

### Advantages of counting ones using this program:

• It is a simple and efficient way to count the number of ones in an 8-bit number using only a few instructions.

• It can be easily adapted for use in other programs and applications that require counting the number of ones in binary data.

• It provides a low-level understanding of binary operations and the 8085 microprocessor architecture.

### Disadvantages of counting ones using this program:

• It requires specialized knowledge of assembly language programming and the 8085 microprocessor architecture.

• It may not be efficient for larger sets of data, as it involves looping through each bit of the number and checking for a carry.

• It may not be practical for use in high-level programming languages or applications where speed and efficiency are critical.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next