-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstring_concatenation.asm
More file actions
121 lines (101 loc) · 3.26 KB
/
string_concatenation.asm
File metadata and controls
121 lines (101 loc) · 3.26 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
; =============================================================================
; TITLE: String Concatenation
; DESCRIPTION: Joins (concatenates) two user-provided strings into a single
; output string. Manages string length calculation and memory copy.
; 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
; Input Buffers (Format: Max, Actual, Buffer)
STR_A_BUF DB 50, ?, 50 DUP('$')
STR_B_BUF DB 50, ?, 50 DUP('$')
PROMPT_1 DB 0DH, 0AH, "Enter String 1: $"
PROMPT_2 DB 0DH, 0AH, "Enter String 2: $"
MSG_RES DB 0DH, 0AH, "Concatenated: $"
; The Output Buffer (Large enough to hold both)
FINAL_STR DB 101 DUP('$')
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; --- Step 1: Initialize Data Segment ---
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
; --- Step 2: Get Inputs ---
LEA DX, PROMPT_1
MOV AH, 09H
INT 21H
LEA DX, STR_A_BUF
MOV AH, 0AH
INT 21H
LEA DX, PROMPT_2
MOV AH, 09H
INT 21H
LEA DX, STR_B_BUF
MOV AH, 0AH
INT 21H
; --- Step 3: Concatenation ---
LEA DI, FINAL_STR ; Destination Pointer
; Copy String A
LEA SI, STR_A_BUF + 2 ; Skin Metadata
MOV CL, STR_A_BUF + 1 ; Length
CMP CL, 0
JE COPY_B
XOR CH, CH
L_COPY_A:
MOV AL, [SI]
MOV [DI], AL
INC SI
INC DI
LOOP L_COPY_A
COPY_B:
; Copy String B
LEA SI, STR_B_BUF + 2
MOV CL, STR_B_BUF + 1
CMP CL, 0
JE L_SHOW
XOR CH, CH
L_COPY_B:
MOV AL, [SI]
MOV [DI], AL
INC SI
INC DI
LOOP L_COPY_B
; Force Terminator
MOV BYTE PTR [DI], '$'
L_SHOW:
; --- Step 4: Display Result ---
LEA DX, MSG_RES
MOV AH, 09H
INT 21H
LEA DX, FINAL_STR
MOV AH, 09H
INT 21H
; --- Step 5: Exit ---
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES & ARCHITECTURAL INSIGHTS
; =============================================================================
; 1. MEMORY LAYOUT:
; We use a dedicated 'FINAL_STR' buffer to avoid creating a new dynamic
; memory block (which 8086 DOS doesn't do easily). This is "static allocation".
;
; 2. BUFFER ACCESS:
; DOS Input Buffer starts with [MAX_LEN] [ACTUAL_LEN]. The actual chars
; start at Offset + 2. We must skip the first two bytes to access the text.
;
; 3. POINTER CONTINUITY:
; DI (Destination Index) is NOT reset between copies. It continues from
; where String A left off, ensuring String B is appended immediately after.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =