- Joined
- Mar 3, 2024
- Messages
- 1
- Reaction score
- 0
I wrote this code in C++ with inline assembly _asm for square matrix multiplication.
It doesn’t give any errors, but the resultant matrix C isn’t correct. It gives me these elements
I tried to change the offsets, but it didn't help.
Code:
#include <iostream>
using namespace std;
int main() {
int n = 2;
int A[2][2] = { {1, 2}, {3, 4} };
int B[2][2] = { {5, 6}, {7, 8} };
int C[2][2] = { {0, 0}, {0, 0} };
_asm {
mov ecx, n
lea esi, A
lea edi, B
lea edx, C
outer_loop :
push ecx
mov ecx, n
inner_loop :
push ecx
mov eax, [esi] ; load A[i][k]
mov ebx, [edi] ; load B[k][j]
imul eax, ebx ; compute A[i][k] * B[k][j]
add [edx], eax ; accumulate result in C[i][j]
add esi, 4 ; increment A pointer
add edi, 4 ; increment B pointer
add edx, 4 ; increment C pointer
pop ecx
loop inner_loop ; repeat for all k
add esi, 400 ; jump to next row of A
sub esi, ecx ; adjust A pointer
mov eax, ecx
imul eax, 4
sub eax, n
imul eax, 4
lea edi, [edi + eax] ; jump to next column of B
pop ecx
loop outer_loop ; repeat for all i
}
cout << "Resultant matrix:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}
Code:
5 12
0 0
I tried to change the offsets, but it didn't help.