Advertisement
Guest User

Untitled

a guest
May 26th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.17 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.07, 0.0003, -0.5}}
  35. WEIGHTS_TRANSPOSED Matrix {, , {}}
  36. LABELS Matrix {100, 1, {}}
  37. PREDICTIONS Matrix {100, 1, {}}
  38. WEIGHTED_SUM Matrix {100,1 , {}}
  39.  
  40.  
  41. temp DQ 0
  42. var dd 0
  43. a dd 0
  44. b dd 0
  45. l dd 0
  46. err dd 0
  47. adr_input dd 0
  48. alfa dd 0.00001
  49. sign DD 0
  50. pula DD 0
  51.  
  52. .code
  53.  
  54. matrix_print PROC
  55.  
  56. PUSH OFFSET message
  57. CALL printf
  58. ADD ESP, 4
  59.  
  60. ; EAX -> row count
  61. MOV EAX, -1
  62. MOV EDX, 0
  63.  
  64. ; ESI is pointing to the data stored in the matrix
  65. MOV ESI, [ESP + 4]
  66.  
  67. rows_loop:
  68. ; We increment the row count, reset the column count and then we check to see if we are out of bounds
  69. INC EAX
  70. ; EBX -> column count
  71. MOV EBX, -1
  72. MOV ECX, DWORD PTR[ESI]
  73. CMP ECX, EAX
  74. JE finish
  75.  
  76. ; These registers are saved on the stack
  77. ; because the printf function modifies them
  78. PUSH EAX
  79. PUSH ECX
  80. PUSH EDX
  81.  
  82. PUSH OFFSET row_format
  83. CALL printf
  84. ADD ESP, 4
  85.  
  86. POP EDX
  87. POP ECX
  88. POP EAX
  89.  
  90. cols_loop:
  91. ; We increment the column count and then we check to see if we are out of bounds
  92. ; If we are we go to the next row
  93. INC EBX
  94. MOV ECX, DWORD PTR[ESI + 4]
  95. CMP ECX, EBX
  96. JE rows_loop
  97.  
  98. ; EAX and EBX are saved on the stack because the current row and column are saved in them.
  99. ; The piece of code that follows modifies EAX and EBX.
  100.  
  101. PUSH EAX
  102. PUSH EBX
  103.  
  104. ; We call index converter in order to find
  105. ; the element at the position (i, j) in the matrix
  106. MOV EDX, DWORD PTR[ESI + 4]
  107. PUSH EDX
  108. PUSH EBX
  109. PUSH EAX
  110. CALL index_converter
  111.  
  112. ; These registers are saved on the stack
  113. ; Because the printf function modifies them
  114. PUSH EAX
  115. PUSH ECX
  116. PUSH EDX
  117.  
  118. ; We write the element found at (i, j) on the screen
  119. fld DWORD PTR[ESI + 8 + 4*EAX]
  120. sub esp, 8
  121. fstp QWORD PTR[ESP]
  122. ;MOV EDX, DWORD PTR[ESI + 8 + 4*EAX]
  123.  
  124. PUSH OFFSET column_format
  125. CALL printf
  126. ADD ESP, 12
  127.  
  128. ; Restore the registers
  129. pop EDX
  130. POP ECX
  131. POP EAX
  132.  
  133. ; The row count and column count are restored
  134. POP EBX
  135. POP EAX
  136.  
  137. ; Go to the next iteration
  138. JMP cols_loop
  139. finish:
  140. ; Print a new line
  141. PUSH OFFSET row_format
  142. CALL printf
  143. ADD ESP, 4
  144. ; Clean up the stack
  145. ret 4
  146. matrix_print ENDP
  147.  
  148. matrix_print1 PROC
  149.  
  150. PUSH OFFSET message
  151. CALL printf
  152. ADD ESP, 4
  153.  
  154. ; EAX -> row count
  155. MOV EAX, -1
  156. MOV EDX, 0
  157.  
  158. ; ESI is pointing to the data stored in the matrix
  159. MOV ESI, [ESP + 4]
  160.  
  161. rows_loop:
  162. ; We increment the row count, reset the column count and then we check to see if we are out of bounds
  163. INC EAX
  164. ; EBX -> column count
  165. MOV EBX, -1
  166. MOV ECX, DWORD PTR[ESI]
  167. CMP ECX, EAX
  168. JE finish
  169.  
  170. ; These registers are saved on the stack
  171. ; because the printf function modifies them
  172. PUSH EAX
  173. PUSH ECX
  174. PUSH EDX
  175.  
  176. PUSH OFFSET row_format
  177. CALL printf
  178. ADD ESP, 4
  179.  
  180. POP EDX
  181. POP ECX
  182. POP EAX
  183.  
  184. cols_loop:
  185. ; We increment the column count and then we check to see if we are out of bounds
  186. ; If we are we go to the next row
  187. INC EBX
  188. MOV ECX, DWORD PTR[ESI + 4]
  189. CMP ECX, EBX
  190. JE rows_loop
  191.  
  192. ; EAX and EBX are saved on the stack because the current row and column are saved in them.
  193. ; The piece of code that follows modifies EAX and EBX.
  194.  
  195. PUSH EAX
  196. PUSH EBX
  197.  
  198. ; We call index converter in order to find
  199. ; the element at the position (i, j) in the matrix
  200. MOV EDX, DWORD PTR[ESI + 4]
  201. PUSH EDX
  202. PUSH EBX
  203. PUSH EAX
  204. CALL index_converter
  205.  
  206. ; These registers are saved on the stack
  207. ; Because the printf function modifies them
  208. PUSH EAX
  209. PUSH ECX
  210. PUSH EDX
  211.  
  212. ; We write the element found at (i, j) on the screen
  213. ;fld DWORD PTR[ESI + 8 + 4*EAX]
  214. ;sub esp, 8
  215. ;fstp QWORD PTR[ESP]
  216. MOV EDX, DWORD PTR[ESI + 8 + 4*EAX]
  217. push EDX
  218. PUSH OFFSET format
  219. CALL printf
  220. ADD ESP, 8
  221.  
  222. ; Restore the registers
  223. pop EDX
  224. POP ECX
  225. POP EAX
  226.  
  227. ; The row count and column count are restored
  228. POP EBX
  229. POP EAX
  230.  
  231. ; Go to the next iteration
  232. JMP cols_loop
  233. finish:
  234. ; Print a new line
  235. PUSH OFFSET row_format
  236. CALL printf
  237. ADD ESP, 4
  238. ; Clean up the stack
  239. ret 4
  240. matrix_print1 ENDP
  241.  
  242. matrix_transpose PROC
  243. ; Create a stack frame
  244. ; to be able to use local variables
  245. PUSH EBP
  246. MOV EBP, ESP
  247. ; Allocate memory for 2 local variables
  248. ; i and j which will be used for indexing the rows and columns of the matrix
  249. ; to be transposed.
  250. SUB ESP, 8
  251.  
  252. ; The matrix to be transposed
  253. MOV ESI, [EBP + 8]
  254. ; The transposed matrix
  255. MOV EDI, [EBP + 12]
  256.  
  257. ; The tranpose matrix will have the reversed dimensions of the original matrix
  258. ; i.e. a matrix with dimensions M X N will be transposed into a matrix N X M
  259. MOV EAX, DWORD PTR[ESI]
  260. MOV EBX, DWORD PTR[ESI + 4]
  261. MOV [EDI], BX
  262. MOV [EDI + 4], AX
  263.  
  264. ; Initialize i and j to -1
  265. MOV EAX, -1
  266. MOV [EBP-4], EAX
  267. MOV [EBP-8], EAX
  268.  
  269. i_loop:
  270. ; Increment i
  271. MOV EAX, [EBP - 4]
  272. INC EAX
  273. MOV [EBP - 4], EAX
  274. ; Out of bounds?
  275. CMP EAX, DWORD PTR[ESI]
  276. JE end_transpose
  277. ; Reset j
  278. MOV EAX, -1
  279. MOV [EBP - 8], EAX
  280.  
  281. j_loop:
  282. ; Increment j
  283. MOV EAX, [EBP-8]
  284. INC EAX
  285. MOV [EBP-8], EAX
  286. ; Out of bounds?
  287. CMP EAX, DWORD PTR[ESI + 4]
  288. JE i_loop
  289.  
  290. ; Convert (i, j) to (i * cols + j)
  291. MOV EAX, DWORD PTR[ESI + 4]
  292. PUSH EAX
  293. MOV EAX, DWORD PTR[EBP - 8]
  294. PUSH EAX
  295. MOV EAX, DWORD PTR[EBP - 4]
  296. PUSH EAX
  297. CALL index_converter
  298.  
  299. MOV EDX, 0
  300. MOV EDX, EAX
  301.  
  302. ; Save EDX on the stack
  303. ; so it's value is not lost
  304. PUSH EDX
  305.  
  306. ; Convert (j, i) to (j * cols + i)
  307. MOV EAX, DWORD PTR[EDI + 4]
  308. PUSH EAX
  309. MOV EAX, DWORD PTR[EBP - 4]
  310. PUSH EAX
  311. MOV EAX, DWORD PTR[EBP - 8]
  312. PUSH EAX
  313. CALL index_converter
  314.  
  315. ; Restore the old value of EDX
  316. POP EDX
  317.  
  318. MOV ECX, 0
  319. MOV ECX, EAX
  320.  
  321. ; transpode[j][i] = matrix[i][j]
  322. MOV EAX, DWORD PTR[ESI + 8 + 4*EDX]
  323. MOV DWORD PTR[EDI + 8 + 4*ECX], EAX
  324.  
  325. ; Go to the next iteration
  326. JMP j_loop
  327.  
  328. end_transpose:
  329.  
  330. ; Restore the stack pointer`
  331. MOV ESP, EBP
  332. POP EBP
  333.  
  334. ; Clean up the stack
  335. ret 8
  336. matrix_transpose ENDP
  337.  
  338. matrix_multiply PROC
  339.  
  340. ; Creating a stack frame
  341. ; to be able to use local variables
  342. PUSH EBP
  343. MOV EBP, ESP
  344.  
  345. ; We allocate space for 3 local variables
  346. ; i.e. i, j and k which will be used for indexing
  347. SUB ESP, 12
  348.  
  349. ; We initialize the indices to -1
  350. MOV EAX, -1
  351. MOV [EBP-4], EAX
  352. MOV [EBP-8], EAX
  353. MOV [EBP-12], EAX
  354.  
  355. ; The result of the matrix multiplication
  356. ; will be stored in ESI
  357. MOV ESI, [EBP + 16]
  358.  
  359. ; EDI and EDX are the matrices we are multiplying
  360. MOV EDI, [EBP + 12]
  361. MOV EDX, [EBP + 8]
  362.  
  363. ; We check to see if the multiplication is possible
  364. ; if not we jump to the end of the program, meaning
  365. ; the multiplication will not occur
  366. MOV EAX, DWORD PTR[EDX + 4]
  367. CMP EAX, DWORD PTR[EDI]
  368. JNE multiply_end
  369.  
  370. ; If the multiplication is possible
  371. ; the resulting matrix will have the number of rows of the first matrix
  372. ; and the number of columns of the second matrix
  373. MOV EAX, DWORD PTR[EDX]
  374. MOV EBX, DWORD PTR[EDI + 4]
  375. MOV DWORD PTR[ESI], EAX
  376. MOV DWORD PTR[ESI + 4], EBX
  377.  
  378. i_loop:
  379. ; Increment i
  380. MOV EAX, [EBP-4]
  381. INC EAX
  382. MOV [EBP-4], EAX
  383.  
  384. ; Out of bounds?
  385. CMP EAX, DWORD PTR[ESI]
  386. JE multiply_end
  387.  
  388. ; Reset j
  389. MOV EAX, -1
  390. MOV [EBP-8], EAX
  391. j_loop:
  392. ; Increment j
  393. MOV EAX, [EBP-8]
  394. INC EAX
  395. MOV [EBP-8], EAX
  396.  
  397. ; Out of bounds?
  398. CMP EAX, DWORD PTR[ESI + 4]
  399. JE i_loop
  400.  
  401. ; Reset k
  402. MOV EAX, -1
  403. MOV [EBP-12], EAX
  404. k_loop:
  405. ; Increment k
  406. MOV EAX, [EBP-12]
  407. INC EAX
  408. MOV [EBP-12], EAX
  409. ; Out of bounds?
  410. CMP EAX, DWORD PTR[EDX + 4]
  411. JE j_loop
  412.  
  413. ; Save the adress of one of the matrices on the stack
  414. ; so we don't lose the reference to it
  415. PUSH EDX
  416. PUSH EDX
  417.  
  418. ; Convert (i, k) to (i * col + k)
  419. MOV EAX, DWORD PTR[EDX + 4]
  420. PUSH EAX
  421. MOV EAX, DWORD PTR[EBP - 12]
  422. PUSH EAX
  423. MOV EAX, DWORD PTR[EBP - 4]
  424. PUSH EAX
  425. CALL index_converter
  426.  
  427. ; Restore EDX
  428. POP EDX
  429.  
  430. ;CX = A[i][k]
  431. fild DWORD PTR[EDX + 8 + 4*EAX]
  432.  
  433. ; Convert (k, j) to (k * col + j)
  434. PUSH ECX
  435. MOV EAX, DWORD PTR[EDI + 4]
  436. PUSH EAX
  437. MOV EAX, DWORD PTR[EBP-8]
  438. PUSH EAX
  439. MOV EAX, DWORD PTR[EBP-12]
  440. PUSH EAX
  441. CALL index_converter
  442. pop ECX
  443.  
  444. ; AX = B[k][j]
  445. fld DWORD PTR[EDI + 8 + 4*EAX]
  446.  
  447. ; CX = A[i][k] * B[k][j]
  448. fmulp st(1), st(0) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  449. fstp DWORD PTR[var]
  450.  
  451.  
  452. ; Convert (i, j) to (i * col + j)
  453. PUSH ECX
  454. MOV EAX, DWORD PTR[ESI + 4]
  455. PUSH EAX
  456. MOV EAX, DWORD PTR[EBP - 8]
  457. PUSH EAX
  458. MOV EAX, DWORD PTR[EBP - 4]
  459. PUSH EAX
  460. CALL index_converter
  461. POP ECX
  462.  
  463. ;AX = RESULT[i][j]
  464. ;ADD DWORD PTR[ESI + 8 + 4*EAX], ecx
  465. fld var
  466. fld DWORD PTR[ESI + 8 + 4*EAX]
  467. faddp st(1), st(0)
  468. fstp DWORD PTR[ESI + 8 + 4*EAX]
  469.  
  470. ; Restore the adress of one of the matrices
  471. POP EDX
  472.  
  473. ; Go to the next iteration
  474. JMP k_loop
  475. multiply_end:
  476.  
  477. ; Restore the stack pointer
  478. MOV ESP, EBP
  479. POP EBP
  480. ; Clean up the stack
  481. ret 12
  482. matrix_multiply ENDP
  483.  
  484. index_converter PROC
  485.  
  486. ; This function maps a 2 dimensional array index into an unidimensional array index
  487. ; using the formula: (i, j) => (i * cols + j) where cols is the number of columns of the bidimensional array
  488. ; and i and j are the current row and column
  489.  
  490. ; The number of columns
  491. MOV EAX, [ESP + 12]
  492.  
  493. ; The current column
  494. MOV EBX, [ESP + 8]
  495.  
  496. ; The current row
  497. MOV ECX, [ESP + 4]
  498.  
  499. ; EAX = i * cols
  500. MUL ECX
  501. ; EAX = i * cols + j
  502. ADD EAX, EBX
  503. ; Clean up the stack
  504. ret 12
  505. index_converter ENDP
  506.  
  507. citire proc
  508. PUSH OFFSET input_file_mode
  509. PUSH OFFSET input_file_name
  510. CALL fopen
  511. ADD ESP, 8
  512.  
  513. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  514. mov adr_input, eax
  515. mov var, 100
  516. mov esi, 8
  517. mov edi, 8
  518.  
  519. citire_fisier:
  520. push offset l
  521. push offset b
  522. push offset a
  523. push offset format_citire
  524. push adr_input
  525. call fscanf
  526. add esp , 20
  527.  
  528. mov ebx, DWORD PTR[ESP+4]
  529. mov ecx, DWORD PTR[ESP+8]
  530.  
  531. mov eax, a
  532. mov DWORD PTR[EbX + ESI], eax
  533. add ESI, 4
  534. mov eax, b
  535. mov DWORD PTR[EBX+ ESI], eax
  536. add ESI, 4
  537. mov DWORD PTR[EBX+ ESI], 1
  538. add ESI, 4
  539.  
  540. mov eax, l
  541. mov DWORD PTR[ecx+ EDI], eax
  542. add EDI, 4
  543.  
  544.  
  545.  
  546. sub var, 1
  547. cmp var, 0
  548. jne citire_fisier
  549. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  550. ret 8
  551. citire endp
  552.  
  553. start:
  554.  
  555. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  556. PUSH OFFSET LABELS
  557. push offset INPUT_MATRIX
  558. call citire
  559.  
  560.  
  561. push offset WEIGHTS_TRANSPOSED
  562. push offset WEIGHTS_MATRIX
  563. call matrix_transpose
  564.  
  565. push offset WEIGHTED_SUM
  566. push offset WEIGHTS_TRANSPOSED
  567. push offset INPUT_MATRIX
  568. call matrix_multiply
  569.  
  570.  
  571. mov a, 10000
  572. epochs:
  573. mov pula, 0
  574. push offset WEIGHTED_SUM
  575. push offset WEIGHTS_TRANSPOSED
  576. push offset INPUT_MATRIX
  577. call matrix_multiply
  578.  
  579. push offset WEIGHTS_TRANSPOSED
  580. call matrix_print
  581.  
  582. mov var, 100
  583. mov esi, 8
  584.  
  585. pred:
  586. mov eax, 1
  587. cmp DWORD PTR[WEIGHTED_SUM + ESI], 0
  588. jl negative
  589. jmp gata
  590.  
  591. negative:
  592. mov eax, -1
  593.  
  594. gata:
  595. mov DWORD PTR[PREDICTIONS + ESI], eax
  596.  
  597. add esi, 4
  598. sub var, 1
  599. cmp var, 1
  600. jne pred
  601.  
  602. mov var, 100
  603. mov esi, 8
  604. mov edi, 8
  605. points:
  606. mov eax, DWORD PTR[LABELS + ESI]
  607. cmp DWORD PTR[PREDICTIONS + ESI], eax
  608. jne go_backprop
  609. inc pula
  610. jmp no_backprop
  611.  
  612. go_backprop:
  613. mov b, 3
  614. mov EDX, 0
  615. backprop:
  616. ; push edx
  617. ; fild DWORD PTR[INPUT_MATRIX + EDI + EDX]
  618. ; sub esp, 8
  619. ; fstp QWORD PTR[esp]
  620. ; push offset column_format
  621. ; call printf
  622. ; add esp, 12
  623. ; pop edx
  624.  
  625. finit
  626. fild DWORD PTR[LABELS + ESI]
  627. fld DWORD PTR[alfa]
  628. fmulp st(1), st(0)
  629.  
  630. fild DWORD PTR[INPUT_MATRIX + EDI + EDX]
  631. fmulp st(1), st(0)
  632. fld DWORD PTR[WEIGHTS_TRANSPOSED + EDX + 8]
  633. faddp st(1), st(0)
  634. FSTP DWORD PTR[WEIGHTS_TRANSPOSED + EDX + 8]
  635.  
  636. add EDX, 4
  637.  
  638.  
  639. sub b, 1
  640. cmp b, 0
  641. jne backprop
  642.  
  643. no_backprop:
  644. add EDI, 12
  645.  
  646. add esi, 4
  647. sub var, 1
  648. cmp var, 0
  649. jne points
  650.  
  651. ; push pula
  652. ; push offset format
  653. ; call printf
  654. ; add esp, 8
  655.  
  656. sub a, 1
  657. cmp a, 0
  658. jne epochs
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668. push 0
  669. call exit
  670. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement