0% found this document useful (0 votes)
44 views

MPMC EXP 10 Code & Output Print

The document describes three assembly language programs that implement different sorting algorithms: 1) Bubble sort - It sorts an array of elements by repeatedly swapping adjacent elements that are in the wrong order. 2) Insertion sort - It builds a sorted array by taking one element from the unsorted part and inserting it into the sorted part. 3) Selection sort - It repeatedly finds the minimum element from the unsorted part and swapping it with the element in the current position.

Uploaded by

Amisha Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

MPMC EXP 10 Code & Output Print

The document describes three assembly language programs that implement different sorting algorithms: 1) Bubble sort - It sorts an array of elements by repeatedly swapping adjacent elements that are in the wrong order. 2) Insertion sort - It builds a sorted array by taking one element from the unsorted part and inserting it into the sorted part. 3) Selection sort - It repeatedly finds the minimum element from the unsorted part and swapping it with the element in the current position.

Uploaded by

Amisha Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT NO: 10 SORTING ALGORITHMS Date:29/05/2023

PROGRAMS:

EXP 10[A]. Write an assembly language program to implement Bubble Sort.

Code
section .data SYS_READ n,1
prompt db "Enter number of
elements",10 call input
plen equ $-prompt call bubble_sort
prompt2 db "Enter elements",10 call display
p2len equ $-prompt2
prompt3 db "Elements are:",10 mov eax,1
p3len equ $-prompt3 mov ebx,0
newline db 10 int 80h
space db ' '
input:
section .bss SYS_WRITE prompt2,p2len
n resb 4 mov [i],dword '0'
arr resb 10 loop1:
i resb 4 mov esi,[i]
trash resb 1 cmp esi,[n]
jge after1
%macro SYS_WRITE 2
mov eax,4 sub esi,'0'
mov ebx,1 add esi,arr
mov ecx,%1 SYS_READ esi,1
mov edx,%2
int 80h inc dword[i]
%endmacro jmp loop1
after1:
%macro SYS_READ 2 ret
mov eax,3
mov ebx,2 display:
mov ecx,%1 SYS_WRITE prompt3,p3len
mov edx,%2 mov [i],dword '0'
int 80h loop2:
mov eax,3 mov esi,[i]
mov ebx,2 cmp esi,[n]
mov ecx,trash jge after2
mov edx,1
int 80h sub esi,'0'
%endmacro add esi,arr
SYS_WRITE esi,1
section .text SYS_WRITE space,1
global _start
inc dword[i]
_start: jmp loop2
SYS_WRITE prompt,plen after2:

Nidhi Shanbhag 211105036 |P a g e


EXPERIMENT NO: 10 SORTING ALGORITHMS Date:29/05/2023

SYS_WRITE newline,1 jge after4


ret
mov esi,arr
bubble_sort: add esi,ecx ;esi points to arr[ecx]
mov al,0 ;al is counter for outer
loop, initialise to 0 mov ah,[esi]
mov bl,[n] mov bh,[esi+1]
sub bl,'0' cmp ah,bh
sub bl,1 ;bl is n-1 jle after5 ;if ah>bh
loop3:
cmp al,bl ;repeat until al<n-1 mov [esi+1],ah ;swap esi and esi+1
jge after3 mov [esi],bh

pushad after5:
call display inc cl
popad jmp loop4
after4:
mov ecx,0 ;ecx is counter for inner inc al
loop, initialise to 0 jmp loop3
mov dl,bl after3:
sub dl,al ;dl is n-1-al ret
loop4:
cmp cl,dl ;repeat until cl<n-1-al

Output:

Nidhi Shanbhag 211105036 |P a g e


EXPERIMENT NO: 10 SORTING ALGORITHMS Date:29/05/2023

EXP 10[B]. Write an assembly language program to implement Insertion Sort.

Code:
section .data mov eax,1
prompt db "Enter number of elements",10 mov ebx,0
plen equ $-prompt int 80h
prompt2 db "Enter elements",10
p2len equ $-prompt2 input:
prompt3 db "Elements are:",10 SYS_WRITE prompt2,p2len
p3len equ $-prompt3 mov [i],dword '0'
newline db 10 loop1:
space db ' ' mov esi,[i]
cmp esi,[n]
section .bss jge after1
n resb 4
arr resb 10 sub esi,'0'
i resb 4 add esi,arr
trash resb 1 SYS_READ esi,1

%macro SYS_WRITE 2 inc dword[i]


mov eax,4 jmp loop1
mov ebx,1 after1:
mov ecx,%1 ret
mov edx,%2
int 80h display:
%endmacro SYS_WRITE prompt3,p3len
mov [i],dword '0'
%macro SYS_READ 2 loop2:
mov eax,3 mov esi,[i]
mov ebx,2 cmp esi,[n]
mov ecx,%1 jge after2
mov edx,%2
int 80h sub esi,'0'
mov eax,3 add esi,arr
mov ebx,2 SYS_WRITE esi,1
mov ecx,trash SYS_WRITE space,1
mov edx,1
int 80h inc dword[i]
%endmacro jmp loop2
after2:
section .text SYS_WRITE newline,1
global _start ret

_start: insertion_sort:
SYS_WRITE prompt,plen mov eax,1 ;al is counter for outer loop,
SYS_READ n,1 initialise to 1
mov bl,[n] ;bl=n
call input sub bl,'0'
call insertion_sort loop3:
call display cmp al,bl ;repeat until al<n
jge after3

Nidhi Shanbhag 211105036 |P a g e


EXPERIMENT NO: 10 SORTING ALGORITHMS Date:29/05/2023

jge after4
pushad
call display mov dh,[arr+ecx]
popad mov [arr+ecx+1],dh ;arr[ecx], arr[ecx+1]

mov ecx,0 dec ecx ;ecx--


mov cl,al jmp loop4
sub cl,1 ;cl is counter for inner loop, after4:
initialise to al-1 mov [arr+ecx+1],dl ;arr[ecx+1]=dl

mov dl,[arr+eax] ;store arr[eax] in dl inc al ;al++


loop4: jmp loop3
cmp cl,0 ;repeat until cl>=0 and... after3:
jl after4 ret

cmp dl,[arr+ecx] ;...dl<arr[cl]


Output:

EXP 10[C]. Write an assembly language program to implement Selection Sort.

Code
section .data n resb 4
prompt db "Enter number of arr resb 10
elements",10 i resb 4
plen equ $-prompt trash resb 1
prompt2 db "Enter elements",10
p2len equ $-prompt2 %macro SYS_WRITE 2
prompt3 db "Elements are:",10 mov eax,4
p3len equ $-prompt3 mov ebx,1
newline db 10 mov ecx,%1
space db ' ' mov edx,%2
int 80h
%endmacro
section .bss
Nidhi Shanbhag 211105036 |P a g e
EXPERIMENT NO: 10 SORTING ALGORITHMS Date:29/05/2023

%macro SYS_READ 2 mov esi,[i]


mov eax,3 jge after2
mov ebx,2
mov ecx,%1 add esi,arr
mov edx,%2 SYS_WRITE esi,1
int 80h SYS_WRITE space,1
mov eax,3
mov ebx,2 inc dword[i]
mov ecx,trash jmp loop2
mov edx,1 after2:
int 80h SYS_WRITE newline,1
%endmacro ret

section .text selection_sort:


global _start mov eax,0 ;al is counter for outer
loop, initialise to 0
_start: mov bl,[n]
SYS_WRITE prompt,plen sub bl,1 ;bl is n-1
SYS_READ n,1 loop3:
sub byte[n],'0' cmp al,bl ;repeat until al<n-1
call input jge after3
call selection_sort
call display pushad
call display
mov eax,1 popad
mov ebx,0
int 80h mov ecx,0 ;clear ecx register
mov cl,al ;ecx is counter for inner
input: loop, initialise to al+1
SYS_WRITE prompt2,p2len add cl,1
mov [i],dword 0
loop1: mov edi,arr
mov esi,[i] add edi,eax ;edi points to arr[eax]
cmp esi,[n] loop4:
jge after1 cmp cl,[n] ;repeat until cl<n
jge after4
add esi,arr
SYS_READ esi,1 mov esi,arr
add esi,ecx ;esi points to arr[ecx]
inc dword[i]
jmp loop1 mov bh,[esi]
after1: mov dh,[edi]
ret cmp bh,dh
jge after5 ;if bh<dh
display:
SYS_WRITE prompt3,p3len mov edi,arr ;edi points to arr[ecx]
mov [i],dword 0 add edi,ecx
loop2: after5:
Nidhi Shanbhag 211105036 |P a g e
EXPERIMENT NO: 10 SORTING ALGORITHMS Date:29/05/2023

inc cl mov [edi],bh


jmp loop4
after4: inc al
;; swap arr[eax] and [edi] jmp loop3
mov bh,[arr+eax] after3:
mov dh,[edi] ret
mov [arr+eax],dh

Output:

Conclusion:
All programs were executed successfully

Nidhi Shanbhag 211105036 |P a g e

You might also like