Skip to content
Related Articles

Related Articles

8085 program for Binary search

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 23 Aug, 2021
Improve Article
Save Article

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: 

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

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 – 

2000H LDA 3000HLoad value to search for
2003H MOV B, ASave it in register B
2004H MVI A, 0 
2006H STA 3003HStore iteration number
2009H M0V L, A 
200AH MVI A, 9 
200CH MOV H, AStoring high and low indices in H-L pair done
200DHstart_loop:LDA 3003HLoad iteration number
2010H INR AIncrement iteration number
2011H STA 3003HStore back in 3003H
2014H MOV A, HStore high index in Accumulator
2015H CMP LCompare with lower index
2016H JC loop_endIf carry generated, this means high is less than low so binary search over
2019H ADD LAdd high to low
202AH RARRight rotate to divide by two and generate mid
202BH MOV C, ASave mid in register C
202CH JNC resetIf carry flag unset, go directly to reset.
202FH CMCForce unset carry flag
2031H LXI D, 3010HLoad start address in D-E pair
2034H ADD EAdd mid to E to get the offset
2035H MOV E, AGet the changed address back in E so it becomes a pointer to arr[mid]
2036H MVI A, 0Handle possible overflow
2038H ADC D 
2039H MOV D, AMemory index handled
203AH LDAX DLoad the array element in accumulator
203BH CMP BCompare with value to search
203CH JC else_blockImplies value is greater than value at mid, so we need low=mid+1
203FH JZ printIf zero flag set, match found. Jump to print block
2042H MOV A, CNeither executed so value<mid and we need high=mid-1
2043H DCR Amid=mid-1
2044H MOV H, Ah=mid
2045H JMP start_loopJump back
2046Helse_blockMOV A, CWe need low=mid+1
2047H INR Amid=mid+1
2048H MOV A, Ll=mid
2049H JMP start_loop 
204CHprintMVI A, 1Move 1 to Accumulator
204EH STA 3001HStore it in 3001H to indicate success
2051H MOV A, CMove index, that is mid, back to Accumulator
2052H STA 3002HStore it in 3002H
2055H JMP true_endJump to end of the code
2058Hloop_endMVI A, 2 
205AH STA 3001HStore 2 in 3001H to indicate failure

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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!