3*3矩阵和3*1矩阵乘法使用risc-v汇编语言[重复]

问题描述 投票:0回答:0
// description: matrix muliply with two-level for loop

#include<stdio.h>
int main()
{
        int f,i=0;
        int h[9]={0}, x[3]={0}, y[3]={0};
        FILE *input = fopen("../input/3.txt","r");
        for(i = 0; i<9; i++) fscanf(input, "%d", &h[i]);
        for(i = 0; i<3; i++) fscanf(input, "%d", &x[i]);
        for(i = 0; i<3; i++) fscanf(input, "%d", &y[i]);
        fclose(input);
        int *p_x = &x[0] ;
        int *p_h = &h[0] ;
        int *p_y = &y[0] ;

        for (i = 0 ; i < 3; i++)
        {

                p_x = &x[0] ;
        /*
         for (f = 0 ; f < 3; f++)
             *p_y += *p_h++ * *p_x++ ;
                 p_y++;
        */

                for (f = 0 ; f < 3; f++){
                        asm volatile (
                    "addi t0, zero, 2\n\t"
                                        "bne t0, %[f], Multi\n\t"
                                        "mul t1, %[sp_h], %[sp_x]\n\t"
                                        "add %[sp_y], %[sp_y], t1\n\t"
                                        "addi %[p_h], %[p_h], 4\n\t"
                                        "addi %[p_x], %[p_x], 4\n\t"
                                        "addi %[p_y], %[p_y], 4\n\t"
                                        "beq zero, zero, Exit\n\t"
                                        "Multi: \n\t"                  # *p_y += *p_h++ * *p_x++
                                        "mul t1, %[sp_h], %[sp_x]\n\t" 
                                        "add %[sp_y], %[sp_y], t1\n\t"
                                        "addi %[p_h], %[p_h], 4\n\t"
                                        "addi %[p_x], %[p_x], 4\n\t"
                                        "Exit: \n\t"
                    :[p_y] "+r"(p_y),
                     [p_x] "+r"(p_x),
                     [p_h] "+r"(p_h),
                     [sp_y] "+r"(*p_y)
                    :[sp_h] "r"(*p_h),
                     [sp_x] "r"(*p_x),
                     [f] "r"(f)

                );
            printf("x value=%d, h value=%d, y value=%d, y address=%d\n", *p_x, *p_h, *p_y, p_y);
        }

        }

        p_y = &y[0];

        for(i = 0; i<3; i++)
                printf("%d \n", *p_y++);

        return(0) ;

}

我想把这条评论

for (f = 0 ; f < 3; f++) *p_y += *p_h++ * *p_x++ ;
转移到asm volatile(...)中,但是上面的asm代码我遇到了这个问题: my problem 看起来
p_y
地址添加正确,我的乘法正确,但我存储到内存中的值是错误的。它存储到内存中的速度太快了,现在我的答案将以前的答案加在一起。上面的代码答案是
5 28 69
有人能帮帮我吗?

期待

14 
32 
50

求答案。 这是输入数据:

1 2 3 4 5 6 7 8 9
1 2 3
0 0 0
assembly inline-assembly volatile riscv
© www.soinside.com 2019 - 2024. All rights reserved.