Professional Documents
Culture Documents
LP9
LP9
: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
;----------------------------------------------------------------
-----------
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
call bsort
jmp Exit
xor eax,eax
mov ebp,[n]
dec ebp
xor ecx,ecx
xor edx,edx
xor esi,esi
xor edi,edi
2
mov al,[esi]
cmp al,[edi]
jbe next
mov dl,0
mov dl,[edi] ; swap
mov [edi],al
mov [esi],dl
inc ecx
cmp ecx,ebp
jb oloop
fclose [filehandle]
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
3
inc byte[n] ; counter
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