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
122
Reaction score
15
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.
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top