Open In App

8085 program for Binary search

Last Updated : 23 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite – Binary Search 
Problem – Write an assembly language program in the 8085 microprocessor to find a given number in the list of 10 numbers. If found store 1 in output, else store 2 in output. Also, store the number of iterations and the index of the element, if found. 

Example: Let the list be as follows: 

Test-1: 
Input: 21H (at 3000H)
Output: 1 (success) in 3001H,
2 (index) in 3002H, and
3 (iteration number) in 3003H.

Test-2: 
Input: 22H (at 3000H)
Output: 2 (failure) in 3001H,
X (don't care) in 3002H, and
4 (iteration before failure) in 3003H 

Assumption – 
Assume data to compare it with is stored in 3000H, list of numbers is from 3010H to 3019H and results are stored as follows: number of iterations in 3003H, success/failure (1/2) in 3001H and index in 3002H 

Algorithm – 

  1. Move 0 to Accumulator and store it in 3003H, to indicate number of iterations so far.
  2. Move 0 and 9 to L and H registers, respectively.
  3. Load the data to search for in Accumulator from 3000H and shift it to B register.
  4. Retrieve the number of iterations from 3003H, increase it by one and store back in 3003H.
  5. Move value of H register to Accumulator and compare with L register.
  6. If carry is generated, binary search is over so JUMP to step 20.
  7. Add value of L register to Accumulator and right rotate it.
  8. Store value of Accumulator in register C and force reset carry flag, if set.
  9. Load the start address of the array in D-E register pair.
  10. Add the value of accumulator to Register E and store the result in E.
  11. Move 0 to Accumulator and use the ADC command to add any possible carry generated due to previous addition and store it back in Register D.
  12. Load the value pointed to by D-E pair and compare with Register B. If carry is generated, JUMP to step 15 and if Zero flag is set, JUMP to step 17.
  13. Move value of Register C to Accumulator and decrement Accumulator.
  14. Move value of Accumulator to H and JUMP back to step 4.
  15. Move value of Register C to Accumulator and increment Accumulator.
  16. Move value of Accumulator to L and JUMP back to step 4.
  17. Move 1 to Accumulator ad store in 3001H to indicate success.
  18. Move value of Register C to Accumulator and store it in 3002H to save the index.
  19. JUMP to statement 21.
  20. Move 2 to Accumulator and store it in 3001H to indicate failure.
  21. End the program.

Program – 

Address Label Instruction Comment
2000H   LDA 3000H Load value to search for
2003H   MOV B, A Save it in register B
2004H   MVI A, 0  
2006H   STA 3003H Store iteration number
2009H   M0V L, A  
200AH   MVI A, 9  
200CH   MOV H, A Storing high and low indices in H-L pair done
200DH start_loop: LDA 3003H Load iteration number
2010H   INR A Increment iteration number
2011H   STA 3003H Store back in 3003H
2014H   MOV A, H Store high index in Accumulator
2015H   CMP L Compare with lower index
2016H   JC loop_end If carry generated, this means high is less than low so binary search over
2019H   ADD L Add high to low
202AH   RAR Right rotate to divide by two and generate mid
202BH   MOV C, A Save mid in register C
202CH   JNC reset If carry flag unset, go directly to reset.
202FH   CMC Force unset carry flag
2030H reset NOP  
2031H   LXI D, 3010H Load start address in D-E pair
2034H   ADD E Add mid to E to get the offset
2035H   MOV E, A Get the changed address back in E so it becomes a pointer to arr[mid]
2036H   MVI A, 0 Handle possible overflow
2038H   ADC D  
2039H   MOV D, A Memory index handled
203AH   LDAX D Load the array element in accumulator
203BH   CMP B Compare with value to search
203CH   JC else_block Implies value is greater than value at mid, so we need low=mid+1
203FH   JZ print If zero flag set, match found. Jump to print block
2042H   MOV A, C Neither executed so value<mid and we need high=mid-1
2043H   DCR A mid=mid-1
2044H   MOV H, A h=mid
2045H   JMP start_loop Jump back
2046H else_block MOV A, C We need low=mid+1
2047H   INR A mid=mid+1
2048H   MOV A, L l=mid
2049H   JMP start_loop  
204CH print MVI A, 1 Move 1 to Accumulator
204EH   STA 3001H Store it in 3001H to indicate success
2051H   MOV A, C Move index, that is mid, back to Accumulator
2052H   STA 3002H Store it in 3002H
2055H   JMP true_end Jump to end of the code
2058H loop_end MVI A, 2  
205AH   STA 3001H Store 2 in 3001H to indicate failure
205DH true_end HLT Terminate

Explanation –  

  1. We move value of higher and lower index (9 and 0 in this case) to H and L registers respectively in step 2
  2. Higher and lower indices are compared in step 5. On getting a carry, which indicates low>high, we jump to end of loop else go to step 6.
  3. In steps 7 and 8 we add value of H and L registers and right rotate it, which is equivalent to (high+low)/2 in order to find the index in say C language
  4. In step 10, we add the value of mid to start address of array so that it acts as an offset, similar to how *(arr+x) and arr[x] is identical in C.
  5. Step 11 ensures no overflow occurs.
  6. In step 12, we compare the value at mid index with the value to be searched. If it’s equal, we jump out of the loop and set the values appropriately.
  7. If they are not equal, step 12 branches appropriately to let us increment/decrement mid by 1 and move that value to L/H register, as necessary (just like high=mid-1 or low=mid+1 is done in C) and go back to start of loop, that is step 2.

Note – This approach will fail if the element to be searched is smaller than the smallest element in the array. In order to handle that, add an extra zero to the start of the loop and move values 10 and 1 to H-L pair in step 2, respectively.
 


Previous Article
Next Article

Similar Reads

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 - Load data byte to be searched in B register and counter in D r
2 min read
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 s
4 min read
8085 program to convert a BCD number to binary
Problem – Write an assembly language program for converting a 2 digit BCD number to its binary equivalent using 8085 microprocessor. Examples: Input : 72H (0111 0010)2Output : 48H (in hexadecimal) (0011 0000)2((4x16)+(8x1))=72Algorithm: Load the BCD number in the accumulatorUnpack the 2 digit BCD number into two separate digits. Let the left digit
2 min read
8085 code to convert binary number to ASCII code
Problem - Assembly level program in 8085 which converts a binary number into ASCII number. Example - Assumptions - Binary number which have to convert in ASCII value is stored at memory location 2050 and output will be displayed at memory location 3050 and 3051. Algorithm - Load the content of 2050.Then separate the LSB of the no. using ANI 0F inst
5 min read
8085 program to find the element that appears once
Prerequisite - Find the element that appears once in an array where every other element appears twice Problem – Given 11 numbers, write an assembly language program to find the element that appears once where 5 numbers appear twice and one element appears once. Examples: Input : [01H, 02H, 09H, 01H, 01H, 02H, 0AH, 01H, 09H, 03H, 03H] Output : 0AH E
2 min read
8085 program to find maximum and minimum of 10 numbers
Problem - Write an assembly language program in 8085 microprocessor to find maximum and minimum of 10 numbers. Example - Minimum: 01H, Maximum: FFH In CMP instruction: If Accumulator &gt; Register then carry and zero flags are reset If Accumulator = Register then zero flag is set If Accumulator &lt; Register then carry flag is set Assumption - List
3 min read
8085 program to find maximum of two 8 bit numbers
Problem - Write a assembly language program to find maximum of two 8 bit numbers in 8085 microprocessor. Assumptions - Starting memory locations and output memory locations are 2050, 2051 and 3050 respectively. Example - Algorithm - Load value in the accumulator Then, copy the value to any of the register Load next value in the accumulator Compare
1 min read
8085 program to add two 8 bit numbers
Problem - Write an assembly language program to add two 8 bit numbers stored at address 2050 and address 2051 in 8085 microprocessor. The starting address of the program is taken as 2000. Example - Algorithm - Load the first number from memory location 2050 to accumulator.Move the content of accumulator to register H.Load the second number from mem
2 min read
8085 program to multiply two 8 bit numbers
Problem - Multiply two 8 bit numbers stored at address 2050 and 2051. Result is stored at address 3050 and 3051. Starting address of program is taken as 2000. Example - Algorithm - We are taking adding the number 43 seven(7) times in this example.As the multiplication of two 8 bit numbers can be maximum of 16 bits so we need register pair to store
3 min read
8085 program to convert an 8 bit number into Grey number
Prerequisite - Binary to/from Gray Code Problem - Write an assembly language program in 8085 which convert an 8 bit number into grey number Example - Assumption - 8 bit number (input) is stored at memory location 2050 and output to be stored at memory location 3050. Algorithm - Load the content of memory location 2050 in AccumulatorReset carry flag
2 min read