Advertisement
Guest User

Untitled

a guest
May 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 KB | None | 0 0
  1. .386
  2. .model flat, stdcall
  3.  
  4. includelib msvcrt.lib
  5. extern exit: proc
  6. extern printf: proc
  7.  
  8. extern fopen: proc
  9. extern fclose: proc
  10. extern fscanf: proc
  11.  
  12. public start
  13.  
  14. .data
  15.  
  16. input_file_mode DB "r", 0
  17. input_file_name DB "Input.txt", 0
  18.  
  19. format_citire DB "%d %d %d", 0
  20. format DB "%d ", 0
  21. variable DD 0, 0
  22.  
  23. column_format DB "%f ", 0
  24. row_format DB 0AH, 0
  25. message DB "Matrix: ", 0
  26.  
  27. Matrix STRUCT
  28. rows DD ? ; Number of rows
  29. cols DD ? ; Number of columns
  30. data DD 1200 dup(?) ; The data stored in the matrix
  31. Matrix ENDS
  32.  
  33. INPUT_MATRIX Matrix{100, 3, {0, 0, 1}}
  34. WEIGHTS_MATRIX Matrix{01H, 03H, {0.3, 0.4, 0.5}}
  35. WEIGHTS_TRANSPOSED Matrix {, , {}}
  36. LABELS Matrix {100, 1, {}}
  37. PREDICTIONS Matrix {100, 1, {}}
  38. WEIGHTED_SUM Matrix {100,1 , {}}
  39.  
  40. temp DQ 0
  41. var dd 0
  42. a dd 0
  43. b dd 0
  44. l dd 0
  45.  
  46. adr_input dd 0
  47.  
  48. .code
  49.  
  50. matrix_print PROC
  51.  
  52. PUSH OFFSET message
  53. CALL printf
  54. ADD ESP, 4
  55.  
  56. ; EAX -> row count
  57. MOV EAX, -1
  58. MOV EDX, 0
  59.  
  60. ; ESI is pointing to the data stored in the matrix
  61. MOV ESI, [ESP + 4]
  62.  
  63. rows_loop:
  64. ; We increment the row count, reset the column count and then we check to see if we are out of bounds
  65. INC EAX
  66. ; EBX -> column count
  67. MOV EBX, -1
  68. MOV ECX, DWORD PTR[ESI]
  69. CMP ECX, EAX
  70. JE finish
  71.  
  72. ; These registers are saved on the stack
  73. ; because the printf function modifies them
  74. PUSH EAX
  75. PUSH ECX
  76. PUSH EDX
  77.  
  78. PUSH OFFSET row_format
  79. CALL printf
  80. ADD ESP, 4
  81.  
  82. POP EDX
  83. POP ECX
  84. POP EAX
  85.  
  86. cols_loop:
  87. ; We increment the column count and then we check to see if we are out of bounds
  88. ; If we are we go to the next row
  89. INC EBX
  90. MOV ECX, DWORD PTR[ESI + 4]
  91. CMP ECX, EBX
  92. JE rows_loop
  93.  
  94. ; EAX and EBX are saved on the stack because the current row and column are saved in them.
  95. ; The piece of code that follows modifies EAX and EBX.
  96.  
  97. PUSH EAX
  98. PUSH EBX
  99.  
  100. ; We call index converter in order to find
  101. ; the element at the position (i, j) in the matrix
  102. MOV EDX, DWORD PTR[ESI + 4]
  103. PUSH EDX
  104. PUSH EBX
  105. PUSH EAX
  106. CALL index_converter
  107.  
  108. ; These registers are saved on the stack
  109. ; Because the printf function modifies them
  110. PUSH EAX
  111. PUSH ECX
  112. PUSH EDX
  113.  
  114. ; We write the element found at (i, j) on the screen
  115. fld DWORD PTR[ESI + 8 + 4*EAX]
  116. sub esp, 8
  117. fstp QWORD PTR[ESP]
  118. ;MOV EDX, DWORD PTR[ESI + 8 + 4*EAX]
  119.  
  120. PUSH OFFSET column_format
  121. CALL printf
  122. ADD ESP, 12
  123.  
  124. ; Restore the registers
  125. pop EDX
  126. POP ECX
  127. POP EAX
  128.  
  129. ; The row count and column count are restored
  130. POP EBX
  131. POP EAX
  132.  
  133. ; Go to the next iteration
  134. JMP cols_loop
  135. finish:
  136. ; Print a new line
  137. PUSH OFFSET row_format
  138. CALL printf
  139. ADD ESP, 4
  140. ; Clean up the stack
  141. ret 4
  142. matrix_print ENDP
  143.  
  144. matrix_transpose PROC
  145. ; Create a stack frame
  146. ; to be able to use local variables
  147. PUSH EBP
  148. MOV EBP, ESP
  149. ; Allocate memory for 2 local variables
  150. ; i and j which will be used for indexing the rows and columns of the matrix
  151. ; to be transposed.
  152. SUB ESP, 8
  153.  
  154. ; The matrix to be transposed
  155. MOV ESI, [EBP + 8]
  156. ; The transposed matrix
  157. MOV EDI, [EBP + 12]
  158.  
  159. ; The tranpose matrix will have the reversed dimensions of the original matrix
  160. ; i.e. a matrix with dimensions M X N will be transposed into a matrix N X M
  161. MOV EAX, DWORD PTR[ESI]
  162. MOV EBX, DWORD PTR[ESI + 4]
  163. MOV [EDI], BX
  164. MOV [EDI + 4], AX
  165.  
  166. ; Initialize i and j to -1
  167. MOV EAX, -1
  168. MOV [EBP-4], EAX
  169. MOV [EBP-8], EAX
  170.  
  171. i_loop:
  172. ; Increment i
  173. MOV EAX, [EBP - 4]
  174. INC EAX
  175. MOV [EBP - 4], EAX
  176. ; Out of bounds?
  177. CMP EAX, DWORD PTR[ESI]
  178. JE end_transpose
  179. ; Reset j
  180. MOV EAX, -1
  181. MOV [EBP - 8], EAX
  182.  
  183. j_loop:
  184. ; Increment j
  185. MOV EAX, [EBP-8]
  186. INC EAX
  187. MOV [EBP-8], EAX
  188. ; Out of bounds?
  189. CMP EAX, DWORD PTR[ESI + 4]
  190. JE i_loop
  191.  
  192. ; Convert (i, j) to (i * cols + j)
  193. MOV EAX, DWORD PTR[ESI + 4]
  194. PUSH EAX
  195. MOV EAX, DWORD PTR[EBP - 8]
  196. PUSH EAX
  197. MOV EAX, DWORD PTR[EBP - 4]
  198. PUSH EAX
  199. CALL index_converter
  200.  
  201. MOV EDX, 0
  202. MOV EDX, EAX
  203.  
  204. ; Save EDX on the stack
  205. ; so it's value is not lost
  206. PUSH EDX
  207.  
  208. ; Convert (j, i) to (j * cols + i)
  209. MOV EAX, DWORD PTR[EDI + 4]
  210. PUSH EAX
  211. MOV EAX, DWORD PTR[EBP - 4]
  212. PUSH EAX
  213. MOV EAX, DWORD PTR[EBP - 8]
  214. PUSH EAX
  215. CALL index_converter
  216.  
  217. ; Restore the old value of EDX
  218. POP EDX
  219.  
  220. MOV ECX, 0
  221. MOV ECX, EAX
  222.  
  223. ; transpode[j][i] = matrix[i][j]
  224. MOV EAX, DWORD PTR[ESI + 8 + 4*EDX]
  225. MOV DWORD PTR[EDI + 8 + 4*ECX], EAX
  226.  
  227. ; Go to the next iteration
  228. JMP j_loop
  229.  
  230. end_transpose:
  231.  
  232. ; Restore the stack pointer`
  233. MOV ESP, EBP
  234. POP EBP
  235.  
  236. ; Clean up the stack
  237. ret 8
  238. matrix_transpose ENDP
  239.  
  240. matrix_multiply PROC
  241.  
  242. ; Creating a stack frame
  243. ; to be able to use local variables
  244. PUSH EBP
  245. MOV EBP, ESP
  246.  
  247. ; We allocate space for 3 local variables
  248. ; i.e. i, j and k which will be used for indexing
  249. SUB ESP, 12
  250.  
  251. ; We initialize the indices to -1
  252. MOV EAX, -1
  253. MOV [EBP-4], EAX
  254. MOV [EBP-8], EAX
  255. MOV [EBP-12], EAX
  256.  
  257. ; The result of the matrix multiplication
  258. ; will be stored in ESI
  259. MOV ESI, [EBP + 16]
  260.  
  261. ; EDI and EDX are the matrices we are multiplying
  262. MOV EDI, [EBP + 12]
  263. MOV EDX, [EBP + 8]
  264.  
  265. ; We check to see if the multiplication is possible
  266. ; if not we jump to the end of the program, meaning
  267. ; the multiplication will not occur
  268. MOV EAX, DWORD PTR[EDX + 4]
  269. CMP EAX, DWORD PTR[EDI]
  270. JNE multiply_end
  271.  
  272. ; If the multiplication is possible
  273. ; the resulting matrix will have the number of rows of the first matrix
  274. ; and the number of columns of the second matrix
  275. MOV EAX, DWORD PTR[EDX]
  276. MOV EBX, DWORD PTR[EDI + 4]
  277. MOV DWORD PTR[ESI], EAX
  278. MOV DWORD PTR[ESI + 4], EBX
  279.  
  280. i_loop:
  281. ; Increment i
  282. MOV EAX, [EBP-4]
  283. INC EAX
  284. MOV [EBP-4], EAX
  285.  
  286. ; Out of bounds?
  287. CMP EAX, DWORD PTR[ESI]
  288. JE multiply_end
  289.  
  290. ; Reset j
  291. MOV EAX, -1
  292. MOV [EBP-8], EAX
  293. j_loop:
  294. ; Increment j
  295. MOV EAX, [EBP-8]
  296. INC EAX
  297. MOV [EBP-8], EAX
  298.  
  299. ; Out of bounds?
  300. CMP EAX, DWORD PTR[ESI + 4]
  301. JE i_loop
  302.  
  303. ; Reset k
  304. MOV EAX, -1
  305. MOV [EBP-12], EAX
  306. k_loop:
  307. ; Increment k
  308. MOV EAX, [EBP-12]
  309. INC EAX
  310. MOV [EBP-12], EAX
  311. ; Out of bounds?
  312. CMP EAX, DWORD PTR[EDX + 4]
  313. JE j_loop
  314.  
  315. ; Save the adress of one of the matrices on the stack
  316. ; so we don't lose the reference to it
  317. PUSH EDX
  318. PUSH EDX
  319.  
  320. ; Convert (i, k) to (i * col + k)
  321. MOV EAX, DWORD PTR[EDX + 4]
  322. PUSH EAX
  323. MOV EAX, DWORD PTR[EBP - 12]
  324. PUSH EAX
  325. MOV EAX, DWORD PTR[EBP - 4]
  326. PUSH EAX
  327. CALL index_converter
  328.  
  329. ; Restore EDX
  330. POP EDX
  331.  
  332. ;CX = A[i][k]
  333. fild DWORD PTR[EDX + 8 + 4*EAX]
  334.  
  335. ; Convert (k, j) to (k * col + j)
  336. PUSH ECX
  337. MOV EAX, DWORD PTR[EDI + 4]
  338. PUSH EAX
  339. MOV EAX, DWORD PTR[EBP-8]
  340. PUSH EAX
  341. MOV EAX, DWORD PTR[EBP-12]
  342. PUSH EAX
  343. CALL index_converter
  344. pop ECX
  345.  
  346. ; AX = B[k][j]
  347. fld DWORD PTR[EDI + 8 + 4*EAX]
  348.  
  349. ; CX = A[i][k] * B[k][j]
  350. fmulp st(1), st(0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  351. fstp DWORD PTR[var]
  352.  
  353.  
  354. ; Convert (i, j) to (i * col + j)
  355. PUSH ECX
  356. MOV EAX, DWORD PTR[ESI + 4]
  357. PUSH EAX
  358. MOV EAX, DWORD PTR[EBP - 8]
  359. PUSH EAX
  360. MOV EAX, DWORD PTR[EBP - 4]
  361. PUSH EAX
  362. CALL index_converter
  363. POP ECX
  364.  
  365. ;AX = RESULT[i][j]
  366. ;ADD DWORD PTR[ESI + 8 + 4*EAX], ecx
  367. fld var
  368. fld DWORD PTR[ESI + 8 + 4*EAX]
  369. faddp st(1), st(0)
  370. fstp DWORD PTR[ESI + 8 + 4*EAX]
  371.  
  372. ; Restore the adress of one of the matrices
  373. POP EDX
  374.  
  375. ; Go to the next iteration
  376. JMP k_loop
  377. multiply_end:
  378.  
  379. ; Restore the stack pointer
  380. MOV ESP, EBP
  381. POP EBP
  382. ; Clean up the stack
  383. ret 12
  384. matrix_multiply ENDP
  385.  
  386. index_converter PROC
  387.  
  388. ; This function maps a 2 dimensional array index into an unidimensional array index
  389. ; using the formula: (i, j) => (i * cols + j) where cols is the number of columns of the bidimensional array
  390. ; and i and j are the current row and column
  391.  
  392. ; The number of columns
  393. MOV EAX, [ESP + 12]
  394.  
  395. ; The current column
  396. MOV EBX, [ESP + 8]
  397.  
  398. ; The current row
  399. MOV ECX, [ESP + 4]
  400.  
  401. ; EAX = i * cols
  402. MUL ECX
  403. ; EAX = i * cols + j
  404. ADD EAX, EBX
  405. ; Clean up the stack
  406. ret 12
  407. index_converter ENDP
  408.  
  409. ; sign PROC
  410. ; mov eax, 1
  411. ; sub esp, 8
  412. ; push 0
  413. ; mov temp, QWORD PTR[ESP + 4]
  414. ; add esp, 8
  415. ; cmp QWORD PTR[ESP+4], temp
  416. ; jle negative
  417. ; jmp gata
  418.  
  419. ; negative:
  420. ; mov eax, -1
  421.  
  422. ; gata:
  423. ; ret 8
  424. ; sign ENDP
  425.  
  426. start:
  427.  
  428. PUSH OFFSET input_file_mode
  429. PUSH OFFSET input_file_name
  430. CALL fopen
  431. ADD ESP, 8
  432.  
  433. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  434. mov adr_input, eax
  435. mov var, 100
  436. mov esi, 8
  437. mov edi, 8
  438.  
  439. citire_fisier:
  440. push offset l
  441. push offset b
  442. push offset a
  443. push offset format_citire
  444. push adr_input
  445. call fscanf
  446. add esp , 20
  447.  
  448. mov eax, a
  449. mov DWORD PTR[INPUT_MATRIX+ESI], eax
  450. add ESI, 4
  451. mov eax, b
  452. mov DWORD PTR[INPUT_MATRIX+ESI], eax
  453. add ESI, 4
  454. mov DWORD PTR[INPUT_MATRIX+ESI], 1
  455. add ESI, 4
  456.  
  457. mov eax, l
  458. mov DWORD PTR[LABELS + EDI], eax
  459. add EDI, 4
  460.  
  461. sub var, 1
  462. cmp var, 0
  463. jne citire_fisier
  464. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  465.  
  466. push offset WEIGHTS_TRANSPOSED
  467. push offset WEIGHTS_MATRIX
  468. call matrix_transpose
  469.  
  470. push offset WEIGHTED_SUM
  471. push offset WEIGHTS_TRANSPOSED
  472. push offset INPUT_MATRIX
  473. call matrix_multiply
  474.  
  475.  
  476. mov var, 100
  477. mov esi, 8
  478.  
  479. pred:
  480. mov eax, 1
  481. cmp DWORD PTR[WEIGHTED_SUM + ESI], 0
  482. jle negative
  483. jmp gata
  484.  
  485. negative:
  486. mov eax, -1
  487.  
  488. gata:
  489. mov DWORD PTR[PREDICTIONS + ESI], eax
  490.  
  491. cmp var, 1
  492. jne pred
  493.  
  494. push offset PREDICTIONS
  495. call matrix_print
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507. push 0
  508. call exit
  509. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement