帮助需要从C了解生成的汇编代码

时间:2011-09-04 08:51:41

标签: linux optimization gcc assembly

#define M 20  
#define N 20  
void main()  
{  
    int i,j;  
    int A[M][N] = {0};  
    for (i=0; i < M; i++)  
    {  
        for (j=0; j< N; j++)  
        {  
            //A[i][j +1] = A[i][j] + 5;  
            A[i][j] = 0;  
        }  
    }  
    printf("%d\n", A[2][3]);  
}   

生成的汇编代码

main:  
    pushl   %ebp  
    xorl    %eax, %eax  
    pxor    %xmm0, %xmm0  
    movl    %esp, %ebp  
    andl    -16, %esp  
    pushl   %edi  
    movl    400, %ecx  
    subl    1628, %esp  
    leal    16(%esp), %edi  
    rep stosl  
    leal    16(%esp), %edx  
    leal    1616(%esp), %eax 
    .p2align 4,,7  
    .p2align 3  
.L2:  
    movdqa  %xmm0, (%edx)  
    movdqa  %xmm0, 16(%edx)  
    movdqa  %xmm0, 32(%edx)  
    movdqa  %xmm0, 48(%edx)  
    movdqa  %xmm0, 64(%edx)  
    addl    80, %edx  
    cmpl    %eax, %edx  
    jne .L2  
    movl    188(%esp), %eax  
    movl    .LC0, (%esp)  
    movl    %eax, 4(%esp)  
    call    printf  
    addl    1628, %esp  
    popl    %edi  
    movl    %ebp, %esp  
    popl    %ebp  
    ret  

我无法从主要标签L2中理解汇编代码。使用自动矢量化优化此汇编代码。     在此先感谢。

1 个答案:

答案 0 :(得分:5)

pushl   %ebp          ; save the old %ebp value
xorl    %eax, %eax    ; clear %eax
pxor    %xmm0, %xmm0  ; clear %xmm0
movl    %esp, %ebp  
andl    -16, %esp  
pushl   %edi          ; save edi  ^--- you have to restore all these value on function return.

movl    400, %ecx  
subl    1628, %esp    ; allocate 1628 bytes from stack
leal    16(%esp), %edi    ; load address of A to %edi
rep stosl             ; repeat cx(400) time, clear the memory -- this initialize "A" as {0}