;macro.asm
%macro read 2
mov
rax,0
;read
mov
rdi,0
;stdin/keyboard
mov
rsi,%1
;buf
mov
rdx,%2
;buf_len
syscall
%endmacro
%macro print 2
mov
rax,1
;print
mov
rdi,1
;stdout/screen
mov
rsi,%1
;msg
mov
rdx,%2
;msg_len
syscall
%endmacro
%macro fopen 1
mov
rax,2
;open
mov
rdi,%1
;filename
mov
rsi,2
;mode RW
mov
rdx,0777o
;File permissions
syscall
%endmacro
%macro fread 3
mov
rax,0
;read
mov
rdi,%1
;filehandle
mov
rsi,%2
;buf
mov
rdx,%3
;buf_len
syscall
%endmacro
%macro fwrite 3
mov
rax,1
;write/print
mov
rdi,%1
;filehandle
mov
rsi,%2
;buf
mov
rdx,%3
;buf_len
syscall
%endmacro
%macro fclose 1
mov
rax,3
;close
mov
rdi,%1
;file handle
syscall
%endmacro
%macro exit 0
print
nline,nline_len
mov
rax,60
;exit
mov
rdi,0
syscall
%endmacro
;file.asm
%include
"macro.asm"
Section .data
title: db 0ah,"----Commands -----", 0ah
db "1. Copy ",0ah
db "2. Type ",0ah
db "3. Delete ",0ah
db "4. Exit ",0ah
db "Enter Your choice",0ah
title_len: equ $-title
openmsg: db "File Opened Successfully",0ah,0dh
openmsg_len: equ $-openmsg
closemsg: db "File Closed Successfully",0ah,0dh
closemsg_len: equ $-closemsg
errormsg: db "Failed to open file", 0ah,0dh
errormsg_len: equ $-errormsg
delmsg: db "Deleted File", 0ah,0dh
delmsg_len: equ $-delmsg
typemsg: db "=-----File Contents ----=",0ah,0dh
typemsg_len: equ $-typemsg
f1name: db 'file1.txt',0
f2name: db 'file2.txt',0
f3name: db 'file3.txt',0
Section .bss
buffer: resb 200
bufferlen:resb 8
cnt1:resb 8
;fdis:resb 8
fhandle: resq 1
choice: resb 2
Section .text
global main
main:
write title,title_len
read choice,2
;------------- CHOOSE OPTION --------------------------
;compare choice here
cmp byte[choice],'1' ;if choice is to display content
je COPY
cmp byte[choice],'2'
je TYPE
cmp byte[choice],'3'
je DELETE
cmp byte[choice],'4'
je EXIT
COPY:
fopen f1name ;Opening file
cmp rax, -1h
je next
mov [fhandle],rax
;mov qword[fdis],rax
;RAX contains file descriptor value
;bt rax,63
;63rd bit is +ve(0) if file is successfull opened else it is -ve (1)
;jc next
write openmsg,openmsg_len
jmp next1
next:
write errormsg,errormsg_len
jmp EXIT
next1:
fread [fhandle],buffer,200
;reading contents of file in buffer
;rax contains actual number of bytes read
mov qword[bufferlen],rax
mov qword[cnt1],rax
;Closing file1
fclose f1name
write closemsg,closemsg_len
;-------------------FILE 2 ------------------
fopen f2name
cmp rax, -1h
je next3
mov [fhandle],rax
;mov qword[fhandle],rax
;RAX contains file descriptor value
;bt rax,63
;63rd bit is +ve(0) if file is successfull opened else it is -ve (1)
;jc next3
write openmsg,openmsg_len
jmp next21
next3:
write errormsg,errormsg_len
jmp EXIT
next21:
fwrite qword[fhandle],buffer,qword[bufferlen] ;writing to file2.txt
fclose f2name
write closemsg,closemsg_len
jmp main
;-----------------------------------------------------------------------
TYPE:
fopen f2name
;Opening file
cmp rax, -1h
je tnext
mov [fhandle],rax
;mov qword[fdis],rax ;RAX contains file descriptor value
;bt rax,63
;63rd bit is +ve(0) if file is successfull opened else it is -ve (1)
;jc tnext
write openmsg,openmsg_len
jmp tnext1
tnext:
write errormsg,errormsg_len
jmp EXIT
tnext1:
fread [fhandle],buffer,200
;reading contents of file in buffer
mov qword[bufferlen],rax
write typemsg,typemsg_len
write buffer,qword[bufferlen]
;Closing file2
fclose f2name
write closemsg,closemsg_len
JMP main
;----------------------------------------------------------
DELETE:
mov rax,87 ;System CALL FOR UNLINK
mov rdi,f3name
syscall
write delmsg,delmsg_len
jmp main
EXIT:
mov rax,3ch
mov rdi,00
syscall