# 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 –

Memory Mnemonics Operands Comment
2000 MVI B, 00 [B] <- 00
2002 MVI C, 08 [C] <- 08
2004 LDA [3000] [A] <- [3000]
2007 RAR rotate ‘A’ right with carry
2008 JNC 200C jump if no carry
200B INR B [B] <- [B] + 1
200C DCR C [C] <- [C] – 1
200D JNZ 2007 jump if not zero
2010 MOV A, B [A] <- [B]
2011 STA [3001] number of ones
2014 HLT Stop

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 their is no carry (3 Byte instruction)
8. JNZ is used to jump to the given step if their 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

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Priti Halder