typedef int *var_matrix;
int var_prod_ele(var_matrix A, var_matrixB, int i, int k, int n)
{
int j;
int result = 0;
for (j = 0; j < n; j++)
result += A[i*n + j] * B[j*n + k];
return result;
}
gcc -O1 -S -m32 array3.c
var_prod_ele:
pushl %ebp
movl %esp, %ebp
pushl %edi
pushl %esi
pushl %ebx
movl $0, %edi //edi = 0 = result
cmpl $0, 24(%ebp) //比较n和0
jle .L4 //如果n小于等于0,跳转到L4
movl 24(%ebp), %edx //edx = n
imull 16(%ebp), %edx //edx = n*i
movl 8(%ebp), %eax //eax = A
leal (%eax,%edx,4), %ecx //ecx = A+4*(n*i),ecx用于描述A[i*n +j]
movl 24(%ebp), %esi //esi = n
sall $2, %esi //esi = 4*n
movl 20(%ebp), %edx //edx = k
movl 12(%ebp), %eax //eax = B
leal (%eax,%edx,4), %edx //edx =B+4*k, edx用于描述B[j*n +k]
movl $0, %ebx //ebx = 0
movl $0, %edi //edi = 0
.L5:
movl (%edx), %eax //eax = B[j*n + k]
imull (%ecx), %eax //eax = B[j*n + k] * A[i*n+ j]
addl %eax, %edi //result += B[j*n + k] * A[i*n + j]
addl $1, %ebx //j++
addl $4, %ecx // A[i*n + j]的下一个元素地址(+4)
addl %esi, %edx // B[j*n + k]的下一个元素地址(+4*n)
cmpl 24(%ebp),%ebx //比较j和n
jne .L5 //如果j不等于n,跳转到L5,继续循环
.L4:
movl %edi, %eax //保存计算结果
popl %ebx
popl %esi
popl %edi
popl %ebp
ret
寄存器溢出:因为没有足够多的寄存器保存局部变量,考虑到参数 n 的值在循环过程中不会改变,编译器选择不把 n 保存到寄存器。