MASM provides several functions via DOS interrupts (primarily int 21h) for performing file operations such as opening, reading, writing, and closing files. These operations are handled via system calls that allow you to interact with files on disk. Below are examples of file operations in MASM, including file opening, reading, writing, and closing using procedures.
To open a file in MASM, you use the DOS interrupt int 21h with function 3Dh, which opens a file and returns a file handle.
; open_file.asm
.model small
.stack 100h
.data
filename db 'testfile.txt', 0 ; Filename to open
handle dw 0 ; File handle
filemode db 'r', 0 ; File access mode ('r' for read, 'w' for write)
errorMessage db 'Error opening file.$', 0
.code
main:
mov ax, @data ; Initialize data segment
mov ds, ax
; Call OpenFile procedure
call OpenFile
; Exit program
mov ah, 4Ch
int 21h
; Procedure to open a file
OpenFile proc
; Open file using DOS function 3Dh (open)
lea dx, filename ; Load address of filename
mov al, 0 ; '0' = read mode, 'w' = write mode, 'a' = append mode
int 21h ; DOS interrupt 21h, function 3Dh (open)
jc ErrorOpeningFile ; Jump if there was an error (carry flag set)
; Store the returned file handle
mov [handle], ax
; File opened successfully, return
ret
ErrorOpeningFile:
; Print error message if file could not be opened
mov ah, 09h
lea dx, errorMessage
int 21h ; DOS interrupt 21h to print string
ret
OpenFile endp
end main
int 21h, function 3Dh: Used to open the file. The filename is passed in DX, and the mode is passed in AL.int 21h call is stored in AX.ErrorOpeningFile.Writing to a file in MASM involves using DOS interrupt int 21h with function 40h. This function requires the file handle, a pointer to the data, and the number of bytes to write.
; write_to_file.asm
.model small
.stack 100h
.data
filename db 'testfile.txt', 0 ; File name
textToWrite db 'Hello, MASM! Writing to a file.$'
handle dw 0 ; File handle
errorMessage db 'Error writing to file.$', 0
.code
main:
mov ax, @data ; Initialize data segment
mov ds, ax
; Open the file for writing
call OpenFile
; Write to the file
call WriteToFile
; Close the file
call CloseFile
; Exit program
mov ah, 4Ch
int 21h
; Procedure to open a file
OpenFile proc
lea dx, filename ; Load address of filename
mov al, 'w' ; 'w' = write mode
int 21h ; DOS interrupt 21h, function 3Dh (open)
jc ErrorOpeningFile ; Jump if error
mov [handle], ax ; Store the returned file handle
ret
ErrorOpeningFile:
mov ah, 09h
lea dx, errorMessage
int 21h ; Print error message
ret
OpenFile endp
; Procedure to write to a file
WriteToFile proc
lea dx, textToWrite ; Load address of text to write
mov ah, 40h ; DOS function 40h (write)
mov bx, [handle] ; Load the file handle into BX
mov cx, 18 ; Number of bytes to write
int 21h ; Call DOS interrupt
jc ErrorWritingFile ; Jump if there was an error
ret
ErrorWritingFile:
mov ah, 09h
lea dx, errorMessage
int 21h ; Print error message
ret
WriteToFile endp
; Procedure to close the file
CloseFile proc
mov ah, 3Eh ; DOS function 3Eh (close file)
mov bx, [handle] ; Load file handle into BX
int 21h ; Call DOS interrupt
ret
CloseFile endp
end main
3Dh function (open file) with 'w' mode.int 21h with function 40h. The file handle is passed through BX, the data to write is in DX, and the number of bytes to write is passed through CX.int 21h with function 3Eh.To read from a file in MASM, you use DOS interrupt int 21h with function 3Fh. This function allows you to read data from a file into a buffer.
; read_from_file.asm
.model small
.stack 100h
.data
filename db 'testfile.txt', 0 ; File name to read from
buffer db 100, 0 ; Buffer to store the data read
handle dw 0 ; File handle
errorMessage db 'Error reading file.$', 0
bytesRead dw 0 ; Variable to store number of bytes read
.code
main:
mov ax, @data ; Initialize data segment
mov ds, ax
; Open the file
call OpenFile
; Read from the file
call ReadFromFile
; Print the data read (optional)
call PrintBuffer
; Close the file
call CloseFile
; Exit program
mov ah, 4Ch
int 21h
; Procedure to open a file
OpenFile proc
lea dx, filename ; Load address of filename
mov al, 'r' ; 'r' = read mode
int 21h ; DOS interrupt 21h, function 3Dh (open)
jc ErrorOpeningFile ; Jump if error
mov [handle], ax ; Store the returned file handle
ret
ErrorOpeningFile:
mov ah, 09h
lea dx, errorMessage
int 21h ; Print error message
ret
OpenFile endp
; Procedure to read from a file
ReadFromFile proc
lea dx, buffer ; Load address of the buffer
mov ah, 3Fh ; DOS function 3Fh (read)
mov bx, [handle] ; Load the file handle into BX
mov cx, 100 ; Number of bytes to read
int 21h ; Call DOS interrupt
mov [bytesRead], ax ; Store the number of bytes read
jc ErrorReadingFile ; Jump if there was an error
ret
ErrorReadingFile:
mov ah, 09h
lea dx, errorMessage
int 21h ; Print error message
ret
ReadFromFile endp
; Procedure to print the buffer (the data read)
PrintBuffer proc
mov ah, 09h
lea dx, buffer
int 21h ; Call DOS interrupt to print string
ret
PrintBuffer endp
; Procedure to close the file
CloseFile proc
mov ah, 3Eh ; DOS function 3Eh (close file)
mov bx, [handle] ; Load file handle into BX
int 21h ; Call DOS interrupt
ret
CloseFile endp
end main
'r').buffer using the int 21h function 3Fh. The number of bytes read is stored in the bytesRead variable.int 21h, function 09h.int 21h, function 3Eh.In MASM, file operations can be done using DOS interrupts. Here’s a summary of the key functions for file manipulation:
int 21h, function 3Dh
AL): `'Open this section to load past papers