Microprocessor/Codes/org-pen/Practical-9/bsort.asm

176 lines
3.0 KiB
NASM
Raw Normal View History

;NAME:- Chinmay M. Mule
;CLASS:-SE COMP II SHIFT
;ROLLNO:-34 PRN:-S18111044
; Assignment Name:- Write X86 program to sort the list of integers in
; ascending order.
; Read the input from the text file and write the sorted
; data back to the same text file using bubble sort.
%include "macro.asm"
section .data
nline db 10
nline_len equ $-nline
ano db 10,10,10,10,"MPL assignment 07-Bubble sort using file operations"
db 10,"........................................",10
ano_len equ $-ano
filemsg db 10,"Enter filename of input data:"
filemsg_len equ $-filemsg
omsg db 10,"Sorting using bubble sort operation successful."
db 10,"Output stored in same file.....",10,10
omsg_len equ $-omsg
errmsg db 10,"ERROR in opening/reading/writing file..",10
errmsg_len equ $-errmsg
exitmsg db 10,10,"Exit from program....",10,10
exitmsg_len equ $-exitmsg
section .bss
buf resb 1024
buf_len equ $-buf ;buffer length
filename resb 50
filehandle resq 1
abuf_len resq 1 ;actual buffer length
array resb 10
n resq 1 ;passes
section .text
global _start
_start:
print ano,ano_len
print filemsg,filemsg_len
read filename,50
dec rax
mov byte[filename+rax],0 ; blank char/null char
fopen filename ;on success returns handle
cmp rax,-1H
je Error
mov[filehandle],rax
fread[filehandle],buf,buf_len
dec rax
mov [abuf_len],rax
call bsort
jmp Exit
Error:print errmsg,errmsg_len
Exit:print exitmsg,exitmsg_len
exit
;........................................................................
bsort: ;Bubble sort procedure
call buf_array
xor rax,rax
mov rbp,[n]
dec rbp
xor rcx,rcx
xor rdx,rdx
xor rsi,rsi
xor rdi,rdi
mov rcx,0 ;i=0
oloop:mov rbx,0 ;j=0
mov rsi,array ;a[j]
iloop:mov rdi,rsi ;a[j+1]
inc rdi
mov al,[rsi]
cmp al,[rdi]
jbe next
mov dl,0
mov dl,[rdi] ;swap
mov [rdi],al
mov [rsi],dl
next:inc rsi
inc rbx ; j++
cmp rbx,rbp
jb iloop
inc rcx
cmp rcx,rbp
jb oloop
fwrite [filehandle],omsg,omsg_len
fwrite [filehandle],array,[n]
fclose [filehandle]
print omsg,omsg_len
print array,[n]
ret
Error1:
print errmsg,errmsg_len
ret
;....................................................
buf_array:
xor rcx,rcx
xor rsi,rsi
xor rdi,rdi
mov rcx,[abuf_len]
mov rsi,buf
mov rdi,array
next_num:
mov al,[rsi]
mov [rdi],al
inc rsi ;number
inc rsi ;newline
inc rdi
inc byte[n] ; counter
dec rcx
dec rcx
jnz next_num
ret
; OUTPUT:-
;chinmay@ubuntu:~Desktop/MPL Programs/Assignment7$ nasm -f elf64 bsort.asm
;chinmay@ubuntu:~Desktop/MPL Programs/Assignment7$ ld -o b bsort.o
;chinmay@ubuntu:~Desktop/MPL Programs/Assignment7$ ./b
;MPL assignment 07-Bubble sort using file operations
;........................................
;Enter filename of input data:fl.txt
;Sorting using bubble sort operation successful.
;Output stored in same file.....
;12467
;Exit from program....