gpt4 book ai didi

linux - 需要帮助才能理解从 C 生成的汇编代码

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:26 24 4
gpt4 key购买 nike

#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 的汇编代码。此汇编代码使用自动矢量化进行了优化。 提前致谢。

最佳答案

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}

关于linux - 需要帮助才能理解从 C 生成的汇编代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7298184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com