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

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.

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 ADDRESS MNEMONICS COMMENT
2000 LXI H 2050 H <- 20, L <- 50
2003 MOV C, M C <- M
2004 LDA 3050 A <- M[3050]
2007 MOV B, A B <- A
2008 INX H HL <- HL + 0001
2009 MOV A, M A <- M
200A CMP B A – B
200B JNZ 2014 Jump if ZF = 0
200E MVI A F0 A <- F0
2010 STA 3051 M[3051] <- A
2013 HLT END
2014 MVI A 0F A <- 0F
2016 STA 3051 M[3051] <- A
2019 DCR C C <- C – 01
201A JNZ 2008 Jump if ZF = 0
201D HLT END

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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.