To perform file operations like open, read, write, close using INT 21H.
Create a file named exp12.asm in Notepad and save it in D:\TASM:
.model small
.stack 100h
.data
filename db "TEST.TXT", 0
handle dw ?
buffer db "Hello, File!$"
.code
main proc
mov ax, @data
mov ds, ax
; Create file
mov ah, 3Ch
xor cx, cx
lea dx, filename
int 21h
mov handle, ax
; Write to file
mov ah, 40h
mov bx, handle
lea dx, buffer
mov cx, 13
int 21h
; Close file
mov ah, 3Eh
mov bx, handle
int 21h
mov ah, 4ch
int 21h
main endp
end main
Inside DOSBox, compile and run:
tasm filename.asm
tlink filename.obj
Run Debug:
td filename.exe
; Open file for reading
mov ah, 3Dh
mov al, 00h ; Read-only mode
lea dx, filename
int 21h
mov handle, ax
; Read from file
mov ah, 3Fh
mov bx, handle
lea dx, buffer
mov cx, 13 ; Number of bytes to read
int 21h
Always check the carry flag after file operations:
int 21h
jc error_handler ; Jump if carry flag is set (error occurred)
; Continue with normal execution
Remember: Always close files after operations to free system resources and ensure data integrity. File handles are limited system resources.