Skip to content
Related Articles

Related Articles

8085 program to count the number of ones in contents of register B

Improve Article
Save Article
  • Last Updated : 02 Jun, 2022
Improve Article
Save Article

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. 




  • 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.


2000MVI B 75B ← 75
2002MVI C 08C ← 75
2004MVI D 00D ← 00
2006MOV A, BA ← B
2007RRCRotate right without carry
2008JNC 200CJump if Not Carry
200BINR DD ← D+1
200CDCR CC ← C-1
200DJNZ 2007Jump if Not Zero
2010MOV A, DA ← D
2011STA 3050A → 3050
2014HLTStops execution


  1. MVI B 75 moves 75 decimal numbers into the B register
  2. MVI C 08 moves 08 decimal number into C register, which is taken as counter as the number is of 8 bites
  3. MVI D 00 moves 00 number into d register
  4. MOV A, B moves the contents of B register into A (accumulator) register
  5. RRC rotates the contents of A (which is 75 with binary equivalent 01110101) right
  6. JNC 200C jumps to 200C address and performs the instructions written there if the carry flag is not zero
  7. INR D increases the value of the D register by adding one to its contents
  8. DCR C decreases the value of the C register by subtracting one from its contents
  9. JNZ 2007 jumps to the 2007 address and performs the instructions written there if the zero flags are not zero
  10. MOV A, D moves the contents of the B register into the A register
  11. STA 3050 store the contents of A at 3050 memory location
  12. 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.

2000MVI B,75H75H is the value that is to be tested for the Number of 1’s
2002MVI C,08HC 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
2004MVI D,00HThis is a counter variable that will increment when 1 will be encountered
2006MOV A, BMove 75H into Accumulator for rotating purposes because RLC will work only on Accumulator.
2007RLCRotate Accumulator Left Without Carry
2008JNC 200CIf Carry Flag = 0 then just decrement counter and again start Rotating Accumulator using RLC.
200BINR DIf Carry Flag = 1 , then we have to increment Counter Register D =D + 1
200CDCR CDecrement value of C register because we have to rotate data 8 times because of 8 bits. So limit of rotation = 8
200DJNZ 2007While [C] =0 this looping structure will execute from location 2007 and keep rotating accumulator contents until [C]=0
2010MOV A, DMove Contents of register D which contains a total number of 1’s in given data into Accumulator.
2011STA 3050Store Accumulator Contents into memory location 3050H as asked in the question.
2014HLTStop execution of the given Program.
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!