int matrix[50][100], a, b, c;
matrix[a][b] = c;
我真的不明白这段C代码是做什么的,我需要理解它以便可以将其翻译成汇编语言。
int matrix[50][100], a, b, c;
matrix[a][b] = c;
int matrix[50][100];
int a = 2;
int b = 3;
int c = 4;
matrix[a][b] = c;
movl $2, -4(%ebp) # a = 2
movl $3, -8(%ebp) # b = 3
movl $4, -12(%ebp) # c = 4
movl -4(%ebp), %edx # %edx = a = 2
movl -8(%ebp), %eax # %eax = b = 3
imull $100, %edx, %edx # %edx = 100 * a = 100 * 2 = 200
addl %eax, %edx # %edx = %edx + b = 200 + 3 = 203
# Formula: %edx = 100 * a + b
movl -12(%ebp), %eax # %eax = c = 4
movl %eax, -20012(%ebp,%edx,4) # Access to 203-th element (each of these
# are 4 bytes, ie. sizeof(int) on my
# computer) and put %eax = 4 in it.
在C语言中,数组确实是按行主序存储的(row-major order)。也就是说,当你在源代码中写matrix[a][b]
时,你将访问到:
offset = row*NUMCOLS + column = a*100 + b
这就是汇编代码所显示的内容。
%edx = 100 * a = 100 * 2 = 200
。 - Grijesh Chauhan
a
和b
中存在垃圾值,代码的行为是未定义的。有时候你可能会遇到分段错误。如果您想在低级别检查代码,可以使用gcc -S file.c
命令,这将给您提供一个file.s
汇编代码文件。是的,虽然没有语法错误,但您的代码将成功编译。 - Grijesh Chauhan