2013-10-30 65 views
1

我真的很困惑我的家庭作業。我們給C代碼,然後組裝下面列出。這是x86彙編。任何援助將不勝感激。我試圖根據我的理解來解決這個問題。裝配到C矩陣Assignment

C代碼:只內環

void transpose(Marray_t A) { 
    int i, j; 
    for (i = 0; i < M; i++) 
     for (j = 0; j < i; j++) { 
      int t = A[i][j]; 
      A[i][j] = A[j][i]; 
      A[j][i] = t; 
     } 
} 

彙編代碼:

1 .L3: 
2 movl (%ebx), %eax  //is this getting the mem location of %ebx and setting to %eax? 
3 movl (%esi,%ecx,4), %edx //ecx * 4 + esi into edx 
4 movl %eax, (%esi,%ecx,4) // 
5 addl $1, %ecx    //add 1 to ecx 
6 movl %edx, (%ebx)   //move edx to mem location of ebx??? 
7 addl $52, %ebx   //I think this is M but I could be wrong 
8 cmpl %edi, %ecx   //compare edi & ecx 
9 jl .L3 

這裏是我來回答:

A.什麼是M值? ...我認爲這是52 ...?

B.什麼寄存器保存程序值我和j? ...我認爲edx和eax? C.編寫一個C代碼版本的轉置文件,利用該循環中發生的優化 。在代碼中使用參數M而不是數字 常量。

在嘗試(C):

void tranpose(Marray_t A) { 
    int i, j; 
    for(i = 0; i < M; i++) { 
     for(j = 0; j < i; j++) { 
      int *row = &A[i][0]; 
      int *col = &A[0][j]; 

      int value = (*row * 4) + *col; 
     } 
    } 
} 
+0

也許你想回顧(並接受我的回答,如果合適的話) –

+0

這就是我所困惑的,我不知道我們是否可以根據所給出的來確定M.我從課本中直接複製了這些,所以我不知道。 – user1758231

回答

1
1 .L3: 
2 movl (%ebx), %eax   // eax := read memory word at ebx 
3 movl (%esi,%ecx,4), %edx // edx := read memory word at esi + 4*ecx 
4 movl %eax, (%esi,%ecx,4) // store eax into that location 
5 addl $1, %ecx    // add 1 to ecx 
6 movl %edx, (%ebx)   // store edx into memory at ebx 
7 addl $52, %ebx   // add 52 to ebx 
8 cmpl %edi, %ecx   // compare edi & ecx 
9 jl .L3 

因此,在這種代碼。 %ebxA[j][i],%esi的地址是A[i]%ecx的地址是j。 52是sizeof(A[j]),所以M可能是13(因爲數組元素的大小是4)

+0

好的,因爲我現在正在努力轉換到C。我發佈了迄今爲止的代碼,但我仍然需要幫助。 – user1758231