汇编到C矩阵辅助

时间:2013-10-30 01:16:04

标签: c assembly x86

我真的很困惑我的家庭作业。我们给出了C代码,然后是下面列出的汇编。这是x86程序集。任何帮助将不胜感激。我试图根据我的理解来解决它。

C代码:

void transpose(Marray_t A) {
    int i, j;
    for (i = 0; i < M; i++)
        for (j = 0; j < i; j++) {
           int t = A[i][j];
           A[i][j] = A[j][i];
           A[j][i] = t;
        }
}

ONLY内循环的汇编代码:

1 .L3:
2 movl (%ebx), %eax     //is this getting the mem location of %ebx and setting to %eax?
3 movl (%esi,%ecx,4), %edx  //ecx * 4 + esi into edx
4 movl %eax, (%esi,%ecx,4)  //
5 addl $1, %ecx             //add 1 to ecx
6 movl %edx, (%ebx)         //move edx to mem location of ebx???
7 addl $52, %ebx            //I think this is M but I could be wrong
8 cmpl %edi, %ecx           //compare edi & ecx
9 jl .L3

这是我要回答的问题:

一个。 M的价值是多少? ......我认为这是52 ......?

B中。什么寄存器保存程序值i和j? ......我认为edx和eax?

℃。编写一个使用优化的转置的C代码版本 发生在这个循环中。在代码中使用参数M而不是数字 常数。

尝试(C):

void tranpose(Marray_t A) {
    int i, j;
    for(i = 0; i < M; i++) {
        for(j = 0; j < i; j++) {
            int *row = &A[i][0];
            int *col = &A[0][j];

            int value = (*row * 4) + *col;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

1 .L3:
2 movl (%ebx), %eax         // eax := read memory word at ebx
3 movl (%esi,%ecx,4), %edx  // edx := read memory word at esi + 4*ecx
4 movl %eax, (%esi,%ecx,4)  // store eax into that location
5 addl $1, %ecx             // add 1 to ecx
6 movl %edx, (%ebx)         // store edx into memory at ebx
7 addl $52, %ebx            // add 52 to ebx
8 cmpl %edi, %ecx           // compare edi & ecx
9 jl .L3

所以在这段代码中。 %ebx是[{1}}的地址,A[j][i]%esi的地址,A[i]是j。 52是%ecx,因此M可能是13(因为数组元素大小是4)