8085 program to count the number of ones in contents of register B
Problem: Write an assembly language program to count the number of ones in the contents of register B and store the result at memory location 3050.
Example:
Algorithm:
- Convert the decimal number in Accumulator to its binary equivalent.
- Rotate the digits of the binary number right without carry.
- Apply a loop till the count is not zero to change the values of the D register and count.
- Copy the value of the D register to the accumulator and store the result.
Program:
MEMORY ADDRESS |
MNEMONICS |
COMMENTS |
2000 |
MVI B 75 |
B ← 75 |
2002 |
MVI C 08 |
C ← 75 |
2004 |
MVI D 00 |
D ← 00 |
2006 |
MOV A, B |
A ← B |
2007 |
RRC |
Rotate right without carry |
2008 |
JNC 200C |
Jump if Not Carry |
200B |
INR D |
D ← D+1 |
200C |
DCR C |
C ← C-1 |
200D |
JNZ 2007 |
Jump if Not Zero |
2010 |
MOV A, D |
A ← D |
2011 |
STA 3050 |
A → 3050 |
2014 |
HLT |
Stops execution |
Explanation:
- MVI B 75 moves 75 decimal numbers into the B register
- MVI C 08 moves 08 decimal number into C register, which is taken as counter as the number is of 8 bytes
- MVI D 00 moves 00 number into d register
- MOV A, B moves the contents of B register into A (accumulator) register
- RRC rotates the contents of A (which is 75 with binary equivalent 01110101) right
- JNC 200C jumps to 200C address and performs the instructions written there if the carry flag is not zero
- INR D increases the value of the D register by adding one to its contents
- DCR C decreases the value of the C register by subtracting one from its contents
- JNZ 2007 jumps to the 2007 address and performs the instructions written there if the zero flags are not zero
- MOV A, D moves the contents of the B register into the A register
- STA 3050 store the contents of A at 3050 memory location
- HLT stops execution
Approach : 2 [Using Rotate Instruction RLC] :
Here algorithm will be the same as above but here we are moving contents of accumulator bits to 1-bit left and then checking carry flag values and updating the count register accordingly.
If Carry Flag = 1 , then Count = Count + 1
Otherwise: Rotate Accumulator Left again without carrying and repeat the above procedure.
Both Different Instructions do the same task and concept of achieving end-goal is one and the same. Only Rotating Style is Different but these 2 methods can do the same task for us.
MEMORY ADDRESS |
MNEMONICS |
COMMENTS |
2000 |
MVI B,75H |
75H is the value that is to be tested for the Number of 1’s |
2002 |
MVI C,08H |
C is storing 8 because of the 8-bit data which we are using and we need to rotate the given number 8 times hence for loop [C]=08 |
2004 |
MVI D,00H |
This is a counter variable that will increment when 1 will be encountered |
2006 |
MOV A, B |
Move 75H into Accumulator for rotating purposes because RLC will work only on Accumulator. |
2007 |
RLC |
Rotate Accumulator Left Without Carry |
2008 |
JNC 200C |
If Carry Flag = 0 then just decrement counter and again start Rotating Accumulator using RLC. |
200B |
INR D |
If Carry Flag = 1 , then we have to increment Counter Register D =D + 1 |
200C |
DCR C |
Decrement value of C register because we have to rotate data 8 times because of 8 bits. So limit of rotation = 8 |
200D |
JNZ 2007 |
While [C] =0 this looping structure will execute from location 2007 and keep rotating accumulator contents until [C]=0 |
2010 |
MOV A, D |
Move Contents of register D which contains a total number of 1’s in given data into Accumulator. |
2011 |
STA 3050 |
Store Accumulator Contents into memory location 3050H as asked in the question. |
2014 |
HLT |
Stop execution of the given Program. |
Advantages of counting ones using this program:
- It is a simple and efficient way to count the number of ones in the contents of register B using only a few instructions.
- It is a low-level operation that provides a deep understanding of the internal workings of the 8085 microprocessor.
- It can be easily adapted for use in other programs and applications that require counting the number of ones in binary data.
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 register 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.
Last Updated :
07 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...