Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .386
- .model flat, stdcall
- includelib msvcrt.lib
- extern exit: proc
- extern printf: proc
- public start
- .data
- column_format DB "%d ", 0
- row_format DB 0AH, 0
- message DB "Matrix: ", 0
- Matrix STRUCT
- rows DW ? ; Number of rows
- cols DW ? ; Number of columns
- data DW 100 dup(?) ; The data stored in the matrix
- Matrix ENDS
- A Matrix {03H, 03H, {01H, 02H, 03H, 04H, 05H, 06H, 07H, 08H, 09H}}
- B Matrix {03H, 03H, {01H, 02H, 03H, 04H, 05H, 06H, 07H, 08H, 09H}}
- RESULT Matrix {, , { }}
- ; 3 x 3 Matrix
- ; 1 2 3
- ; 4 5 6
- ; 7 8 9
- .code
- matrix_print PROC
- PUSH OFFSET message
- CALL printf
- ADD ESP, 4
- ; EAX -> row count
- MOV EAX, -1
- MOV EDX, 0
- ; ESI is pointing to the data stored in the matrix
- MOV ESI, [ESP + 4]
- rows_loop:
- ; We increment the row count, reset the column count and then we check to see if we are out of bounds
- INC EAX
- ; EBX -> column count
- MOV EBX, -1
- MOV CX, WORD PTR[ESI]
- CMP CX, AX
- JE finish
- ; These registers are saved on the stack
- ; because the printf function modifies them
- PUSH EAX
- PUSH ECX
- PUSH EDX
- PUSH OFFSET row_format
- CALL printf
- ADD ESP, 4
- POP EDX
- POP ECX
- POP EAX
- cols_loop:
- ; We increment the column count and then we check to see if we are out of bounds
- ; If we are we go to the next row
- INC EBX
- MOV CX, WORD PTR[ESI + 2]
- CMP CX, BX
- JE rows_loop
- ; EAX and EBX are saved on the stack because the current row and column are saved in them.
- ; The piece of code that follows modifies these registers.
- PUSH EAX
- PUSH EBX
- ; We call index converter in order to find
- ; the element found at position (i, j) in the matrix
- MOV DX, WORD PTR[ESI + 2]
- PUSH EDX
- PUSH EBX
- PUSH EAX
- CALL index_converter
- ; These registersr are saved on the stack
- ; Because the printf function modifies them
- PUSH EAX
- PUSH ECX
- PUSH EDX
- ; We write the element found at (i, j) on the screen
- MOV DX, WORD PTR[ESI + 4 + 2*EAX]
- PUSH EDX
- PUSH OFFSET column_format
- CALL printf
- ADD ESP, 8
- pop EDX
- POP ECX
- POP EAX
- ; The row count and column count are restored
- POP EBX
- POP EAX
- ; Go to the next element
- JMP cols_loop
- finish:
- PUSH OFFSET row_format
- CALL printf
- ADD ESP, 4
- ret 4
- matrix_print ENDP
- matrix_multiply PROC
- matrix_multiply ENDP
- index_converter PROC
- ; This function maps a 2 dimensional array index into an unidimensional array index
- ; using the formula: (i, j) => (i * cols + j) where cols is the number of columns of the bidimensional array
- ; and i and j are the current row and column
- ; The number of columns
- MOV EAX, [ESP + 12]
- ; The current column
- MOV EBX, [ESP + 8]
- ; The current row
- MOV ECX, [ESP + 4]
- ; EAX = i * cols
- MUL CX
- ; EAX = i * cols + j
- ADD EAX, EBX
- ret 12
- index_converter ENDP
- start:
- PUSH OFFSET A
- CALL matrix_print
- PUSH OFFSET B
- CALL matrix_print
- PUSH OFFSET RESULT
- PUSH OFFSET B
- PUSH OFFSET A
- CALL matrix_multiply
- push 0
- call exit
- end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement