Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

EXPERIMENT NO: 9 SEARCHING ALGORITHMS Date:29/05/2023

PROGRAMS:
EXP 09[A]. Write an assembly language program to implement Linear Search.

Code
section .data section .text
msg1 db 'Enter the number of elements global _start
in the array: '
msg1len equ $-msg1 _start:
msg2 db 'Enter the elements of the write_message msg1,msg1len
array:',10 read_message num,9
msg2len equ $-msg2 write_message msg2,msg2len
msg3 db 'Enter the element to be
searched:' mov eax,[num]
msg3len equ $-msg3 mov [i],eax
msg4 db 'Element found at index '
msg4len equ $-msg4 mov esi,array
msg5 db 'Element not found',10 mov eax,num
msg5len equ $-msg5
newline db ' ',10 inputelement:
newlinelen equ $-newline mov eax,3
mov ebx,2
;array declaration and initialization mov ecx,element
array dw 0,0,0,0,0,0,0,0,0 mov edx,2
arraylen equ 9 ; static array count int 80h

section .bss mov ebx,[element]


num resb 9 mov [esi],ebx
element resb 9
i resb 9 dec byte[i]
n resb 9 inc esi
index resb 9
cmp byte[i],'0'
%macro write_message 2 jne inputelement
mov eax,4
mov ebx,1 write_message msg3,msg3len
mov ecx,%1 read_message n,9
mov edx,%2
int 80h mov eax,[num]
%endmacro mov [i],eax

%macro read_message 2 mov esi,array


mov eax,3 mov eax,i
mov ebx,2
mov ecx,%1 linearSearch:
mov edx,%2 mov eax,[esi]
int 80h mov [element],eax
%endmacro
mov al,[n]
Komal Ambe Roll No:211105028 |P a g e
EXPERIMENT NO: 9 SEARCHING ALGORITHMS Date:29/05/2023

sub al,'0' exit:


mov bl,[element] write_message msg4,msg4len
sub bl,'0'
cmp al,bl mov eax,[num]
je exit sub eax,'0'
mov ebx,[i]
dec byte[i] sub ebx,'0'
inc esi sub eax,ebx
add eax,'0'
cmp byte[i],'0' mov [index],eax
jne linearSearch
write_message index,9
write_message msg5,msg5len write_message newline,newlinelen

mov eax,1 mov eax,1


mov ebx,0 mov ebx,0
int 80h int 80h

Output:

EXP 09[B]. Write an assembly language program to implement Binary Search.

Code:
section .data ;array declaration and initialization
msg1 db 'Enter the elements of the array dw 0,0,0,0,0,0,0,0,0
array:',10 arraylen equ 9 ; static array count
msg1len equ $-msg1
msg3 db 'Enter the number of elements in section .bss
the array:' num resb 9
msg3len equ $-msg3 element resb 9
msg4 db 'Element found at index ' ele resb 9
msg4len equ $-msg4 i resb 9
msg5 db 'Element not found ' l resb 9
msg5len equ $-msg5 h resb 9
msg6 db 'Enter the element to be searched:' mid resb 9
msg6len equ $-msg6 count resb 9
newline db ' ',10
newlinelen equ $-newline %macro calc_mid 3
mov eax,4

Komal Ambe Roll No:211105028 |P a g e


EXPERIMENT NO: 9 SEARCHING ALGORITHMS Date:29/05/2023

mov ebx,1
mov ecx,'' dec byte[num]
mov edx,0 inc esi
int 80h
cmp byte[num],'0'
mov al,[%1] jne inputelement
sub al,'0'
mov bl,[%2] write_message msg6,msg6len
sub bl,'0' read_message ele,9
add al,bl
mov bl,'2' mov ebx,'0'
sub bl,'0' mov [l],ebx
div bl mov eax,[i]
add ax,'0' mov [h],eax
mov [%3],ax dec byte[h]
%endmacro
calc_mid l,h,mid
%macro write_message 2
mov eax,4 mov eax,[count]
mov ebx,1 mov [mid],eax
mov ecx,%1
mov edx,%2
int 80h
%endmacro
mov esi,array
%macro read_message 2 mov eax,i
mov eax,3
mov ebx,2 mov esi,array
mov ecx,%1 mov eax,i
mov edx,%2 jmp search
int 80h
%endmacro inc_1:
inc byte[count]
section .text dec byte[mid]
global _start call float_int

_start: float_int:
write_message msg3,msg3len cmp byte[mid],'1'
read_message num,9 jge inc_1
write_message msg1,msg1len ret

mov eax,[num] incr:


mov [i],eax inc esi
inc bl
mov esi,array jmp array_index
mov eax,num
array_index:
inputelement: cmp al,bl
read_message element,2 jge incr
ret
mov ebx,[element]
mov [esi],ebx search:

Komal Ambe Roll No:211105028 |P a g e


EXPERIMENT NO: 9 SEARCHING ALGORITHMS Date:29/05/2023

mov al,[l] mov bl,'1'


mov bl,[h] call array_index
cmp al,bl
jg not_found mov eax,[esi]
mov [element],eax
calc_mid l,h,mid
mov al,'0' mov al,[ele]
mov [count],al mov bl,[element]
call float_int
cmp al,bl
mov esi,array jl left
mov eax,[i]

mov al,[count]
jg right mov [l],al
je found jmp search

left: found:
mov al,[count] ;inc byte[count]
sub al,'0'
mov bl,'1' write_message msg4,msg4len
sub bl,'0' write_message count,9
sub al,bl jmp exit
add al,'0'
mov [h],al not_found:
jmp search write_message msg5,msg5len

right: exit:
mov al,[count] write_message newline,newlinelen
sub al,'0'
mov bl,'1' mov eax,1
sub bl,'0' mov ebx,0
add al,bl int 80h
add al,'0'

Output:

Conclusion:
All programs were executed successfully

Komal Ambe Roll No:211105028 |P a g e

You might also like