Program in C++ that calculates the sum of unsigned char array of 80 elements using MMX instructions through inline assembly programming

81 views Asked by At

`I am trying a program in C++ that declares an unsigned char array of 80 elements and initializes every element with "1." The program then calculates the sum of these 80 elements using MMX instructions through inline assembly programming and displays it on screen.

Hint i am given is the last eight bytes would be summed serially. I tried `

#include <iostream>

int main() {
    unsigned char dataArray[80];
    for (int i = 0; i < 80; ++i) {
        dataArray[i] = 1;
    }

    unsigned char sum;

    __asm {
        pxor mm0, mm0
        movq mm1, [dataArray]
        movq mm2, sum

        add_loop:
        movq mm3, [mm1]
        paddb mm0, mm3
        add mm1, 8
        dec ecx
        jnz add_loop
        movq sum, mm0
        emms
    }

    std::cout << "Sum: " << static_cast<int>(sum) << std::endl;

    return 0;
}

Result Sum of last eight elements: -1179010631 instead of 80 please help regarding this

0

There are 0 answers