-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmatrix_addition.asm
More file actions
86 lines (73 loc) · 3 KB
/
matrix_addition.asm
File metadata and controls
86 lines (73 loc) · 3 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
; =============================================================================
; TITLE: Matrix Addition (3x3)
; DESCRIPTION: Demonstrate element-wise addition of two 3x3 matrices
; stored in row-major order.
; 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
; Matrix A (3x3) - Row Major Storage
MATRIX_A DB 1, 2, 3
DB 4, 5, 6
DB 7, 8, 9
; Matrix B (3x3)
MATRIX_B DB 9, 8, 7
DB 6, 5, 4
DB 3, 2, 1
; Matrix C (Resultant)
MATRIX_C DB 9 DUP(0)
DIM EQU 3 ; Dimension (N)
MSG DB 'Matrix Addition completed. Result stored in memory.$'
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; Initialize Data Segment
MOV AX, @DATA
MOV DS, AX
; Pointers for the three matrices
LEA SI, MATRIX_A ; Source Index 1
LEA DI, MATRIX_B ; Destination Index (used as Source 2)
LEA BX, MATRIX_C ; Base Register (Result pointer)
; Total elements in a 3x3 matrix = 9
MOV CX, DIM * DIM
; -------------------------------------------------------------------------
; ELEMENT-WISE ADDITION LOOP
; Logic: C[i] = A[i] + B[i]
; Since matrices are flat arrays in memory, a single loop handles all indices.
; -------------------------------------------------------------------------
ADD_LOOP:
MOV AL, [SI] ; Fetch element from A
ADD AL, [DI] ; Add element from B
MOV [BX], AL ; Store sum in C
INC SI ; Point to next element in A
INC DI ; Point to next element in B
INC BX ; Point to next element in C
LOOP ADD_LOOP ; Repeat 9 times
; Display status
LEA DX, MSG
MOV AH, 09H
INT 21H
; Exit
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES
; =============================================================================
; 1. MATRIX STORAGE:
; - Row-Major Order: Matrix is stored row by row (A[0][0], A[0][1]...).
; - Adding two matrices of the same size is essentially a linear vector addition.
; - Expected Result (all 10s):
; 10, 10, 10
; 10, 10, 10
; 10, 10, 10
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =