Skip to content
Related Articles

Related Articles

8085 program to count number of ones in the given 8-bit number
  • Last Updated : 29 Nov, 2019

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 –

MemoryMnemonicsOperandsComment
2000MVIB, 00[B] <- 00
2002MVIC, 08[C] <- 08
2004LDA[3000][A] <- [3000]
2007RARrotate ‘A’ right with carry
2008JNC200Cjump if no carry
200BINRB[B] <- [B] + 1
200CDCRC[C] <- [C] – 1
200DJNZ2007jump if not zero
2010MOVA, B[A] <- [B]
2011STA[3001]number of ones
2014HLTStop

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

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :