My goal is to create a DWORD matrix product of two WORD matrixs. The program produces the right result in all cases I’ve tried, but after printing the result the program crashes because a stack-based buffer overrun was detected.
#include <stdio.h>
void main()
{
// Number of rows of the first matrix
unsigned int m = 3;
// Number of columns of the first matrix
unsigned int n = 2;
// Number of columns of the second matrix
unsigned int k = 4;
// First matrix
short int mat1[] = { -1,-2, 4,5, 4,-2 };
// Second matrix
short int mat2[] = { 2,0,0,0, 0,2,0,0 };
// Result matrix
int mat3[1024];
__asm {
// Initialization of the row index to 0
mov eax, 0 // i = 0
// Outer loop for the rows of the result matrix
esterno_ciclo:
// Check if we have reached the last row of the matrix
cmp eax, m
jge fine_ciclo_esterno // If i >= m, exit the outer loop
// Initialization of the column index to 0
mov ebx, 0 // j = 0
// Inner loop for the columns of the result matrix
interno_ciclo:
// Check if we have reached the last column of the matrix
cmp ebx, k
jge fine_ciclo_interno // If j >= k, exit the inner loop
// Initialization of the sum to 0 for each new cell of the result matrix
mov dword ptr[ebp - 4], 0 // sum = 0
// Initialization of the column index of the first matrix to 0
mov ecx, 0 // l = 0
// Loop to multiply the elements of the matrices and sum the results
ciclo_medio:
// Check if we have reached the last column of the first matrix
cmp ecx, n
jge fine_ciclo_medio // If l >= n, exit the middle loop
// Calculate the addresses of the current elements of the matrices
mov edx, eax // edx = i
imul edx, n // edx = i * n
add edx, ecx // edx = i * n + l
movsx esi, word ptr[mat1 + edx * 2] // esi = mat1[i * n + l]
mov edx, ecx // edx = l
imul edx, k // edx = l * k
add edx, ebx // edx = l * k + j
movsx edi, word ptr[mat2 + edx * 2] // edi = mat2[l * k + j]
// Multiply the elements of the matrices and add to the sum
imul esi, edi // esi = mat1[i * n + l] * mat2[l * k + j]
add dword ptr[ebp - 4], esi // sum += mat1[i * n + l] * mat2[l * k + j]
// Increment l
inc ecx
jmp ciclo_medio
fine_ciclo_medio :
// Save the sum in the result matrix
mov edx, eax // edx = i
imul edx, k // edx = i * k
add edx, ebx // edx = i * k + j
mov esi, dword ptr[ebp - 4] // esi = sum
mov dword ptr[mat3 + edx * 4], esi // mat3[i * k + j] = sum
// Increment j
inc ebx
jmp interno_ciclo
fine_ciclo_interno :
// Increment i
inc eax
jmp esterno_ciclo
fine_ciclo_esterno :
}
// Print to screen
{ unsigned int i, j, h;
printf("Product matrix:n");
for (i = h = 0; i < m; i++)
{
for (j = 0; j < k; j++, h++)
printf("%6d ", mat3[h]);
printf("n");
}
}
}
I can’t understand what the problem is, I’m trying to simplify the program to try not to let it overflow, any advice?