簡體   English   中英

基於匯編 32 位 (x86) 編寫 c 代碼

[英]writing c code based on assembly 32 bit (x86)

我有這段代碼,我必須將其從程序集轉換為 c。 我必須將函數的匯編代碼轉換為該函數的 c 代碼。

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

我不確定它會完全像這樣,但這是我想出的。 我很難真正理解如何理解匯編。 這個翻譯有意義嗎?任何幫助表示贊賞。

您對匯編代碼的解釋是正確的。 我可能要補充的一件事是,當函數返回一個值時,該值在寄存器A返回。 所以C函數看起來像這樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM