Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个C矩阵代码是做什么的?_C_Matrix - Fatal编程技术网

这个C矩阵代码是做什么的?

这个C矩阵代码是做什么的?,c,matrix,C,Matrix,我真的不明白这个C代码是做什么的,我需要这样做,这样我才能将它“翻译”成汇编程序 int matrix[50][100], a, b, c; matrix[a][b] = c; 它创建了50个包含100个int的数组。然后用值c初始化ath数组的bth整数。但是您应该初始化a、b和c。否则,由于它们具有自动存储持续时间,因此其值将未定义 int matrix[50][100], a, b, c; matrix[a][b] = c; 这就是我的gcc(4.4.4)如何将代码转换为程序集(

我真的不明白这个C代码是做什么的,我需要这样做,这样我才能将它“翻译”成汇编程序

int matrix[50][100], a, b, c; 
matrix[a][b] = c; 
它创建了50个包含100个
int
的数组。然后用值
c
初始化
a
th数组的
b
th整数。但是您应该初始化
a
b
c
。否则,由于它们具有自动存储持续时间,因此其值将未定义

int matrix[50][100], a, b, c; 
matrix[a][b] = c;
这就是我的
gcc
(4.4.4)如何将代码转换为程序集(AT&T语法):

在C中,数组确实存储在中。也就是说,在源代码中编写
矩阵[a][b]
时,您将访问:

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?不,说真的……由于
a
b
中的垃圾值,代码的行为未定义。有时您可能会遇到分段错误。+如果您想在较低级别检查代码,可以使用
gcc-S file.c
这将为您提供
file.S
中的asm代码。是的,没有语法错误,因此您的代码将成功编译。asm代码中的一些注释将很好:)@GrijeshChauhan:对。我会更新它。为了进一步改进,您可以添加存储在col major中的矩阵信息,这就是为什么有效地址计算为
%edx=100*a=100*2=200
好的,对不起,我逐行写错了。这就是为什么*到100。。。我忘了告诉你这就是我回来的原因。非常好的回答谢谢@GrijeshChauhan.-)
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.
offset = row*NUMCOLS + column = a*100 + b