Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TITLE AsmBinarySearch Procedure (AsmBinarySearch.asm)
  2.  
  3.     .586
  4.     .model flat,C
  5.     INCLUDE \Irvine\Irvine32.inc
  6.     AsmBinarySearch PROTO, searchValue:DWORD, arrayPTR:PTR DWORD, count:DWORD
  7.  
  8.     .data
  9.     first DWORD ?
  10.     last DWORD ?
  11.     mid DWORD ?
  12.     .code
  13.     ;----------------------------------------------------------
  14.     AsmBinarySearch PROC USES edi, searchValue:DWORD, arrayPTR:PTR DWORD, count:DWORD
  15.     ;
  16.     ; Performs a binary search for a 32 - bit integer
  17.     ; in an array of integers. Returns the value of the subscript
  18.     ; of the matching array element in EAX, or -1 in EAX if the
  19.     ; search value was not found in the array.
  20.     ; ----------------------------------------------------------
  21.    
  22.     mov eax, 0              ;FIRST VAR
  23.     mov ebx, count      ;LAST VAR
  24.     dec ebx
  25.     mov ecx, searchValue    ;comparison VAR
  26.     mov edi, arrayPTR       ;array pointer
  27.    
  28.     .WHILE eax <= ebx
  29.         mov esi, ebx
  30.         add esi, eax        ;middle = (last+first/2)
  31.         shr esi, 1          ;esi has middle VAR
  32.         mov edx, [edi+esi*4]    ;current value of array[mid] NOTE COULD INCREASE SPEED BY SHIFTING RIGHT INSTEAD OF MULT
  33.  
  34.         .IF edx < ecx
  35.             mov eax, esi
  36.             inc eax
  37.         .ELSEIF edx > ecx
  38.             mov ebx, esi
  39.             dec ebx
  40.         .ELSE
  41.             mov eax, esi
  42.             ret
  43.         .ENDIF
  44.     .ENDW
  45.  
  46.     mov eax,-1      ; Set the return value to indicate that
  47.     ret         ; return
  48.  
  49. AsmBinarySearch ENDP
  50. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement