Header Ads

ASM Program to implement DOS Commans(COPY,TYPE,DELETE)

Problem Statement:

Write X86 menus driven Assembly Language Program (ALP) to implement OS (DOS) commands TYPE, COPY and DELETE using file operations. User is supposed to provide command line arguments in all cases.

Code:



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
%macro scall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro

Section .data
title: db 0x0A,"----Commands -----", 0x0A
 db "1. Copy ",0x0A
 db "2. Type ",0x0A
 db "3. Delete ",0x0A
 db "4. Exit ",0x0A
 db "Enter Your choice", 0x0A
title_len: equ $-title
openmsg: db "File Opened Successfully",0x0A
openmsg_len: equ $-openmsg
closemsg: db "File Closed Successfully",0x0A
closemsg_len: equ $-closemsg
errormsg: db "Failed to open file", 0x0A
errormsg_len: equ $-errormsg
delmsg: db "Deleted File", 0x0A
delmsg_len: equ $-delmsg
typemsg: db "=-----File Contents ----=",0x0A
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
choice: resb 2

Section .text
global main
main:
scall 1,1,title,title_len
scall 0,0,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:
 scall 2,f1name,2,777  ;Opening file

 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

 scall 1,1,openmsg,openmsg_len
 jmp next1

next:
 scall 1,1,errormsg,errormsg_len
 jmp EXIT

next1:
 scall 0,[fdis],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
 mov rax,3
 mov rdi,f1name
 syscall
 scall 1,1,closemsg,closemsg_len




;-------------------FILE 2 ------------------
 scall 2,f2name,2,777
 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 next3

 scall 1,1,openmsg,openmsg_len
 jmp next21

next3:
 scall 1,1,errormsg,errormsg_len
 jmp EXIT

next21:
 scall 1,qword[fdis],buffer,qword[bufferlen]  ;writing to file2.txt

 mov rax,3
 mov rdi,f2name
 syscall
 scall 1,1,closemsg,closemsg_len
 jmp main

TYPE:
 scall 2,f2name,2,777  ;Opening file
 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

 scall 1,1,openmsg,openmsg_len
 jmp tnext1

tnext:
 scall 1,1,errormsg,errormsg_len
 jmp EXIT

tnext1:
 scall 0,[fdis],buffer,200 ;reading contents of file in buffer
 mov qword[bufferlen],rax
 scall 1,1, typemsg,typemsg_len
 scall 1,1, buffer,qword[bufferlen]

 ;Closing file2
 mov rax,3
 mov rdi,f2name
 syscall
 scall 1,1,closemsg,closemsg_len
JMP main

DELETE:
 mov rax,87 ;System CALL FOR ULINK
 mov rdi,f3name
 syscall
 scall 1,1, delmsg,delmsg_len
jmp main

EXIT:
 mov rax,60
 mov rdi,0
 syscall

Output:

1 comment:

  1. please provide execution step for the program type, copy delete commands

    ReplyDelete

Powered by Blogger.