2017-10-16 92 views
0

[編輯] 有人可以向我解釋在這個問題中,我們如何得到M和N的值,遍歷相應彙編代碼的每一行?反編譯IA32 32位AT&T彙編代碼中的一個函數C

我總是被困在movl array2部分。我們如何推導出常數M和N的值:

M和N常量使用#

#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]; 
} 

如果上述代碼生成以下彙編代碼中定義?

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 
+2

你真的希望我們輸入所有的?不要發佈代碼圖片!請解決你的問題。 – ikegami

+0

爲什麼代碼爲圖像? –

+0

當你說「我們得到M和N的值」時,你是什麼意思?你的意思是'我'和'j'? – ikegami

回答

0

好吧,經過很多研究,我可以找到解決方案。糾正我,如果我錯了。

通過以下步驟組裝所以要通過步驟:(由行號爲便於)

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 
  1. %ebp入堆棧

  2. 定義的常量
  3. %ebp%esp

  4. %ebx入堆棧

  5. %ecx等於int i(指數爲數組訪問)

  6. %ebx等於int j(指數爲數組訪問)

  7. %edx等於8 * %ecx + %ecx9i

  8. %edx2

  9. %eax左二進制移位後等於36i等於%EBX或j

  10. %eax4

  11. %eax左二進制移位後等於16j等於%eax - %ebx = 16j - j = 15j

  12. %eax等於60j2

  13. %eax左二進制移位後等於數組2元件與索引[4%ecx + %ebx] or [4i + 60j]

  14. 元索引[ 4%ebx + %edx ] or [ 4j + 36i ]的ARRAY1等於%eax[4i + 60j]

兩個數組元素的交換在12和13中使用%eax作爲 中間寄存器完成。

  • %ebx彈出

  • %esp的舊值恢復

  • %ebp彈出

  • 現在我們假設array1[i][j]的元件訪問權限等於4Ni + 4j

    array2[j][i]的元素訪問等於4Mj + 4i

    (作爲int的每個索引項中的4是4個字節,而i,j是從起始數組位置的單獨偏移量) 這是真的,因爲C將數組以行的形式存儲。

    因此,我們得到,M = 15和N = 9。

    1

    你需要檢查裝配的其他部分。例如,如果定義了M和N爲8兩者,會發現在裝配

    array1: 
        .zero 256 
    array2: 
        .zero 256 
    

    以下,因爲我的機器上,int是4個字節,8次8是64和64 * 4 = 256 。樣品組件可以找到here

    +0

    我認爲有一種方法可以找到答案,而不必假設M和N的值,但我認爲我們不需要知道程序集的其他部分。 – 24dinitrophenylhydrazine