forked from jht3QAQ/MatrixMultiplication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.asm
430 lines (368 loc) · 9.29 KB
/
Main.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
.386
.model flat, stdcall
.stack 4096
option casemap:none
include windows.inc
include msvcrt.inc
includelib msvcrt.lib
.const
s_d db "%d ",0
s_s db "%s",0
s_r db "r",0
s_w db "w",0
s_blank db " ",0
s_arg_error db "args number error",10,0
s_get_args_error db "get args error",10,0
s_reading_input_file_s db "reading input file:%s",10,0
s_open_file_error db "open file error",10,0
s_matrix_format_error db "matrix format error",10,0
s_this_is_a_d_d_matrix db "this is a %d * %d matrix",10,0
s_matrix_cant_be_multiplied db "matrix can't be multiplied",10,0
s_the_answer_is db "the answer is:",10,0
s_saving_to_the_file_s db "saving to the file %s",10,0
s_saved db "saved",10,0
fileData struct
len LONG 0
pData PCHAR 0
fileData ends
pFileData typedef ptr fileData
matrixData struct
X DWORD 0
Y DWORD 0
lpData PVOID 0
matrixData ends
pMatrixData typedef ptr matrixData
main proto
readFile proto _lpszFileName:LPSTR
readMatrix proto _lpStFileData:pFileData
mulMatrix proto _lpSMatrixDataA:pMatrixData,_lpSMatrixDataB:pMatrixData
printMatrix proto _lpSMatrixData:pMatrixData
saveMatrix proto _lpSMatrixData:pMatrixData,_lpszFileName:LPSTR
.code
main proc uses esi
local @argc:PINT,@argv:PVOID,@env:PVOID,@dwNewMode:DWORD
local @matrixFileA:pFileData,@matrixFileB:pFileData
local @lpStMatrixDataA:pMatrixData,@lpStMatrixDataB:pMatrixData,@lpStAns:pMatrixData
invoke crt___getmainargs,addr @argc,addr @argv,addr @env,0,addr @dwNewMode
.if eax != 0
invoke crt_puts,addr s_get_args_error
invoke crt_exit, -1
.endif
.if @argc != 4
invoke crt_puts,addr s_arg_error
invoke crt_exit, -1
.endif
mov esi,@argv
invoke readFile,[esi]+4
mov @matrixFileA,eax
invoke readMatrix,@matrixFileA
mov @lpStMatrixDataA,eax
invoke readFile,[esi]+8
mov @matrixFileB,eax
invoke readMatrix,@matrixFileB
mov @lpStMatrixDataB,eax
invoke mulMatrix,@lpStMatrixDataA,@lpStMatrixDataB
mov @lpStAns,eax
invoke crt_printf,addr s_the_answer_is
invoke printMatrix,@lpStAns
invoke saveMatrix,@lpStAns,[esi]+12
invoke crt_exit, 0
main endp
;子程序名: readFile
;功能: 读取文件并返回文件数据
;入口参数: lpszFileName=文件名称字符串指针
;出口参数: eax=fileData结构体指针
readFile proc uses esi,_lpszFileName:LPSTR
local @pStInputFile:PVOID,@lpStFileData:pFileData ;文件指针FILE* pStInputFile,文件数据fileData* pStFileData
invoke crt_malloc,sizeof fileData ;为fileData分配内存
mov @lpStFileData,eax
invoke crt_printf,addr s_reading_input_file_s,_lpszFileName ;pStInputFile=fopen(lpszFileName)
invoke crt_fopen,_lpszFileName,addr s_r
mov @pStInputFile,eax
.if @pStInputFile == 0
invoke crt_puts,addr s_open_file_error
invoke crt_exit, -1
.endif
mov esi,@lpStFileData ;获取文件大小
assume esi:pFileData
invoke crt_fseek,@pStInputFile,0,SEEK_END
invoke crt_ftell,@pStInputFile
mov [esi].len,eax
invoke crt_fseek,@pStInputFile,0,SEEK_SET
invoke crt_calloc,1,[esi].len ;为文件数据分配空间
mov [esi].pData,eax
invoke crt_fread,[esi].pData,sizeof CHAR,[esi].len,@pStInputFile;读取文件数据
invoke crt_puts,[esi].pData
invoke crt_fclose,@pStInputFile ;fclose(lpszFileName)
mov eax,@lpStFileData
ret
readFile endp
;子程序名: readMatrix
;功能: 根据读取到的文件信息转化成数组信息
;入口参数: pStFileData=fileData结构体指针
;出口参数: eax=matrixData结构体指针
readMatrix proc uses esi edi ecx,_lpStFileData:pFileData
local @matrixX:DWORD,@matrixY:DWORD
local @lpArrY:DWORD,@lpMatrix:DWORD,@matrixSize:DWORD
mov @matrixX,0
mov @matrixY,0
mov esi,_lpStFileData ;获取行数
assume esi:pFileData
mov esi,[esi].pData
mov ecx,0
.repeat
LODSB
.if al == 10
inc ecx
.endif
.until al == 0
inc ecx
mov @matrixY,ecx
invoke crt_calloc,@matrixY,sizeof DWORD
mov @lpArrY,eax
mov esi,_lpStFileData ;储存行
assume esi:pFileData
mov esi,[esi].pData
mov edi,@lpArrY
mov [edi],esi
add edi,4
.repeat
LODSB
.if al == 10
;mov byte ptr [esi-1],0
mov [edi],esi
add edi,4
.endif
.until al == 0
mov ecx,0 ;循环每一行 获取列数
mov esi,@lpArrY
.while ecx < @matrixY
push ecx
push esi
assume eax:DWORD
mov eax,esi
mov esi,[eax]
mov ecx,0
mov ebx,0
.repeat
lodsb
.if al != 32 && al != 9
.if ebx == 0
inc ecx
inc ebx
.endif
.else
xor ebx,ebx
.endif
.until al==0 || al==10
.if @matrixX == 0
mov @matrixX,ecx
.else
.if @matrixX != ecx
invoke crt_puts,addr s_matrix_format_error
invoke crt_exit, -1
.endif
.endif
pop esi
add esi,4
pop ecx
inc ecx
.endw
invoke crt_free,@lpArrY
mov eax,@matrixX ;为数组分配内存
mul @matrixY
mov @matrixSize,eax
invoke crt_calloc,@matrixSize,sizeof DWORD
mov @lpMatrix,eax
mov ecx,0 ;向数组写入数字
mov esi,_lpStFileData
assume esi:pFileData
mov esi,[esi].pData
.while ecx < @matrixSize
push ecx
shl ecx,2
add ecx,@lpMatrix
invoke crt_sscanf,esi,addr s_d,ecx
.repeat
lodsb
.until al==0 || al==10 || al==32 || al==9
.repeat
lodsb
.until al!=32 || al!=9 || al!=10
dec esi
pop ecx
inc ecx
.endw
invoke crt_printf,addr s_this_is_a_d_d_matrix,@matrixY,@matrixX
invoke crt_malloc,sizeof matrixData
assume eax:pMatrixData
mov ebx,@matrixY
mov [eax].Y,ebx
mov ebx,@matrixX
mov [eax].X,ebx
mov ebx,@lpMatrix
mov [eax].lpData,ebx
ret
readMatrix endp
;子程序名: mulMatrix
;功能: 根据提供的矩阵进行矩阵乘运算
;入口参数: lpSMatrixDataA=第一个矩阵matrixData结构体指针
; lpSMatrixDataB=第二个矩阵matrixData结构体指针
;出口参数: eax=matrixData结构体指针
mulMatrix proc uses ebx ecx edx esi edi,_lpSMatrixDataA:pMatrixData,_lpSMatrixDataB:pMatrixData
local @X1:DWORD,@Y1:DWORD,@X2:DWORD,@Y2:DWORD
local @lpMatrix:DWORD
local @tempX:DWORD,@tempY:DWORD,@tempXY:DWORD
mov esi,_lpSMatrixDataA
mov edi,_lpSMatrixDataB
assume esi:pMatrixData
assume edi:pMatrixData
assume eax:DWORD
mov eax,[esi].X
mov @X1,eax
mov eax,[esi].Y
mov @Y1,eax
mov eax,[edi].X
mov @X2,eax
mov eax,[edi].Y
mov @Y2,eax
mov esi,[esi].lpData
mov edi,[edi].lpData
assume esi:DWORD
assume edi:DWORD
mov eax,@X1
mov ebx,@Y2
.if eax != ebx
invoke crt_puts,addr s_matrix_cant_be_multiplied
invoke crt_exit, -1
.endif
mov eax,@Y1
mul @X2
invoke crt_calloc,eax,sizeof DWORD
mov @lpMatrix,eax
mov @tempY,0 ;A数组Y
mov @tempX,0 ;B数组X
mov @tempXY,0 ;A数组X B数组Y
mov eax,@tempY ;矩阵乘法
.while eax<@Y1
mov eax,@tempX
.while eax<@X2
mov eax,@tempXY
.while eax<@Y2
mov eax,[@tempY]
mov ebx,@X1
mul ebx
add eax,[@tempXY]
shl eax,2
add eax,esi
mov eax,[eax]
push eax
mov eax,[@tempXY]
mov ebx,@X2
mul ebx
add eax,[@tempX]
shl eax,2
add eax,edi
mov eax,[eax]
push eax
pop eax
pop ebx
mul ebx
push eax
mov eax,[@tempY]
mov ebx,@X2
mul ebx
add eax,[@tempX]
shl eax,2
add eax,@lpMatrix
pop ebx
add [eax],ebx
mov eax,[eax]
inc @tempXY
mov eax,@tempXY
.endw
mov @tempXY,0
inc @tempX
mov eax,@tempX
.endw
mov @tempX,0
inc @tempY
mov eax,@tempY
.endw
mov @tempY,0
invoke crt_malloc,sizeof matrixData
assume eax:pMatrixData
mov ebx,@Y1
mov [eax].Y,ebx
mov ebx,@X2
mov [eax].X,ebx
mov ebx,@lpMatrix
mov [eax].lpData,ebx
ret
mulMatrix endp
;子程序名: printMatrix
;功能: 打印矩阵到屏幕
;入口参数: lpSMatrixData=需要打印的矩阵
printMatrix proc uses ebx ecx edx esi edi,_lpSMatrixData:pMatrixData
assume eax:pMatrixData
mov eax,_lpSMatrixData
mov ebx,[eax].X
mov ecx,[eax].Y
mov edx,[eax].lpData
xor esi,esi
.while esi<ecx
xor edi,edi
.while edi<ebx
mov eax,[edx]
pushad
invoke crt_printf,addr s_d,eax
popad
add edx,4
inc edi
.endw
pushad
invoke crt_putchar,10
popad
inc esi
.endw
ret
printMatrix endp
;子程序名: saveMatrix
;功能: 保存矩阵到文件
;入口参数: lpSMatrixData=需要保存的矩阵
;
saveMatrix proc uses ebx ecx edx esi edi,_lpSMatrixData:pMatrixData,_lpszFileName:LPSTR
local @pStInputFile:PVOID
invoke crt_printf,addr s_saving_to_the_file_s,_lpszFileName
invoke crt_fopen,_lpszFileName,addr s_w
mov @pStInputFile,eax
.if @pStInputFile == 0
invoke crt_puts,addr s_open_file_error
invoke crt_exit, -1
.endif
assume eax:pMatrixData
mov eax,_lpSMatrixData
mov ebx,[eax].X
mov ecx,[eax].Y
mov edx,[eax].lpData
xor esi,esi
.while esi<ecx
xor edi,edi
.while edi<ebx
mov eax,[edx]
pushad
invoke crt_fprintf,@pStInputFile,addr s_d,eax
popad
add edx,4
inc edi
.endw
pushad
invoke crt_fputc,10,@pStInputFile
popad
inc esi
.endw
invoke crt_fclose,@pStInputFile
invoke crt_printf,addr s_saved
ret
saveMatrix endp
end main