2013-07-12 51 views
1
void myfun1(char *str) { 
push ebp 
mov ebp,esp 
char buffer[16]; 
sub esp,0x18 
strcpy(buffer, str); 
mov eax,DWORDPTR [ebp+8] 
mov DWORD PTR [esp+4],eax 
lea eax,[ebp-16] 
mov DWORD PTR [esp],eax 
call 0x80482c4 <[email protected]> 
myfun2(buffer); 
lea eax,[ebp-16] 
mov DWORD PTR [esp],eax 
call 0x80483b4 <myfun2> 
} 
leave 
ret 

如果有任何可以請也解釋了此代碼爲我..即時組裝一個初學者..在裝配中strcpy是什麼意思?

+1

看起來像字符串複製給我。將字符串複製到指定的緩衝區。而不是這個C混入了組裝,而不是純粹的組裝? – Gray

+0

是的,它與C混合..你是對的! – Rebecca

+0

這是哪個編譯器的工作原理?視覺?數字? –

回答

1

這似乎是C代碼彙編的行由行做混合正是以前行說。請注意,這樣做沒有任何意義。其中一種語言應該被註釋掉。

void myfun1(char *str) {  //C 
    push ebp     //ASM 
    mov ebp,esp     //- 
    char buffer[16];   //C 
    sub esp,0x18    //ASM 
    strcpy(buffer, str);  //C 
    mov eax,DWORDPTR [ebp+8] //ASM 
    mov DWORD PTR [esp+4],eax //- 
    lea eax,[ebp-16]   //- 
    mov DWORD PTR [esp],eax  //- 
    call 0x80482c4 <[email protected]> //- 
    myfun2(buffer);    //C 
    lea eax,[ebp-16]   //ASM 
    mov DWORD PTR [esp],eax  //- 
    call 0x80483b4 <myfun2>  //- 
}        //C 
leave       //ASM 
ret        //- 
+0

我正在爲我的考試而學習,而這段代碼是由我的醫生撰寫的,我試圖弄清楚它是什麼意思,但仍然沒有明確指出它。所以我需要有人能夠向我解釋這一點。 。預先感謝所有.. – Rebecca

1

原來的C函數爲:

void myfun1(char *str) { 
    char buffer[16]; 
    strcpy(buffer, str); 
    myfun2(buffer); 
} 

你身邊的報表中看到的組件僅僅是什麼是編譯器爲每個語句產生。例如,調用myfun2與myfun2(buffer)產生以下彙編指令:

lea eax,[ebp-16]   ; load the address of buffer (local variable at ebp-16) into eax 
mov DWORD PTR [esp],eax  ; "push" the address on the stack as parameter - stack pointer has already been adjusted earlier 
call 0x80483b4 <myfun2>  ; call the function 

這樣的輸出可通過反彙編器來產生,條件是經編譯的代碼具有足夠的信息來裝配語句映射回源代碼行。

+0

如果你擅長裝配,能否請你在這段代碼中逐行解釋彙編語言的代碼行..謝謝! – Rebecca

+0

給我第二個... –

+0

好吧..=)把你的時間,我等待 – Rebecca

4

這只是顯示由C函數生成的彙編代碼。如果您註釋掉C行,並在中間添加一些換行符,它會變得更清晰。

// void myfun1(char *str) { 
push ebp 
mov ebp,esp 

// char buffer[16]; 
sub esp,0x18     // Allocate space for buffer and function args. 

// strcpy(buffer, str); 
mov eax,DWORDPTR [ebp+8]  // Load the str parameter into eax. 
mov DWORD PTR [esp+4],eax  // Set str as the second argument to strcpy. 
lea eax,[ebp-16]    // Load the address of the buffer into eax. 
mov DWORD PTR [esp],eax  // Set the address as the first argument to strcpy. 
call 0x80482c4 <[email protected]> // Call strcpy. 

// myfun2(buffer); 
lea eax,[ebp-16]    // Load the address of the buffer into eax. 
mov DWORD PTR [esp],eax  // Set the address as the first argument to myfunc. 
call 0x80483b4 <myfun2>  // Call myfunc. 

// } 
leave 
ret 

這與我期望通常生成C代碼有點不同。在調用函數之前,您通常會將參數推入堆棧,而此代碼已預先在堆棧中創建空間,然後將參數移至堆棧。

爲了更好地理解ebp和esp引用,它有助於構建堆棧的外觀。

 ebp+08 The str parameter 
     ebp+04 The return address 
     ebp+00 The saved copy of ebp   <- ebp 
     ebp-04 Space for buffer 
     ebp-08 Space for buffer 
     ebp-12 Space for buffer 
     ebp-16 Space for buffer 
esp+04 ebp-20 Second function argument 
esp+00 ebp-24 First function argument   <- esp 

當函數被調用時,STR參數壓入堆棧,加調用代碼的返回地址。該函數然後保存ebp的副本,並將ebp設置爲指向堆棧上的該位置。最後它爲緩衝區(16字節)加上空間,並在函數調用中使用兩個參數(另外8個字節)。

+0

非常感謝詹姆斯,這真的有幫助=) – Rebecca

+0

你怎麼知道DWORDPTR [ebp + 8]是str? – Rebecca

+0

更新了我的答案,顯示瞭如何使用堆棧。 –