如何跨preT本次大会code,以获取数组的信息? [英] How to interpret this assembly code in order to get the information of the array?

查看:156
本文介绍了如何跨preT本次大会code,以获取数组的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C code有关数组的数组[M] [N]

I have c code about array array[M][N]

int array[M][N];
array_index(int i, int j)
{
 return array[i][j] + array[j][i];
}

在执行code结果到组件code如下:

When executed the code results into the assembly code as below:

array_index:
movl 4(%esp), %eax
movl 8(%esp), %edx
movl %eax, %ecx
sall $4, %ecx
addl %edx, %ecx
sall $4, %edx
addl %eax, %edx
movl array(,%ecx,4), %eax
addl array(,%edx,4), %eax
ret

怎能间preT的算法,以及如何获得M和N的信息?

How can we interpret the algorithm, and how to get the information of M and N?

推荐答案

我想你只是想了解大会code?

I suppose you just want to understand the assembly code?

好吧,首先,在C二维数组是通过在装配长度 M *ñ的一维数组psented重新$ P $。毕竟,内存寻址是线性在大多数的处理器架构。

Well, first of all, the two-dimensional array in C is represented by a one-dimensional array of length M * N in assembly. After all, memory addressing is linear in most processor architectures.

二维阵列的行被存储在存储器中的一个接一个。为了找到一个行 I ,你要跳过的 N I 行$ C>元素的每个( N 是列数,换句话说,它是一个单行的长度),然后执行Ĵ更多的元素以达到正确的列。

The 'rows' of the two-dimensional array are stored in memory one after the other. To find a row i, you have to skip i rows of N elements each (N is the number of columns, in other words it is the length of a single row), then proceed j more elements to reach the correct column.

在公式:数组[I] [J] 对应数组[I * N + J] 中的一维数组重新presentation

In formula: array[i][j] corresponds to array[i * N + j] in the one-dimensional array representation.

计算我* N + J 是由这些指令执行的:

The calculation i * N + j is performed by these instructions:

movl %eax, %ecx
sall $4, %ecx
addl %edx, %ecx

Sall的是一个位移(横跨4比特),这相当于(略比更有效)用2 4 一个乘法= 16。

sall is a bitwise shift (across 4 bits), which is equivalent to (and slightly more efficient than) a multiplication with 24 = 16.

这意味着 N 16,虽然在理论上它可能是9和16个(包括两端)之间的任何东西,因为它是不寻常的编译器牺牲一些内存实现良好的对齐的内存地址。

This implies that N is 16, though in theory it might be anything between 9 and 16 (both inclusive), since it is not uncommon for compilers to sacrifice some memory to achieve nicely aligned memory addresses.

至于 M ,也没有办法得出从code。这是典型的由C编译器生成的汇编code;边界检查是不是原生的事情。

As for M, there is no way to derive that from the code. This is typical for assembly code generated by a C compiler; bounds checking is not a native thing.

这篇关于如何跨preT本次大会code,以获取数组的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆