0% found this document useful (0 votes)
13 views4 pages

Exp 7

Uploaded by

Sumit Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Exp 7

Uploaded by

Sumit Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Sumit Raghunath Rathod

UID: 2023800094

Experiment No. 7

AIM: Program for File Handling using BIOS interrupts in Assembly Language.

CODE: .model small


.stack 100h
.DATA
MESS1 DB 0AH,0DH,07H,'PLEASE ENTER FILE NAME $'
MESS2 DB 0AH,0DH,07H,'SORRY FILE OPENING ERROR $'
MESS3 DB 0AH,0DH,07H,'S0RRY FILE READING ERROR$'
MESS4 DB 0AH,0DH,07H,'SORRY FILE CLOSING ERROR$'

STRING DB 30 DUP(?)
HANDLE DW ?
CHAR DB ?

DISPLAY MACRO DISP


LEA DX,DISP
MOV AH,09H
INT 21H
ENDM
.CODE
;-----------------
;READ FILE NAME
;-----------------
MOV AX,@data
MOV DS,AX

DISPLAY MESS1
MOV AH,0AH ;PLACE KEYBOARD CHARACTER INTO INPUT BUFFER
MOV DX,OFFSET STRING ;START OF THE INPUT BUFFER
MOV STRING,27 ;MAXIMUM LENGTH OF THE STRING
INT 21H
MOV BX,DX ;LENGTH OF THE STRING ACTUALLY ENTERED
MOV AL,STRING+1 ;
SUB AH,AH ;CLEAR AH
ADD BX,AX
MOV BYTE PTR [BX+2],0 ;LAST CHARACTER OF THE STRING 00H

; New Line
MOV AH,02H
MOV DL,0DH
INT 21H
MOV DL,0AH
INT 21H
;-----------------------
;OPEN THE FILE
;-----------------------
MOV AL,0 ;MODE OF ACCESS
MOV AH,3DH ;OPEN A FILE
MOV DX,OFFSET STRING+2 ;IDENTIFY THE FILE
INT 21H
INC
JC ERROR1
MOV HANDLE,AX ;UNLESS ERROR OCCURS AX CONTAINS THE FILE
HANDLE
;---------------------------
;READ THE FILE CHAR BY CHAR
;---------------------------
LOOP1: MOV CX,1 ;THE NUMBER OF BYTES TO READ
MOV BX,HANDLE ;FILE HANDLE RETURNED BY OPEN FILE
MOV AH,3FH ;READ FROM A FILE
MOV DX,OFFSET CHAR ;BYTE OF MEMORY TO PLACE THE INFORMATION
INT 21H
JC ERROR2
CMP AX,0 ;ACTUAL NUMBER OF BYTES TO READ 00H FOR E.O.F.
JE CLOSE

;-------------------------
; DISPLAY THE CHAR ON SCREEN
;-------------------------
MOV DL,CHAR ;ASCII CODE FOR THE CHARACTER TO BE DISPLAYED
MOV AH,02H ;DISPLAY THE CHARACTER AT THE O/P DEVICE
INT 21H
JMP LOOP1
;------------------
; CLOSING THE FILE
;-----------------------
CLOSE: MOV BX,HANDLE
MOV AH,3EH ;CLOSE A FILE HANDLE(UPDATE FILE DIRECTORY, FLUSHES
INT 21H ;ALL INTERNAL BUFFERS ASSOCIATED WITH THE FILE)
JC ERROR3
JMP QUIT
;----------------------
;ERROR MESSAGE
;-----------------------
ERROR1: DISPLAY MESS2
JMP QUIT
ERROR2: DISPLAY MESS3
JMP QUIT
ERROR3: DISPLAY MESS4
QUIT:
MOV AH,4CH
INT 21H

END

EXPLANATION: This program demonstrates file handling in assembly language by using


BIOS interrupts to open, read, and close a file. The program starts by
prompting the user to enter a file name, which is stored in an input buffer.
The filename input is processed to ensure it ends with a null terminator for
proper handling.
To open the file, the program sets the mode to read-only and uses
interrupt 21H with function 3DH. If the file opens successfully, the file
handle is stored in the HANDLE variable. If an error occurs, an appropriate
error message is displayed.
Once the file is opened, the program enters a loop to read the file one
character at a time using interrupt 21H with function 3FH. Each character
read is displayed on the screen. The loop continues until the end of the
file.
Finally, the program closes the file using interrupt 21H with function 3EH.
If there are any errors in reading or closing the file, corresponding error
messages are shown.

RESULT:

CONCLUSION: This experiment helped me understand file handling in assembly language


using BIOS interrupts. I learned how to open, read, and close files by using
specific interrupt functions, as well as how to handle errors in file
operations.

You might also like