Tuesday 2 February 2016

Binary Search Example of Assembly Language | Assembly Language Codes

Binary Search (Assembly Language)

Download Binary Search (Assembly Language)


[org 0x0100]

   jmp main

binarySearch:
   push bp
   mov bp , sp
   push ax
   push bx
   push cx
   push si
  
   mov bx , [bp+10]      ;ARR
   mov ax , [bp+6]    ;MIN
   mov cx , [bp+4]    ;MAX
   cmp ax , cx
   jg false
   add ax , cx
   mov cx , 2
   div cx
   mul cx
   mov si , ax
   mov ax , [bp+8]    ;KEY
   mov cx , [bx+si]      ;ELEMENT
   cmp ax , cx
   je true
   jl less
   jg great
   
   next:
      call binarySearch
      pop ax
      
      theEnd:
         mov [bp+12] , ax
         pop si
         pop bx
         pop cx
         pop ax
         pop bp
         ret 8

false:
   mov ax , -1
   jmp theEnd

true:
   mov ax , si
   mov cx , 2
      div cx
   jmp theEnd
  
less:
   sub sp , 2
   push bx
   push ax
   mov ax , [bp+6]
   push ax
   mov ax , si
   mov cx , 2
   div cx
   dec ax
   push ax
   jmp next

great:
   sub sp , 2
   push bx
   push ax
   mov ax , si
   mov cx , 2
   div cx
   inc ax
   push ax
   mov ax , [bp+4]
   push ax
   jmp next
  
  
main:
   sub sp , 2
   push elements
   push word [key]
   push 0
   mov ax , [size]
   dec ax
   push ax
   call binarySearch
   pop dx
  
   mov ax , 0x4c00
   int 0x21


key: dw 0
size: dw 10
elements: dw 1,3,7,11,13,18,23,28,35,41


1 comment

  1. Wow. Amazing Code. Does it handle memory leaks as well?

    ReplyDelete

Recent Posts