- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 8085 program for Binary search

Here we will see how to perform binary search in 8085.

**Problem Statement**:

Write 8085 Assembly language program to perform binary search on a set of data stored at location F110 to F119. The key is located at F100.

## Discussion

To perform binary search, the array must be sorted. We are taking the lower limit into L and upper limit into H. The array location is stored at DE register pair. The mid is calculated using (H + L)/2. To perform this division, we are just shifting it to the right one time. Then put the mid value into D and check the item located using DE. if the number is same, then jump to end and store 1 at F101. This indicates that item is found, and also store the mid value as index. If the mid is not matched, then there are two case, if the number is smaller than the mid element, then it is in the lower half, so the upper limit will be mid – 1, in another case it will be in the other side, so the lower limit will be mid + 1. If the item is not found it will store 02 at location F101.

## Input

Address | Data |
---|---|

F100 | 48 |

… | … |

F110 | 04 |

F111 | 08 |

F112 | 22 |

F113 | 2B |

F114 | 31 |

F115 | 38 |

F116 | 48 |

F117 | 51 |

F118 | 54 |

F119 | 62 |

… | … |

Address | Data |
---|---|

F100 | 9D |

… | … |

F110 | 04 |

F111 | 08 |

F112 | 22 |

F113 | 2B |

F114 | 31 |

F115 | 38 |

F116 | 48 |

F117 | 51 |

F118 | 54 |

F119 | 62 |

… | … |

## Flow Diagram

## Program

Address | HEX Codes | Labels | Mnemonics | Comments |
---|---|---|---|---|

F000 | 3A, 00, F1 | | LDA F100 | Load the key into A |

F003 | 47 | | MOV B,A | Store key into B |

F004 | AF | | XRA A | Clear Acc |

F005 | 32, 03, F1 | | STA F103 | Store number of iterations into F103H |

F008 | 6F | | MOV L,A | Store A into L also |

F009 | 26, 09 | | MVI H,09 | Load 9 into H |

F00B | 3A, 03, F1 | START | LDA F103 | load number of iterations into A |

F00E | 3C | | INR A | Increase A |

F00F | 32, 03, F1 | | STA F103 | Restore number of iterations |

F012 | 7C | | MOV A,H | Take the upper limit from H to A |

F013 | BD | | CMP L | Compare A and L |

F014 | DA, 46, F0 | | JC L2 | If CY = 1, jump to L2 |

F017 | 85 | | ADD L | Otherwise add L with A |

F018 | 1F | | RAR | Right rotate to get half of it |

F019 | 4F | | MOV C,A | Store mid value into C |

F01A | D2, 1E, F0 | | JNC RESET | If CY = 0, jump to Reset |

F01D | 3F | | CMC | Complement the carry |

F01E | 11, 10, F1 | RESET | LXI D,F110 | Load the initial address of array into DE |

F021 | 83 | | ADD E | Add E and Mid |

F022 | 5F | | MOV E,A | Store index into E |

F023 | AF | | XRA A | Clear A |

F024 | 8A | | ADC D | Add D with A with the carry |

F025 | 57 | | MOV D,A | Restore A into D |

F026 | 1A | | LDAX D | Load A with the value of mid position |

F027 | B8 | | CMP B | compare it with Key |

F028 | DA, 34, F0 | | JC ELSE | If CY = 1, jump to ELSE |

F02B | CA, 3A, F0 | | JZ PRINT | If they are same, jump to PRINT |

F02E | 79 | | MOV A,C | Take the mid from C to A |

F02F | 3D | | DCR A | Decrease A to get mid – 1 |

F030 | 67 | | MOV H,A | Update the upper limit with mid – 1 |

F031 | C3, 0B, F0 | | JMP START | Jump to START again |

F034 | 79 | ELSE | MOV A,C | Load the mid from C to A |

F035 | 3C | | INR A | increase A to get mid + 1 |

F036 | 7D | | MOV A,L | update lower limit with mid + 1 |

F037 | C3, 0B, F0 | | JMP START | Jump to START again |

F03A | 3E, 01 | PRINT | MVI A,01 | Load 1 as item is found |

F03C | 32, 01, F1 | | STA F101 | Store result at F101 |

F03F | 79 | | MOV A,C | Take the mid from C to A |

F040 | 32, 02, F1 | | STA F102 | Store index of the key into F102 |

F043 | C3, 4B, F0 | | JMP END | End the task |

F046 | 3E, 02 | L2 | MVI A,02 | Load 2 into A that key not present |

F048 | 32, 01, F1 | | STA F101 | Store result at F102 |

F04B | 76 | END | HLT | Terminate the program |

## Output

Address | Data |
---|---|

F101 | 01 |

F102 | 06 |

Address | Data |
---|---|

F101 | 02 |

- Related Questions & Answers
- Python Program for Binary Search
- Java Program for Binary Search (Recursive)
- C Program for Binary Search (Recursive and Iterative)?
- C++ Program to Search for an Element in a Binary Search Tree
- 8085 Program to perform linear search
- C++ Program to Implement a Binary Search Algorithm for a Specific Search Sequence
- Binary Search in C++ program?
- Binary Search program in JavaScript
- Python Program for Depth First Binary Tree Search using Recursion
- Binary Search
- Java program to implement binary search
- 8085 program to convert gray to binary
- 8085 Program to convert ASCII to binary
- Program to perform linear search in 8085 Microprocessor
- C++ Program to Perform Uniform Binary Search