Skip to content
Related Articles

Related Articles

Improve Article

8085 program to search a number in an array of n numbers

  • Last Updated : 22 May, 2018

Problem – Write an assembly language program in 8085 to search a given number in an array of n numbers. If number is found, then store F0 in memory location 3051 otherwise store 0F in 3051.

Assumptions – Count of elements in an array is stored at memory location 2050. Array is stored from starting memory address 2051 and number which user want to search is stored at memory location 3050.

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.

 

Examples –




Algorithm –

  1. Make the memory pointer points to memory location 2050 by help of LXI H 2050 instruction
  2. Store value of array size in register C
  3. Store number to be search in register B
  4. Increment memory pointer by 1 so that it points to next array index
  5. Store element of array in accumulator A and compare it with value of B
  6. If both are same i.e. if ZF = 1 then store F0 in A and store the result in memory location 3051 and go to step 9
  7. Otherwise store 0F in A and store it in memory location 3051
  8. Decrement C by 01 and check if C is not equal to zero i.e. ZF = 0, if true go to step 3 otherwise go to step 9
  9. End of program

Program –

MEMORY ADDRESSMNEMONICSCOMMENT
2000LXI H 2050H <- 20, L <- 50
2003MOV C, MC <- M
2004LDA 3050A <- M[3050]
2007MOV B, AB <- A
2008INX HHL <- HL + 0001
2009MOV A, MA <- M
200ACMP BA – B
200BJNZ 2014Jump if ZF = 0
200EMVI A F0A <- F0
2010STA 3051M[3051] <- A
2013HLTEND
2014MVI A 0FA <- 0F
2016STA 3051M[3051] <- A
2019DCR CC <- C – 01
201AJNZ 2008Jump if ZF = 0
201DHLTEND

Explanation – Registers used A, B, C, H, L and indirect memory M:

  1. LXI H 2050 – initialize register H with 20 and register L with 50
  2. MOV C, M – assign content of indirect memory location, M which is represented by registers H and L to register C
  3. LDA 3050 – loads the content of memory location 3050 in accumulator A
  4. MOV B, A – move the content of A in register B
  5. INX H – increment HL by 1, i.e. M is incremented by 1 and now M will point to next memory location
  6. MOV A, M – move the content of memory location M in accumulator A
  7. CMP B – subtract B from A and update flags of 8085
  8. JNZ 2014 – jump to memory location 2014 if zero flag is reset i.e. ZF = 0
  9. MVI A F0 – assign F0 to A
  10. STA 3051 – stores value of A in 3051
  11. HLT – stops executing the program and halts any further execution
  12. MVI A 0F – assign 0F to A
  13. STA 3051 – stores value of A in 3051
  14. DCR C – decrement C by 01
  15. JNZ 2008 – jump to memory location 2008 if zero flag is reset
  16. HLT – stops executing the program and halts any further execution
My Personal Notes arrow_drop_up
Recommended Articles
Page :