-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathread_file.asm
More file actions
101 lines (83 loc) · 3.05 KB
/
read_file.asm
File metadata and controls
101 lines (83 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
; =============================================================================
; TITLE: Read File Content
; DESCRIPTION: Opens an existing text file ("INPUT.TXT") and reads its content
; into a buffer, then displays it to standard output.
; AUTHOR: Amey Thakur (https://github.com/Amey-Thakur)
; REPOSITORY: https://github.com/Amey-Thakur/8086-ASSEMBLY-LANGUAGE-PROGRAMS
; LICENSE: MIT License
; =============================================================================
.MODEL SMALL
.STACK 100H
; -----------------------------------------------------------------------------
; DATA SEGMENT
; -----------------------------------------------------------------------------
.DATA
FILE_NAME DB "INPUT.TXT", 0 ; Target file
FILE_HANDLE DW ?
BUFFER DB 512 DUP('$') ; Read buffer (initialized with Terminator)
MSG_HEADER DB 0DH, 0AH, "--- File Contents ---", 0DH, 0AH, "$"
MSG_ERR DB 0DH, 0AH, "Error: Could not read file.$"
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; --- Step 1: Initialize Data Segment ---
MOV AX, @DATA
MOV DS, AX
; --- Step 2: Open File (INT 21h / AH=3Dh) ---
; AL = Access Mode (0=Read, 1=Write, 2=Read/Write)
MOV AL, 0
LEA DX, FILE_NAME
MOV AH, 3DH
INT 21H
JC L_ERROR
MOV FILE_HANDLE, AX ; Save Handle
; --- Step 3: Read File (INT 21h / AH=3Fh) ---
; BX = Handle
; CX = Byte Count to Read
; DS:DX = Buffer
MOV BX, FILE_HANDLE
MOV CX, 510 ; Read up to 510 bytes (safe limit)
LEA DX, BUFFER
MOV AH, 3FH
INT 21H
JC L_ERROR
; Note: AX now holds the actual number of bytes read.
; Since we initialized BUFFER with '$', we can print directly if it's text.
; --- Step 4: Display Content ---
LEA DX, MSG_HEADER
MOV AH, 09H
INT 21H
LEA DX, BUFFER
MOV AH, 09H
INT 21H
; --- Step 5: Close File (INT 21h / AH=3Eh) ---
MOV BX, FILE_HANDLE
MOV AH, 3EH
INT 21H
JMP L_EXIT
L_ERROR:
LEA DX, MSG_ERR
MOV AH, 09H
INT 21H
L_EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES & ARCHITECTURAL INSIGHTS
; =============================================================================
; 1. BUFFER MANAGEMENT:
; We read a chunk of data into memory. If the file is larger than the buffer,
; we would need a loop to read -> print -> read until AX (bytes read) is 0.
;
; 2. FILE POINTER:
; DOS maintains a read/write pointer for each handle. It advances automatically
; after each read operation.
;
; 3. DISPLAY LIMITATION:
; Using AH=09h (Print String) requires the data to be '$' terminated.
; Binary files would need a different display method (e.g., Hex Dump).
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =