Open In App

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

Last Updated : 25 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Advantages of searching a number in an array:

  1. Searching a number in an array is a common operation used in many algorithms and applications, such as finding a specific record in a database or searching for a value in a list.
     
  2. Searching an array is a fast operation that can be completed in O(n) time complexity, where n is the number of elements in the array.
     
  3. It is a simple and straightforward task that can be easily implemented in any programming language.
     

Disadvantages of searching a number in an array:

  1. If the array is unsorted, searching for a specific number may require iterating through the entire array, which can be inefficient for very large arrays.
     
  2. If multiple elements in the array have the same value as the search number, the program may not be able to distinguish between them and return incorrect results.
     
  3. If the array is very large and memory is limited, storing the entire array in memory may not be feasible, which could require a more complex solution such as searching the array in smaller parts or using an external data storage.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads