title: binary search of sorted array in assembly language
body: <pre><code>[org 0x100]
  ;This code is for counting the size 
          mov di, 0           ; to be used for indexing 
          mov bp, s_a
          mov cx, 0
          jmp count
  
  j1:     inc cx              ; storing the size in cx
  count:  mov ax, [bp+di] 
          add di, 2
          cmp ax, -1          ;-1 is the ending condition
          jne j1
  ;=================================;
  
          mov si, cx          ;Moving the size in si
          mov cx, 2           ;using cx for division number
  
  ;This code is for finding the centre point by division
  j2:     mov ax, si
          mov bx, 0
          mov bx, cx
          div bl
  ;=================================;
          inc cx
          inc cx 
          cmp al, 0           ;If al has 0 then the number is not in array
          je nfound
          mov dl, al  
          mov di, 0
  
  ;Increasing bx to point to the required number
  j3:     inc di
          inc di
          dec dx
          cmp dx, 0
          jne j3
  ;=================================;
          mov ax, [bp+di] ;Moving the centre number in array to ax
          mov dx, [b_s]       ;The number to be found
  
          cmp dx, ax
          je found            
  
          cmp dx, ax
          jl below
  
          cmp dx, ax
          jg above
  
  above:  add bp, di
          jmp j2
  
  below:  jmp j2
  
  found:  mov cx, bp
          add cx, di
          jmp exit
  
  nfound: mov ax, [bp]        ;This code is for the first element
          mov di, 0
          cmp ax, [b_s]
          je found            
          ;=================;
          mov cx, 0xffff
  exit:
  
  mov ax, 4c00h
  int 21h
  
  s_a: dw 1, 2, 3, 4, 5, -1
  b_s: dw 5
  </code></pre>
  
  <p>This is my code for binary search its working fine for all numbers here except for '1' and '5'. I have handled the for '1'. Please suggest a solution for '5' as well. which means last number. '-1' is not to be searched.</p>