Program of Binary Search in Assembly Language

A program written in assembly language comprises of a progression of memory helper processor guidelines and meta-proclamations (referred to differently as orders, pseudo-directions and pseudo-operations), remarks and information. Assembly language guidelines as a rule comprise of an opcode mental helper pursued by a rundown of information, contentions or parameters.Code of Binary Search in assembly language is given below.

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO , a1:DWORD
GetStdHandle PROTO, a1:DWORD
WriteConsoleA PROTO, a1:DWORD, a2: PTR BYTE, a3: Dword, a4: ptr dword, a5: dword
ReadConsoleA PROTO, a1:DWORD, a2: PTR BYTE, a3: Dword, a4: ptr dword, a5: dword
CloseHandle PROTO, a1:dword
.data
array dword 5 dup(?)
buffer1 dword 10 dup(?)
msg1 byte 0dh,0ah,”Enter a Number : “,0
msg2 byte 0dh,0ah,”Cant write in arrary anymore”,0dh,0ah,”You pressed -1″,0
msg3 byte 0dh,0ah,”Enter a number to Search : “,0
msg4 byte 0dh,0ah,”Not Found”,0
msg5 byte 0dh,0ah,”No Found”,0
inhandle dword ?
outhandle dword ?
count dword ?
num sdword 0
check sdword ?
searchnum sdword ?
.code

ToDecimal PROC uses ebx ecx edx esi edi
push esi
cmp byte ptr [esi], ‘-‘
jne itspostive
inc esi
itspostive:
mov ebx, 0
L1:
mov cl, [esi]
cmp cl, 0
je endL1
mov edi, 10
mov eax, ebx
mul edi
sub cl, 48
movzx ecx, cl
add eax, ecx
mov ebx, eax
inc esi
jmp L1
endL1:
mov eax, ebx
pop esi
cmp byte ptr[esi], ‘-‘
jne itwaspositive
neg eax
itwaspositive:
ret
ToDecimal ENDP

ToString PROC uses ebx ecx edx esi edi
push eax
mov edi, 10
mov ecx, 0
findnumofdigits:
cmp eax, 0
je exitfinddigits
mov edx, 0
div edi
inc ecx
jmp findnumofdigits
exitfinddigits:
pop eax
push ecx
mov byte ptr [esi+ecx] , 0
savecharacters:
mov edx, 0
div edi
add dl, 48
mov [esi+ecx-1], dl
loop savecharacters
pop eax
ret
ToString ENDP

binarySearch proc uses eax ebx edx esi
mov esi , [esp+20]
mov edx , [esp+24]
mov ebx , [esp+28]
mov eax , [esp+32]
cmp ebx , eax
jnae theend
push eax
push ebx
push edx
push esi
sub ebx , eax
mov esi , eax
mov eax , ebx
cdq
mov bl , 2
idiv bl
mov ebx , eax
add eax , esi
mov edi , eax
pop esi
pop edx
pop ebx
pop eax
cmp [esi+edi] , edx
jne nextline
mov searchnum , edi
jmp theend2
nextline:
cmp [esi+edi] , edx
jna theend2
push edi
sub edi , 4
mov ebx , edi
pop edi
push eax
push ebx
push edx
push esi
call binarySearch
ret 16
theend2:
push edi
add edi , 4
mov eax , edi
pop edi
push eax
push ebx
push edx
push esi
call binarySearch
ret 16
theend:
mov check , -1
ret 16
binarySearch endp

main proc
pushad
mov edi , offset array
startwhile:
invoke GetStdHandle, -11
mov outhandle, eax
invoke WriteConsoleA, outhandle, offset msg1, lengthof msg1 -1, offset count, 0
invoke GetStdHandle, -10
mov inhandle, eax
invoke ReadConsoleA, inhandle, offset buffer1, lengthof buffer1, offset count, 0
mov ebx, count
mov buffer1[ebx-2], 0
mov esi, offset buffer1
call ToDecimal
mov num , eax
cmp num , -1
je jmpfromloop
mov dword ptr [edi],eax
add edi , 4
jmp startwhile
jmpfromloop:
invoke GetStdHandle, -11
mov outhandle, eax
invoke WriteConsoleA, outhandle, offset msg2, lengthof msg2 -1, offset count, 0
popad
invoke GetStdHandle, -11
mov outhandle, eax
invoke WriteConsoleA, outhandle, offset msg3, lengthof msg3 -1, offset count, 0
invoke GetStdHandle, -10
mov inhandle, eax
invoke ReadConsoleA, inhandle, offset buffer1, lengthof buffer1, offset count, 0
pushad
mov ebx, count
mov buffer1[ebx-2], 0
mov esi, offset buffer1
call ToDecimal
mov searchnum , eax
popad
push 0
push lengthof array
push searchnum
push offset array
call binarySearch
cmp check , -1
jne found
invoke GetStdHandle, -11
mov outhandle, eax
invoke WriteConsoleA, outhandle, offset msg4, lengthof msg4 -1, offset count, 0
jmp exited
found:
invoke GetStdHandle, -11
mov outhandle, eax
invoke WriteConsoleA, outhandle, offset msg5, lengthof msg5 -1, offset count, 0
exited:
invoke ExitProcess , 0
main endp
end main

Comment if you have any query or confusion.

Add a Comment

Your email address will not be published. Required fields are marked *