8085 program for Linear search | Set 2

Problem – Write an assembly language program in 8085 microprocessor to find a given number in the list of 10 numbers, if found store 1 in output else store 0 in output.

Example –

Assumption – Data to be found at 2040H, list of numbers from 2050H to 2059H and output at 2060H.

Algorithm –

  1. Load data byte to be searched in B register and counter in D register.
  2. Load starting element in Accumulator.
  3. Compare Accumulator and B register.
  4. If zero flag is set then JUMP to point 8 (as CMP instruction sets Zero flag when both are equal).
  5. Decrement D register
  6. If D>0 take next element in Accumulator and go to point 3.
  7. If D=0, this means element not found then store 00H. End the program.
  8. Store 01H as element found. End the program.

Program –

Address Label Instruction Comment
2000H Data LXI H, 2040H Load address of data to be searched
2003H MOV B, M Store data to be searched in B register
2004H LXI H, 2050H Load starting address of list
2007H MVI D, 0AH Counter for 10 elements
2009H NEXT MOV A, M Retrieve list element in Accumulator
200AH CMP B Compare element with data byte
200BH JZ STOP Jump if data byte found
200EH INX H Next element of list
200FH DCR D Decrement counter
2010H JNZ NEXT Jump to NEXT if D>0
2013H LXI H, 2060H Load address of output
2016H MVI M, 00H Store 00H
2018H HLT Halt
2019H STOP LXI H, 2060H Load address of output
201CH MVI M, 01H Store 01H
201EH HLT Halt

Explanation –

  1. One by one all elements are compared with data byte in B register
  2. If element found, loop ends and 01H is stored
  3. Loop executes 10 number of times
  4. If at the end of 10 iterations, data byte is not found then 00H is stored

Refer for Set-1: 8085 program to search a number in an array of n numbers

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

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.