-
Notifications
You must be signed in to change notification settings - Fork 1
/
15. MATRIX ADDITION.asm
382 lines (300 loc) · 8.51 KB
/
15. MATRIX ADDITION.asm
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
; THIS SIMPLE PROGRAM IS FOR ADDING TWO 2x2 MATRIX
; IF TWO 2x2 MATRIX IS PROVIDED THEN IT CALCULATE THEIR SUR
; FOR EXAMPLE IF
; 2 -1 ; 1 0
; AND
; 1 -2 ; 0 1
; IS GIVEN THEN IT SHOW THE FOLLOWIN RESLUT
; 2 -1 ; 1 0 ; 3 -1
; 1 -2 + ; 0 1 = ; 1 -1
.MODEL SMALLL
.STACK 100H
.DATA
SIZE DW 2
ROW DW 2
COL DW 2
CURRENT_MATRIX DB ?
MATRIX1 DW 2 DUP (?)
DW 2 DUP (?)
MATRIX2 DW 2 DUP (?)
DW 2 DUP (?)
RESULT DW 2 DUP (?)
DW 2 DUP (?)
CLEAR DB 0AH, 0DH, '$'
MSG1 DB ' ENTER FIRST MATRIX: ', 0AH, 0DH, '$'
MSG2 DB 0AH, 0DH, ' ENTER SECOND MATRIX: ', 0AH, 0DH, '$'
MSG3 DB 0AH, 0DH, ' RESULTANT MATRIX IS: ', 0AH, 0DH, '$'
.CODE
OUTDEC PROC
; PROCEDURE FOR DECIMAL OUTPUT
; ALGORITHM:
; IF AX < 0
; THEN
; PRINT A MINUS SIGN
; REPLACE AX BY IT'S 2's COMPLEMENT
; THEN
; GET THE DIGITS IN AX's DECIMAL REPRESENTATION
; CONVERT THE DIGITS TO CHARACTERS AND PRINT
; PRINTS AX AS A SIGNED INTEGER
PUSH AX
PUSH BX
PUSH CX
PUSH DX
; CHECKING FOR SIGN TO PRINT
OR AX, AX
JGE @END_IF1
; IF AX IS LESS THAN ZERO (AX<0)
PUSH AX
MOV DL, '-'
MOV AH, 2
INT 21H
POP AX
; TAKE 2's COMPLEMENT
NEG AX
; JUMP HERE
@END_IF1:
; PRINT IN DECIMAL
; CX COUNTS DIGITS
XOR CX, CX
; SET THE DIVISOR
MOV BX, 10D
@REPEAT:
XOR DX, DX
; NOW DIVIDE AX BY BX
; AX HAS QUOTIENT AND DX HAS REMAINDER
DIV BX
PUSH DX
INC CX
; CHECK THAT IF AX = 0
OR AX, AX
JNE @REPEAT
; CONVERT THE DIGITS TO CHARACTER AND PRINT
MOV AH, 2
@PRINT_LOOP:
POP DX
OR DL, 30H ; CONVERTING TO CHARACTER
INT 21H
LOOP @PRINT_LOOP
POP DX
POP CX
POP BX
POP AX
RET
OUTDEC ENDP
INDEC PROC
; PROCEDURE FOR DECIMAL INPUT
; ALGORITHM:
; TOTAL = 0
; READ AN ASCII DIGIT
; REPEAT
; CONVERT CHARACTER TO IT's BINARY VALUE
; TOTAL = TOTAL*10 + VALUE
; READ A CHARACTER
; UNTIL CHARACTER IS A CARRIAGE RETURN OR WHITE SPACE
@BEGIN:
; SAVE THE REGISTERS
PUSH BX
PUSH CX
PUSH DX
; BX HOLDS TOTAL
XOR BX, BX
; CX HOLDS SIGN
XOR CX, CX
; CHECK THE SIGN
MOV AH, 1
INT 21H
; TRACK THE SIGN
CMP AL, '-'
JE @MINUS
CMP AL, '+'
JE @PLUS
JMP @REPEAT_2
@MINUS:
MOV CX, 1
@PLUS:
; READ A CHARACTER
INT 21H
@REPEAT_2:
; CHECKING IF IT IS >= 0
CMP AL, 20H
JE @EXIT
CMP AL, 0DH
JE @EXIT
CMP AL, '0'
JNGE @NOT_DIGIT
CMP AL, '9'
JNLE @NOT_DIGIT
; CONVERT THE CHARACTER TO DIGIT
AND AX, 000FH
PUSH AX
; CALCULATE THE TOTAL
MOV AX, 10
MUL BX
POP BX
ADD BX, AX
; READ AGAIN
MOV AH, 1
INT 21H
;CMP AL, 0DH
JMP @REPEAT_2
JE @EXIT
NEG AX
@EXIT:
MOV AX, BX
OR CX, CX
JE @EXIT_1
NEG AX
@EXIT_1:
POP DX
POP CX
POP BX
RET
@NOT_DIGIT:
MOV AH, 1
INT 21H
JMP @REPEAT_2
INDEC ENDP
OUTPUTMATRIX PROC
; STORE REGISTERS
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV CX, ROW
MOV SI, 0
@ROW:
MOV BX, 0
@COL:
CMP BX, 4
JE END_PRINTING_OUTPUT
MOV AX, RESULT[SI+BX]
CALL OUTDEC
MOV DL, ' '
MOV AH, 2
INT 21H
ADD BX, 2
JMP @COL
END_PRINTING_OUTPUT:
ADD SI, 4
; PRINT A NEW LINE HERE
PUSH AX
PUSH BX
PUSH CX
PUSH DX
LEA DX, CLEAR
MOV AH, 9
INT 21H
POP DX
POP CX
POP BX
POP AX
LOOP @ROW
; RESTORE REGISTERS
POP DX
POP CX
POP BX
POP AX
RET
OUTPUTMATRIX ENDP
INPUTMATRIX PROC
; STORE REGISTERS
PUSH AX
PUSH BX
PUSH CX
PUSH DX
; CHECK HOW MANY TIMES INPUT TO TAKE
MOV CX, ROW
MOV SI, 0
@@ROW:
MOV BX, 0
@@COL:
MOV AX, 4
CMP BX, AX
JE END_TAKING_INPUT
CALL INDEC
CMP CURRENT_MATRIX, 2
JE @MATRIX2
MOV MATRIX1[SI+BX], AX
JMP @NEXT
@MATRIX2:
MOV MATRIX2[SI+BX], AX
@NEXT:
ADD BX, 2
JMP @@COL
END_TAKING_INPUT:
ADD SI, 4
; PRINT A NEW LINE HERE
PUSH AX
PUSH BX
PUSH CX
PUSH DX
LEA DX, CLEAR
MOV AH, 9
INT 21H
POP DX
POP CX
POP BX
POP AX
LOOP @@ROW
POP DX
POP CX
POP BX
POP AX
; RESTORE REGISTERS FINISHED
RET
INPUTMATRIX ENDP
MATRIXADDITION PROC
; STORE REGISTERS
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV CX, ROW
MOV SI, 0
@ROW1:
MOV BX, 0
@COL1:
MOV AX, COL
IMUL SIZE
CMP BX, AX
JE END_COL
MOV AX, MATRIX1[SI+BX]
ADD AX, MATRIX2[SI+BX]
MOV RESULT[SI+BX], AX
ADD BX, 2
JMP @COL1
END_COL:
ADD SI, 4
LOOP @ROW1
; RESTORE REGISTERS
POP DX
POP CX
POP BX
POP AX
RET
MATRIXADDITION ENDP
MAIN PROC
; INITIALIZE DS
MOV AX, @DATA
MOV DS, AX
LEA DX, MSG1
MOV AH, 9
INT 21H
MOV CURRENT_MATRIX, 1
CALL INPUTMATRIX
LEA DX, MSG2
MOV AH, 9
INT 21H
MOV CURRENT_MATRIX, 2
CALL INPUTMATRIX
; CALL MATRIX ADDITION IN THIS POSITION
CALL MATRIXADDITION
LEA DX, MSG3
MOV AH, 9
INT 21H
CALL OUTPUTMATRIX
; DOS EXIT
MOV AX, 4CH
INT 21H
MAIN ENDP
END MAIN