You are on page 1of 2

org 100h ; Set the origin of the program to 100h (256 decimal)

.data ; Begin the data segment

Filename db 'file.txt',00h ; Declare a null-terminated string containing the filename of the file
to be read
F dw ? ; Declare a word-sized variable F to hold the file handle returned by the DOS function
call
Buffer db 80h dup(?) ; Declare an 80-byte buffer to hold data read from the file

.code ; Begin the code segment

Program: ; Start of the program

mov ax, @data ; Move the address of the data segment to the AX register
mov ds, ax ; Move the value in AX to the DS (Data Segment) register

mov ah, 3dh ; Move the value 3dh to the AH register, indicating that we want to open a file
mov al, 0 ; Move the value 0 to the AL register, indicating that we want to open the file for
reading
lea dx, Filename ; Load the offset of the filename string into the DX register
int 21h ; Call the DOS interrupt 21h to open the file

mov F, ax ; Move the value returned by the DOS call to the variable F

LP: ; Start of a loop to read the file

mov ah,3fh ; Move the value 3fh to the AH register, indicating that we want to read from the
file
lea dx, Buffer ; Load the offset of the buffer into the DX register
mov cx, 1 ; Move the value 1 to the CX register, indicating that we want to read 1 byte at a
time
mov bx, F ; Move the file handle into the BX register
int 21h ; Call the DOS interrupt 21h to read from the file

cmp ax, cx ; Compare the value returned by the DOS call to the value in CX
jne EOF ; If the two values are not equal, jump to the end of file (EOF)

mov al, Buffer ; Move the byte read from the file into the AL register
call write ; Call the write procedure to output the byte to the console
jmp LP ; Jump back to the beginning of the loop to read the next byte

EOF: ; End of file reached

mov bx, F ; Move the file handle into the BX register


mov ah, 3eh ; Move the value 3eh to the AH register, indicating that we want to close the file
int 21h ; Call the DOS interrupt 21h to close the file
int 20h ; Call the DOS interrupt 20h to terminate the program

write PROC ; Start of the write procedure

mov ah, 2 ; Move the value 2 to the AH register, indicating that we want to output a character
to the console
mov dl, Buffer ; Move the byte to be output to the DL register
int 21h ; Call the DOS interrupt 21h to output the byte to the console

ret ; Return from the procedure

; End of the program

You might also like