Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

;Assignment no.

:7
;Assignment Name :X86/64 Assembly language program (ALP)
;To sort the list of integers in ascending/descending order using
bubble sort.
;Read the input from the text file and write the sorted data back
to the same text file.
;----------------------------------------------------------------
--------

%include "macro.nasm"
;----------------------------------------------------------------
--------
section .data
nline db 10
nline_len equ $-nline

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

ermsg db 10, "ERROR in writing File...",10


ermsg_len equ $-ermsg

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 resd 1
abuf_len resd 1 ; actual buffer length

array resb 10
n resd 1
;----------------------------------------------------------------
----------
section .text

1
global _start

_start:

print filemsg,filemsg_len
read filename,20
dec eax
mov byte[filename+eax],0 ; blank char/null
char

fopen filename ; on succes returns


handle
cmp eax,-1H ; on failure returns
-1
je Error
mov [filehandle],eax

fread [filehandle],buf, buf_len


dec eax ; EOF
mov [abuf_len],eax

call bsort
jmp Exit

Error: print errmsg, errmsg_len

Exit: print exitmsg,exitmsg_len


exit
;----------------------------------------------------------------
----------------
bsort: ; Bubble sort procedure
call buf_array

xor eax,eax
mov ebp,[n]
dec ebp

xor ecx,ecx
xor edx,edx
xor esi,esi
xor edi,edi

mov ecx,0 ; i=0

oloop: mov ebx,0 ; j=0

mov esi,array ; a[j]

iloop: mov edi,esi ; a[j+1]


inc edi

2
mov al,[esi]
cmp al,[edi]
jbe next

mov dl,0
mov dl,[edi] ; swap
mov [edi],al
mov [esi],dl

next: inc esi


inc ebx ; j++
cmp ebx,ebp
jb iloop

inc ecx
cmp ecx,ebp
jb oloop

fwrite [filehandle],omsg, omsg_len


fwrite [filehandle],array,[n]

fclose [filehandle]

print omsg, omsg_len


print array,[n]

ret

Error1:
print ermsg, ermsg_len
RET
;----------------------------------------------------------------
--
buf_array:

xor ecx,ecx
xor esi,esi
xor edi,edi

mov ecx,[abuf_len]
mov esi,buf
mov edi,array

next_num:
mov al,[esi]
mov [edi],al

add esi,04 ; number


;inc esi ; newline
add edi,04

3
inc byte[n] ; counter

dec ecx ; number


;dec ecx ; newline
jnz next_num
ret
;----------------------------------------------------------------
--

macro.nasm

;macro.asm
;macros as per 64 bit conventions

%macro read 2
mov eax,03 ;read
mov ebx,0 ;stdin/keyboard
mov ecx,%1 ;buf
mov edx,%2 ;buf_len
int 80h
%endmacro

%macro print 2
mov eax,04 ;print
mov ebx,01 ;stdout/screen
mov ecx,%1 ;msg
mov edx,%2 ;msg_len
int 80h
%endmacro

%macro fopen 1
mov eax,05 ;open
mov ebx,%1 ;filename
mov ecx,2 ;mode RW
mov edx,0777o ;File permissions
int 80H
%endmacro

%macro fread 3
mov eax,0 ;read
mov ebx,%1 ;filehandle
mov ecx,%2 ;buf

4
mov edx,%3 ;buf_len
int 80h
%endmacro

%macro fwrite 3
mov eax,1 ;write/print
mov ebx,%1 ;filehandle
mov ecx,%2 ;buf
mov edx,%3 ;buf_len
int 80h
%endmacro

%macro fclose 1
mov eax,06 ;close
mov ebx,%1 ;file handle
int 80h
%endmacro

%macro exit 0
print nline,nline_len
mov eax,01
mov ebx,00
int 80h

%endmacro

You might also like