Professional Documents
Culture Documents
MPMC EXP 09 Code & Output Print
MPMC EXP 09 Code & Output Print
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
Output:
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
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 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