2014-03-27 38 views
2

我有這段代碼,我必須從彙編轉換爲c。我必須將函數的彙編代碼轉換爲該函數的c代碼。基於程序集編寫c代碼32位(x86)

function : 
    1.pushl %ebp   
    2.movl %esp, %ebp  
    3.movl 12(%ebp), %eax        
    4.movl 8(%ebp), %edx       
    5.addl %edx, %eax        
    6.addl 16(%ebp), %eax       
    7.popl %ebp     
    8.ret    

我爲了解釋的目的而對線條進行了編號。以下是我認爲每條線都在做的事情:

1.Save frame pointer by moving it onto the stack. 
2. Create new frame pointer 
3. Assigns value of the second parameter, which is 12 bytes above the base pointer, to the register eax 
4. Assigns value of first parameter which is 8 bytes above base pointer to the register edx 
5. Adds edx to the value in eax and stores the value in eax 
6. Adds the value in third parameter which is 16 bytes about the base pointer, to the register eax 
7.pops base pointer off of stack 
8. returns the calling function 

現在我很難真正理解如何將其轉換爲c代碼。我認爲代碼看起來有點像這樣:

function (int *x, int *y, int *z){ 
*y = x + y; 
*y = z + y; 
return y; 
} 

我不確定它會完全是這樣,但這是我想出來的。我很難理解如何理解程序集。這個翻譯是否有意義?任何幫助表示讚賞。

+2

參數不是指針,所以它只是'return x + y + z;' –

+0

你能解釋一些嗎?我真的不明白爲什麼它只是這個 – mufc

+2

你這樣做是錯誤的。寫一個看起來像你認爲它應該看起來的C程序。查看編譯器生成的程序集,根據所看到的內容調整您的假設。嘗試學習更多。 –

回答

2

您對彙編代碼的解釋是正確的。我可能會添加的一件事是,當一個函數返回一個值時,該值將返回到寄存器A中。所以C函數看起來像這樣。

int function(int x, int y, int z) 
{ 
    return x + y + z; 
}