[英]Disassembling IA32 32 bit AT&T assembly code of a function in C
[Edited] Could someone explain to me how we get the values of M and N in this problem, going through each line of the corresponding assembly code? [已编辑] 有人可以向我解释我们如何在这个问题中获得 M 和 N 的值,遍历相应汇编代码的每一行吗?
I always get stumped at the movl array2 part.我总是被 movl array2 部分难住。
M and N are constants defined using #define M 和 N 是使用 #define 定义的常量
#define M <some value>
#define N <some value>
int array1[M][N];
int array2[N][M];
int copy(int i, int j)
{
array1[i][j] = array2[j][i];
}
If the above code generates the following assembly code: How do we deduce the values of the constants M and N?如果上述代码生成以下汇编代码: 我们如何推导出常量 M 和 N 的值?
copy:
pushl %ebp
movl %esp, %ebp
pushl %ebx
movl 8(%ebp), %ecx
movl 12(%ebp), %ebx
leal (%ecx, %ecx, 8), %edx
sall $2, %edx
movl %ebx, %eax
sall $4, %eax
subl %ebx, %eax
sall $2, %eax
movl array2(%eax, %ecx, 4), %eax
movl %eax, array1(%edx, %ebx, 4)
popl %ebx
movl %ebp,%esp
popl %ebp
ret
You need to check other parts of the assembly.您需要检查组件的其他部分。 For example, if you define M and N as 8 both, you will find the following in the assembly
例如,如果您将 M 和 N 都定义为 8,您将在程序集中找到以下内容
array1:
.zero 256
array2:
.zero 256
because on my machine, int is 4 bytes and 8 times 8 is 64. And 64 * 4 = 256. The sample assembly can be found here .因为在我的机器上,int 是 4 个字节,8 次 8 是 64。64 * 4 = 256。示例程序集可以在这里找到。
Alright guys, after much research I was able to find a solution.好吧,经过大量研究,我找到了解决方案。 Correct me if I am wrong.
如果我错了,请纠正我。
So going through the following assembly step by step: (Added line numbers for ease)因此,请逐步完成以下组装:(为方便起见添加了行号)
M and N are constants defined using #define M 和 N 是使用 #define 定义的常量
int array1[M][N];
int array2[N][M];
int copy(int i, int j)
{
array1[i][j] = array2[j][i];
}
copy:
1 pushl %ebp
2 movl %esp, %ebp
3 pushl %ebx
4 movl 8(%ebp), %ecx
5 movl 12(%ebp), %ebx
6 leal (%ecx, %ecx, 8), %edx
7 sall $2, %edx
8 movl %ebx, %eax
9 sall $4, %eax
10 subl %ebx, %eax
11 sall $2, %eax
12 movl array2(%eax, %ecx, 4), %eax
13 movl %eax, array1(%edx, %ebx, 4)
14 popl %ebx
15 movl %ebp,%esp
16 popl %ebp
ret
Push %ebp
into stack将
%ebp
压入堆栈
%ebp
points to %esp
%ebp
指向%esp
Push %ebx
into stack将
%ebx
压入堆栈
%ecx
equals int i
(index for array access) %ecx
等于int i
(数组访问的索引)
%ebx
equals int j
(index for array access) %ebx
等于int j
(数组访问的索引)
%edx
equals 8 * %ecx + %ecx
or 9i
%edx
等于8 * %ecx + %ecx
或9i
%edx
equals 36i
after a left binary shift of 2
%edx
在二进制左移2
后等于36i
%eax
equals %ebx or j
%eax
等于 %ebx 或j
%eax
equals 16j
after a left binary shift of 4
%eax
左移二进制4
后等于16j
%eax
equals %eax - %ebx = 16j - j = 15j
%eax
等于%eax - %ebx = 16j - j = 15j
%eax
equals 60j
after a left binary shift of 2
%eax
左移2
后等于60j
%eax
equals array2 element with index [4%ecx + %ebx] or [4i + 60j]
%eax
等于下标为[4%ecx + %ebx] or [4i + 60j]
array2 元素
Element with index [ 4%ebx + %edx ] or [ 4j + 36i ]
of array1 equals %eax
or [4i + 60j]
array1 的索引为
[ 4%ebx + %edx ] or [ 4j + 36i ]
的元素等于%eax
或[4i + 60j]
A swap of the two array elements done in 12 and 13 using %eax as intermediary register.
使用 %eax 作为中间寄存器在 12 和 13 中完成的两个数组元素的交换。
%ebx
popped %ebx
弹出
%esp
's old value restored %esp
的旧值已恢复
%ebp
popped %ebp
弹出
Now we assume array1[i][j]
's element access to be equal to 4Ni + 4j
现在我们假设
array1[i][j]
的元素访问等于4Ni + 4j
And array2[j][i]
's element access to be equal to 4Mj + 4i
.并且
array2[j][i]
的元素访问等于4Mj + 4i
。
( The 4 in each index term as int is of 4 bytes and i, j are individual offsets from starting array location ) This is true because C stores arrays in a row major form. (每个索引项中的 4 作为 int 是 4 个字节,i, j 是距起始数组位置的单独偏移量)这是正确的,因为 C 以行主要形式存储数组。
So equating we get, M = 15 and N = 9.所以等价我们得到,M = 15 和 N = 9。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.