Write X86 menu driven ALP to implement OS commands TYPE, COPY and DELETE using file operations. Provided command line arguments in all cases
%macro print 2 ;Macro for printing
mov rax, 1 ;rax = 1 for sys_write
mov rdi, 1 ;unsigned int file descriptor
mov rsi, %1 ;Buffer address
mov rdx, %2 ;Buffer length
syscall ;Makes system call
%endmacro
%macro input 2 ;Macro for reading
mov rax, 0 ;rax = 0 for sys_read
mov rdi, 0 ;unsigned int file descriptor
mov rsi, %1 ;Buffer address
mov rdx, %2 ;Buffer length
syscall ;Makes system call
%endmacro
%macro Open 1 ;Macro for Opening
mov rax, 2 ;'open' syscall
mov rdi, %1 ;file name
mov rsi, 0102o ;Read and write mode, create if not(flag)
mov rdx, 0666o ;permissions set
syscall ;Makes system call
%endmacro
%macro Read 3 ;Macro for reading
mov rax, 0 ;'Read' syscall
mov rdi, %1 ;file pointer
mov rsi, %2 ;Buffer for read
mov rdx, %3 ;len of data want to read
syscall ;Makes system call
%endmacro
%macro Write 3 ;Macro for Writing
mov rax, 01 ;'Write' syscall
mov rdi, %1 ;file pointer
mov rsi, %2 ;Buffer for write
mov rdx, %3 ;len of data want to write
syscall ;Makes system call
%endmacro
%macro Delete 1 ;Macro for Deleting
mov rax, 87
mov rdi, %1 ;file name
syscall ;Makes system call
%endmacro
%macro Close 1 ;Macro for closing
mov rax, 3
mov rdi, %1 ;file name
syscall ;Makes system call
%endmacro
section .data ;For declaring initialized data
mssg db "Write X86 menu driven ALP to implement OS commands
TYPE, COPY and DELETE using file operations.
Provided command line arguments in all cases"
mssg_len:equ $ - mssg
mssg_name db "AMAN THAKUR 3205" ;Initialising mssg_name
mssg_name_len: equ $-mssg_name
mssg_sel db "--> Select an option from the menu : "
mssg_sel_len: equ $ - mssg_sel
mssg_menu db "--> Menu :", 10,
"1. TYPE", 10,
"2. COPY", 10,
"3. DEL", 10,
"4. Exit", 10
mssg_menu_len: equ $ - mssg_menu
;===================Messages printed during input=================================
mssg1 db "Original file content : "
mssg1_len: equ $-mssg1
mssg2 db "Copied file content: "
mssg2_len: equ $-mssg2
;===================Messages printed after completion of procedure=====================
mssg_copy db "Successfully copyed file."
mssg_copy_len: equ $-mssg_copy
mssg_del db "Successfully deleted file."
mssg_del_len: equ $-mssg_del
mssg_error db "Error!!"
mssg_error_len:equ $-mssg_error
mssg0 db "Invalid number of arguments!"
mssg0_len: equ $-mssg0
space db " "
newline db 10
cnt db 0
cnt2 db 0
section .bss ;For declaring variables.
fname1 resb 20 ;Reserving 20 Byte.
fname2 resb 20
file_ptr1 resq 1 ;Reserving quad word for file pointer.
file_ptr2 resq 1 ;Reserving quad word for file pointer.
file_buffer resb 200
buffer_len resb 1 ;Reserving 1 Byte.
choice resb 2
section .text ;Actual code is present.
global _start ;has to be declared for linker(ld).
_start: ;tells linker entry point.
print mssg, mssg_len ;Problem Statement.
print newline, 1
print mssg_name, mssg_name_len ;Name and roll number.
print newline, 1
;===================Fetching command line Argument=====================
mov rcx, 00
pop rcx
cmp rcx, 3
jne err
pop rsi
pop rsi
mov rdi, fname1
loop0: ;Fetching fname1
mov al, byte[rsi]
mov byte[rdi], al
inc rdi
inc rsi
inc byte[cnt]
cmp byte[rsi], 0
je jump_out
jmp loop0
jump_out:
pop rsi
mov rdi, fname2
loop1: ;Fetching fname2
mov al, byte[rsi]
mov byte[rdi], al
inc rdi
inc rsi
inc byte[cnt2]
cmp byte[rsi], 0
je jump_out1
jmp loop1
jump_out1:
mov bl, byte[cnt]
print fname1, rbx ;Printing fname1
print newline, 1
mov bl, byte[cnt2]
print fname2, rbx ;Printing fname2
print newline, 1
menu:
print mssg_menu, mssg_menu_len ;Printing menu
print newline, 1
print mssg_sel, mssg_sel_len ;Choosing option
input choice, 2
case_1:
cmp byte[choice], 31h
jne case_2
call Type ;Calling Type Procedure when choice is 1
jmp menu
case_2:
cmp byte[choice], 32h
jne case_3
call Copy ;Calling Copy Procedure when choice is 2
jmp menu
case_3:
cmp byte[choice], 33h
jne case_4
call Del ;Calling Delete Procedure when choice is 3
jmp menu
case_4:
cmp byte[choice], 34h
je Exit ;Exiting when choice is 1
print mssg0, mssg0_len
print newline, 1
jmp menu
Exit:
mov rax, 60 ;rax = 60 for sys_exit.
mov rdi, 0
syscall
err:
print mssg_error, mssg_error_len ;Error mssg for invalid input.
print newline, 1
print mssg0, mssg0_len
print newline, 1
mov rax, 60
mov rdi, 0
syscall
Type:
Open fname1 ;Opening file fname1
mov [file_ptr1], rax ;Geting file pointer form rax
Read [file_ptr1], file_buffer, 100
mov [buffer_len], rax
print file_buffer, [buffer_len] ;Printing content of file
print newline, 1
Close fname1
ret
Copy:
Open fname1 ;Opening and printing original file's content first
mov [file_ptr1], rax
Read [file_ptr1], file_buffer, 100
mov [buffer_len], rax
print mssg1, mssg1_len ;Printing original file content
print newline, 1
print file_buffer, [buffer_len]
print newline, 1
Open fname2 ;Opening second file to copy from first
mov [file_ptr2], rax
Write [file_ptr2], file_buffer, [buffer_len] ;Writing to new file
Close fname1
Close fname2 ;Closing both file1 and file2
print newline, 1
print mssg_copy, mssg_copy_len ;Successfully copied.
print newline, 1
Open fname2 ;Opening and printing copied file's content first
mov [file_ptr2], rax
Read [file_ptr2], file_buffer, 100
mov [buffer_len], rax
print mssg2, mssg2_len ;Printing Copied file content
print newline, 1
print file_buffer, [buffer_len]
print newline, 1
ret
Del:
Delete fname2
print mssg_del, mssg_del_len
print newline, 1
print newline, 1
ret
0 Comments
If you have any doubt pls let me know..