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 –
- Move 00 to register B immediately for count
- Move 08 to register C immediately for shifting
- Load the data of memory [3000] into accumulator
- Rotate ‘A’ right with carry
- Jump if no carry to step-7
- Otherwise increase register B by 1
- Decrease register C by 1
- Jump if not zero to step-4
- Move content of register B into accumulator
- Store content of accumulator into memory [3001] (number of count)
- 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.
- MVI is used to load an 8-bit given register immediately (2 Byte instruction)
- LDA is used to load accumulator direct using 16-bit address (3 Byte instruction)
- MOV is used to transfer the data from accumulator to register(any) or register(any) to accumulator (1 Byte)
- RAR is used to shift ‘A’ right with carry (1 Byte instruction)
- STA is used to store data from accumulator into memory direct using 16-bit address (3 Byte instruction)
- INR is used to increase given register by 1 (1 Byte instruction)
- JNC is used to jump to the given step if there is no carry (3 Byte instruction)
- JNZ is used to jump to the given step if there is not zero (3 Byte instruction)
- DCR is used to decrease given register by 1 (1 Byte instruction)
- 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.
Please Login to comment...