-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwrite_file.asm
More file actions
95 lines (79 loc) · 2.7 KB
/
write_file.asm
File metadata and controls
95 lines (79 loc) · 2.7 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
; =============================================================================
; TITLE: Write to File
; DESCRIPTION: Creates (or overwrites) "OUTPUT.TXT" and writes a defined
; string of text into it.
; 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 "OUTPUT.TXT", 0
FILE_HANDLE DW ?
TEXT_DATA DB "Hello World from 8086 Assembly!", 0DH, 0AH
DATA_LEN EQU $ - TEXT_DATA ; Auto-calculate length
MSG_OK DB 0DH, 0AH, "Data successfully written.$"
MSG_ERR DB 0DH, 0AH, "Error during file operation.$"
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; --- Step 1: Initialize Data Segment ---
MOV AX, @DATA
MOV DS, AX
; --- Step 2: Create/Open File (INT 21h / AH=3Ch) ---
; Using Create Mode to Truncate if exists
MOV CX, 0 ; Attributes
LEA DX, FILE_NAME
MOV AH, 3CH
INT 21H
JC L_ERROR
MOV FILE_HANDLE, AX
; --- Step 3: Write Data (INT 21h / AH=40h) ---
; BX = Handle
; CX = Byte Count
; DS:DX = Data Source
MOV BX, FILE_HANDLE
MOV CX, DATA_LEN
LEA DX, TEXT_DATA
MOV AH, 40H
INT 21H
JC L_ERROR
; Check if disk was full (AX < CX, but no Carry Set)
CMP AX, CX
JNE L_ERROR
; --- Step 4: Close File ---
MOV BX, FILE_HANDLE
MOV AH, 3EH
INT 21H
; --- Step 5: Success Message ---
LEA DX, MSG_OK
MOV AH, 09H
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. WRITING OPERATIONS:
; AH=40h writes from the current file pointer position.
; After creation, the pointer is at 0 (start).
;
; 2. ERROR CHECKING:
; Always check the Carry Flag (CF). Also, verify that the number of bytes
; written (returned in AX) equals the number requested (CX). Mismatches
; indicate a full disk.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =