How to multiply two matrices of size in using inline assembly in C++

Joined
Mar 3, 2024
Messages
1
Reaction score
0
I wrote this code in C++ with inline assembly _asm for square matrix multiplication.
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;
}
It doesn’t give any errors, but the resultant matrix C isn’t correct. It gives me these elements
Code:
5 12
0 0

I tried to change the offsets, but it didn't help.
 
Joined
Sep 21, 2022
Messages
143
Reaction score
19
I haven't written assembly in donkey's years, but one thing is jumping out at me.

Shouldn't there be 3 nested loops?

inner loop for k

inside a loop for i

inside a loop for j

I could be wrong.
 
Joined
Aug 6, 2023
Messages
8
Reaction score
0
#include <iostream>
using namespace std;

int main() {
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 {
// Calculate C[0][0] = A[0][0]*B[0][0] + A[0][1]*B[1][0]
mov eax, A[0][0]
mov ebx, B[0][0]
imul eax, ebx
mov ebx, B[1][0]
mov ecx, A[0][1]
imul ecx, ebx
add eax, ecx
mov C[0][0], eax

// Calculate C[0][1] = A[0][0]*B[0][1] + A[0][1]*B[1][1]
mov eax, A[0][0]
mov ebx, B[0][1]
imul eax, ebx
mov ebx, B[1][1]
mov ecx, A[0][1]
imul ecx, ebx
add eax, ecx
mov C[0][1], eax

// Calculate C[1][0] = A[1][0]*B[0][0] + A[1][1]*B[1][0]
mov eax, A[1][0]
mov ebx, B[0][0]
imul eax, ebx
mov ebx, B[1][0]
mov ecx, A[1][1]
imul ecx, ebx
add eax, ecx
mov C[1][0], eax

// Calculate C[1][1] = A[1][0]*B[0][1] + A[1][1]*B[1][1]
mov eax, A[1][0]
mov ebx, B[0][1]
imul eax, ebx
mov ebx, B[1][1]
mov ecx, A[1][1]
imul ecx, ebx
add eax, ecx
mov C[1][1], eax
}

cout << "Resultant matrix:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << C[j] << " ";
}
cout << endl;
}
return 0;
}


I compiled this program in GCC. It works. Haven't tried it in MSVC. See if it works for you. It was a nice exercise, by the way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,861
Messages
2,569,878
Members
46,087
Latest member
KVTRuth63

Latest Threads

Top