Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TITLE AsmBinarySearch Procedure (AsmBinarySearch.asm)
- .586
- .model flat,C
- INCLUDE \Irvine\Irvine32.inc
- AsmBinarySearch PROTO, searchValue:DWORD, arrayPTR:PTR DWORD, count:DWORD
- .data
- first DWORD ?
- last DWORD ?
- mid DWORD ?
- .code
- ;----------------------------------------------------------
- AsmBinarySearch PROC USES edi, searchValue:DWORD, arrayPTR:PTR DWORD, count:DWORD
- ;
- ; Performs a binary search for a 32 - bit integer
- ; in an array of integers. Returns the value of the subscript
- ; of the matching array element in EAX, or -1 in EAX if the
- ; search value was not found in the array.
- ; ----------------------------------------------------------
- mov eax, 0 ;FIRST VAR
- mov ebx, count ;LAST VAR
- dec ebx
- mov ecx, searchValue ;comparison VAR
- mov edi, arrayPTR ;array pointer
- .WHILE eax <= ebx
- mov esi, ebx
- add esi, eax ;middle = (last+first/2)
- shr esi, 1 ;esi has middle VAR
- mov edx, [edi+esi*4] ;current value of array[mid] NOTE COULD INCREASE SPEED BY SHIFTING RIGHT INSTEAD OF MULT
- .IF edx < ecx
- mov eax, esi
- inc eax
- .ELSEIF edx > ecx
- mov ebx, esi
- dec ebx
- .ELSE
- mov eax, esi
- ret
- .ENDIF
- .ENDW
- mov eax,-1 ; Set the return value to indicate that
- ret ; return
- AsmBinarySearch ENDP
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement