Write
X86 program to sort the list of integers in ascending/descending order. Read
the input from the text file and write the sorted data back to the same text
file using bubble sort
Algorithm for Bubble Sort
Bubble sort algorithm works by comparing the first two elements
of an array and swapping if necessary, i.e., if you want to sort the elements
of array in ascending order and if the first element is greater than second
then, you need to swap the elements but, if the first element is smaller than
second, you need not swap the element. Then, again second and third elements
are compared and swapped if it is required and this process go on until last
and second last element is compared and swapped. This completes the first pass
of bubble sort. By doing this we bubble out the largest element at the last
location in the array.
If there are n elements to be sorted then, the process
mentioned above should be repeated n times to get the sorted array. As,
in each pass one element is bubbled out. So to place all the elements in the
unsorted array at their proper place we need to repeat the above said process
for n times.
Following figure will give you better understanding of bubble sort:
Figure: working of Bubble
Sort
j=bufferlen-1
while(j>0)
{
i=cnt1=bufferlen-1
while(i>0)
{
if(a[i]>a[i+1])
swap(a[i],a[i+1])
i--;
}
j--;
}
b_sort2.asm (64-bit
nasm code file)
%macro file 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
Section .data
accept_fn db "Enter
file name",10,13
accept_fn_len equ
$-accept_fn
openmsg db 10,13,"File
Opened Successfully"
openmsg_len equ $-openmsg
closemsg db 10,13,"File
Closed Successfully"
closemsg_len equ $-closemsg
errormsg db
10,13,"Failed to open file", 0xa
errormsg_len equ $-errormsg
space db " "
space_len equ $ - space
sortmsg db 10,13,"After
Sorting "
sortmsg_len equ $-sortmsg
Section .bss
buffer resb 200
bufercpy resb 200
bufferlen resb 8
filename resb 50
cnt1 resb 8
cnt3 resb 8
fdisplay resb 8
Section .text
global _start
_start:
file
1,1,accept_fn,accept_fn_len
file 0,0,filename,50
dec rax
mov byte[filename + rax],0 ; blank char/null char
file 2, filename, 2,
777 ; Opening filein read-write mode
with permission to read write
and
execute to all(owner, group and other)
mov qword[fdisplay],
rax ;RAX contains file descriptor
value
cmp rax,-1H ;
on failure returns -1
je ERROR ; if file is successfull opened rax
is +ve else it is -ve
file 1,1,openmsg,
openmsg_len
jmp next1
ERROR:
file 1,1, errormsg,
errormsg_len
jmp EXIT
next1:
file 0,[fdisplay]
,buffer,200 ; reading contents of file
in buffer
; rax contains actual number of bytes read
mov qword[bufferlen],rax ; Total number of passes
mov qword[cnt1],rax ; number of comparisons within a
single pass
mov qword[cnt3],rax ; total number of elements to be sorted
dec byte[bufferlen];
BUBBLE:
mov al, byte[bufferlen];
mov byte[cnt1],al
mov rsi,buffer
mov rdi,buffer+1
loop:
mov bl,byte[rsi]
mov cl,byte[rdi]
cmp bl,cl
ja SWAP
inc rsi
inc rdi
dec byte[cnt1]
jnz loop
dec byte[bufferlen]
jnz BUBBLE
jmp END
SWAP:
mov byte[rsi],cl
mov byte[rdi],bl
inc rsi
inc rdi
dec byte[cnt1]
jnz loop
dec byte[bufferlen]
jnz BUBBLE
END:
file 1,1,
sortmsg,sortmsg_len ;writing onto output screen
file 1,1, buffer,qword[cnt3]
file
1,qword[fdisplay],sortmsg,sortmsg_len
;writing to input.txt
file
1,qword[fdisplay],buffer,qword[cnt3]
;writing to input.txt
;Closing file
mov rax,3
mov rdi,filename
syscall
file 1,1,
closemsg,closemsg_len
EXIT:
mov rax,60
mov rdi,0
syscall
input.txt
5
4
3
2
1
Output:
[root@localhost ~]# nasm -f
elf64 b_sort2.asm
[root@localhost ~]# ld -o s
b_sort2.o
[root@localhost ~]# ./s
Enter file name
input.txt
File Opened Successfully
After Sorting
12345
File Closed
Successfully[root@localhost ~]#
input.txt
file content
5
4
3
2
1
After Sorting
12345
very useful material...
ReplyDelete