12A. Create and Delete a file [VTU 4th sem MP lab]


vtu mp 12a

Q) Write a program to create a file (input file) and to delete an existing file.

This program will display a menu using which, you can
1. Create a file.
2. Delete a file. Input for both will be the File name

Algorithm:
Step 1: Get file name to be created.
Step 2: Create a file using DOS interrupt (with Service no: 3CH)
Step 3: If carry flag is not set then file is created and print success message else print error message.
Step 4: Get file name to be deleted.
Step 5: Delete the associated file using DOS interrupt (Service no 41H).
Step 6: If carry flag is not set then file is deleted and print success message else error message.
Step 7: Stop.


CODE:


Explanation: 
  • ASSUME CS:CODE,DS:DATA
ASSUME method is used to state that CS is code segment and DS is data segment.

  • DIS_MSG MACRO P1
  • MOV AH,09H
  • LEA DX,P1
  • INT 21H
  • ENDM
DIS_MSG is a macro to display a string using 09H with INT 21H using LEA. Takes a string location as a parameter.

  • DATA SEGMENT
  • M1 DB 10,13,'1.CREATE',10,13,'2.DELETE$'
  • M2 DB 10,13,'ENTER THE OPTION:$'
  • M3 DB 10,13,'ENTER THE FILE NAME:$'
  • M4 DB 10,13,'FILE CREATED SUCCESSFULLY$'
  • M5 DB 10,13,'FILE DELETED SUCCESSFULLY$'
  • M6 DB 10,13,'ERROR$'
  • M7 DB 10,13,'INVALID OPTION$'
  • STR DB 100 DUP(?)
  • DATA ENDS
Data segment with appropriate messages for the user interface. 10,13 have their usual meanings.
STR is the 100 byte location to store the file name which user will be entering during execution. Initially assigned to nothing/null using ‘DUP(?)

  • CODE SEGMENT
  • START:
  • MOV AX,DATA
  • MOV DS,AX
Code segment starts and data segment is initialized.


  • DIS_MSG M1
  • DIS_MSG M2
  • MOV AH,01H
  • INT 21H
  • CMP AL,'1'
  • JE CREATE_FILE
  • CMP AL,'2'
  • JE DELETE_FILE
  • DIS_MSG M7
  • JMP TERM
Display the menu by displaying two messages ‘M1’ and ‘M2’ at the beginning and then asking for user to select an option and the option is taken using 01H service with INT 21H.
The entered option will be in AL register.
Compare it with option/value ‘1’ (CMP AL,'1') and go to label ‘CREATE_FILE’ if it was equal (JE CREATE_FILE).
Compare AL with option/value ‘2’ (CMP AL,'2') and go to label ‘DELETE_FILE’ if it was equal (JE DELETE_FILE).

If user did not enter any of this option (1 or 2), display the message ‘M7’ and terminate by jumping unconditionally to label TERM.

This is the part to create a file.
  • CREATE_FILE:
  • DIS_MSG M3
  • CALL READ
  • MOV AH,3CH
  • MOV CX,020H
  • LEA DX,STR
  • INT 21H
  • JC STOP
  • DIS_MSG M4
  • JMP TERM
 Display the message asking to Enter the file name (M3) using DIS_MSG macro.
CALL READ, calls the read procedure to read the name of the file.

How to create a file in 8086 microprocessor programming?
  • Move the service number ‘3CH’ to AH.(to create a file)
  • Move the number ‘020H’ to CX register (stating that it is an ordinary file).
  • Load the offset address of location where name of the file to be created is stored, to DX.
On interrupt 21H, with 3CH loaded into AH. A file will be created with file name as the String present in the location whose offset is stored in DX register. To define the type of file, you can change the value of CX before calling the interrupt.

To know whether file is created or not, check the carry flag.
If the carry flag is set, “Error”.
If the carry flag is not set,No error”.

If carry is set, jump to label STOP (JC STOP) where you will be displaying Error message and terminating the program.
Else display the success message (M4) and jump to label TERM to terminate.

This is to delete the existing file.
  • DELETE_FILE:
  • DIS_MSG M3
  • CALL READ
  • MOV AH,41H
  • LEA DX,STR
  • INT 21H
  • JC STOP
  • DIS_MSG M5
  • JMP TERM
Display the message asking to Enter the file name (M3) which is to be deleted,using DIS_MSG macro.
CALL READ, calls the read procedure to read the name of the file.
 
How to delete an existing file in 8086 microprocessor programming?
  • Move the service number ‘41H’ to AH.(to create a file)
  • Load the offset address of location where name of the file to be deleted is stored, to DX.
On interrupt 21H, with 41H loaded into AH. A file name, whose offset is stored in the DX register, will be deleted

Again,
To know whether file is deleted or not, check the carry flag.
If the carry flag is set, “Error”.
If the carry flag is not set, “No error”.

If carry is set, jump to label STOP (JC STOP) where you will be displaying Error message and terminating the program.
Else, display the success message (M5) and jump to label TERM to terminate.

  • STOP:
  • DIS_MSG M6
  • TERM:
  • MOV AH,4CH
  • INT 21H
STOP is a label where you will be displaying error message(M6).

TERM is the termination of the program. Usual way of 4CH and INT 21H.

  • READ PROC
  • MOV AH,01H
  • LEA SI,STR
  • LOOP1:
  • INT 21H
  • CMP AL,0DH
  • JZ NEXT
  • MOV [SI],AL
  • INC SI
  • JMP LOOP1
  • NEXT:
  • RET
  • READ ENDP
READ procedure reads the name of the file (character by character) and stores in STR.
Inputting a character is done using 01H service stored in AH register.
Load the offset of STR into SI (index register).

Call the interrupt 21H to read a character(will be stored in AL). Compare it to ‘Enter’ key and if it isn’t ‘Enter’ key, Save it in to the location pointed to by SI, (MOV [SI],AL).
Increment SI for next character to be placed in the next memory of STR.
Jump to label LOOP1 to read next character.

If entered character is ‘Enter’ key, jump to label NEXT (JZ NEXT) to return back to main program.

ENDP defines the end of READ procedure’s definition.

  • CODE ENDS
  • END START
End of code segment and hence the START label.


Output:
1.CREATE
2.DELETE

ENTER THE OPTION: 2

ENTER THE FILE NAME: CODESHUFFLE

ERROR

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!