【问题标题】:Help Needed to understand the generated assembly Code from C需要帮助以了解从 C 生成的汇编代码
【发布时间】:2011-11-10 00:32:26
【问题描述】:
#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  

我无法理解从 main 到标签 L2 的汇编代码。此汇编代码使用自动矢量化进行了优化。 提前致谢。

【问题讨论】:

  • 它正在设置一个堆栈帧并将A 初始化为零。你到底在理解什么方面有困难?
  • 以下指令有什么用:andl -16, %esp leal 16(%esp), %edi leal 16(%esp), %edx leal 1616(%esp), %eax跨度>

标签: linux optimization gcc assembly


【解决方案1】:
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}

【讨论】:

  • 感谢您的帮助。以下指令有什么用:andl -16, %esp leal 16(%esp), %edi leal 16(%esp), %edx leal 1616(%esp), %eax
  • 16(%esp) 是指向A 的指针。 leal 16(%esp), %edi 将其加载到 %edi(对于 rep stosl
  • 1616(%esp) 是另一个局部变量的地址.. 我猜是i,但是我懒得查了。
  • 1616(%esp) 是指向数组最后一个元素的指针(用于检查是否已到达末尾)。 and -16, %esp 在 16 字节边界上对齐堆栈。
猜你喜欢
  • 1970-01-01
  • 2014-07-10
  • 2011-09-12
  • 1970-01-01
  • 2015-08-26
  • 2021-02-06
  • 1970-01-01
  • 2017-06-08
  • 2010-10-21
相关资源
最近更新 更多