Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 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. public start
  9.  
  10. .data
  11.  
  12. column_format DB "%d ", 0
  13. row_format DB 0AH, 0
  14. message DB "Matrix: ", 0
  15.  
  16. Matrix STRUCT
  17. rows DW ? ; Number of rows
  18. cols DW ? ; Number of columns
  19. data DW 100 dup(?) ; The data stored in the matrix
  20. Matrix ENDS
  21.  
  22. A Matrix {03H, 03H, {01H, 02H, 03H, 04H, 05H, 06H, 07H, 08H, 09H}}
  23. B Matrix {03H, 03H, {01H, 02H, 03H, 04H, 05H, 06H, 07H, 08H, 09H}}
  24. RESULT Matrix {, , { }}
  25.  
  26. ; 3 x 3 Matrix
  27. ; 1 2 3
  28. ; 4 5 6
  29. ; 7 8 9
  30.  
  31. .code
  32.  
  33. matrix_print PROC
  34.  
  35. PUSH OFFSET message
  36. CALL printf
  37. ADD ESP, 4
  38.  
  39. ; EAX -> row count
  40. MOV EAX, -1
  41. MOV EDX, 0
  42.  
  43. ; ESI is pointing to the data stored in the matrix
  44. MOV ESI, [ESP + 4]
  45.  
  46. rows_loop:
  47. ; We increment the row count, reset the column count and then we check to see if we are out of bounds
  48. INC EAX
  49. ; EBX -> column count
  50. MOV EBX, -1
  51. MOV CX, WORD PTR[ESI]
  52. CMP CX, AX
  53. JE finish
  54.  
  55. ; These registers are saved on the stack
  56. ; because the printf function modifies them
  57. PUSH EAX
  58. PUSH ECX
  59. PUSH EDX
  60.  
  61. PUSH OFFSET row_format
  62. CALL printf
  63. ADD ESP, 4
  64.  
  65. POP EDX
  66. POP ECX
  67. POP EAX
  68.  
  69. cols_loop:
  70. ; We increment the column count and then we check to see if we are out of bounds
  71. ; If we are we go to the next row
  72. INC EBX
  73. MOV CX, WORD PTR[ESI + 2]
  74. CMP CX, BX
  75. JE rows_loop
  76.  
  77. ; EAX and EBX are saved on the stack because the current row and column are saved in them.
  78. ; The piece of code that follows modifies these registers.
  79.  
  80. PUSH EAX
  81. PUSH EBX
  82.  
  83. ; We call index converter in order to find
  84. ; the element found at position (i, j) in the matrix
  85. MOV DX, WORD PTR[ESI + 2]
  86. PUSH EDX
  87. PUSH EBX
  88. PUSH EAX
  89. CALL index_converter
  90.  
  91. ; These registersr are saved on the stack
  92. ; Because the printf function modifies them
  93. PUSH EAX
  94. PUSH ECX
  95. PUSH EDX
  96.  
  97. ; We write the element found at (i, j) on the screen
  98. MOV DX, WORD PTR[ESI + 4 + 2*EAX]
  99. PUSH EDX
  100. PUSH OFFSET column_format
  101. CALL printf
  102. ADD ESP, 8
  103.  
  104. pop EDX
  105. POP ECX
  106. POP EAX
  107.  
  108. ; The row count and column count are restored
  109. POP EBX
  110. POP EAX
  111.  
  112. ; Go to the next element
  113. JMP cols_loop
  114. finish:
  115.  
  116. PUSH OFFSET row_format
  117. CALL printf
  118. ADD ESP, 4
  119.  
  120. ret 4
  121. matrix_print ENDP
  122.  
  123. matrix_multiply PROC
  124.  
  125.  
  126. matrix_multiply ENDP
  127.  
  128. index_converter PROC
  129.  
  130. ; This function maps a 2 dimensional array index into an unidimensional array index
  131. ; using the formula: (i, j) => (i * cols + j) where cols is the number of columns of the bidimensional array
  132. ; and i and j are the current row and column
  133.  
  134. ; The number of columns
  135. MOV EAX, [ESP + 12]
  136.  
  137. ; The current column
  138. MOV EBX, [ESP + 8]
  139.  
  140. ; The current row
  141. MOV ECX, [ESP + 4]
  142.  
  143. ; EAX = i * cols
  144. MUL CX
  145. ; EAX = i * cols + j
  146. ADD EAX, EBX
  147.  
  148. ret 12
  149. index_converter ENDP
  150.  
  151. start:
  152.  
  153. PUSH OFFSET A
  154. CALL matrix_print
  155.  
  156. PUSH OFFSET B
  157. CALL matrix_print
  158.  
  159. PUSH OFFSET RESULT
  160. PUSH OFFSET B
  161. PUSH OFFSET A
  162. CALL matrix_multiply
  163.  
  164. push 0
  165. call exit
  166. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement