2012-11-13 53 views
5

我試圖進入ASM的概念,並同時觀察由MSVC產生的分解可以去看東西,我不能完全理解。這裏是我的測試案例:MSVC編譯器行爲是用來

#include <tchar.h> 
#include <conio.h> 
#include <iostream> 
using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int v1 = 1; 
    int v2 = 2; 
    int v3 = 3; 
    int v4 = 4; 
    int v5 = 5; 
    int v6 = 6; 
    int v7 = 7; 
    int v8 = 8; 
    int v9 = 9; 
    int v10 = 10; 
    int v11 = 11; 
    int v12 = 12; 
    int v13 = 13; 
    int v14 = 14; 
    int v15 = 15; 
    int v16 = 16; 

    int sum = v1+v2 * (v3+v4 * (v5+v6 * (v7+v8 * (v9+v10 * (v11+v12 * (v13+v14 * (v15+v16))))))); 

    _getch(); 
    return 0; 
} 

產生類似:

mov eax, v1 
    mov edx, v3 
    mov ecx, v5 
    mov ebx, v7 
    mov esi, v9 
    mov edi, v11 // all 6 available registers are filled 
    mov dword ptr [ebp-60h), eax // free eax <<<<<< 
    mov eax, v15 // fill it again 
    add eax, v16 
    imul eax, v12 
    (...) 

所以,我的問題是: 什麼編譯器做在標有 「< < < < < <」 行?我的 猜測是它創建了一個變量來存儲寄存器值 英寸看起來它是在堆棧上,因爲它使用ebp,但它是 東西像一個「全局變量」,或者它是一個變量在當前 範圍(框架)只?

在此先感謝。

+2

你應該看看*寄存器溢出*,這是大多數編譯器手柄怎麼跑出來的自由寄存器(也有很多情況下這一點,取決於優化的水平,要生成的代碼,如果SSA使用等)。 – Necrolis

+0

看起來像「註冊溢出」是我需要的術語。謝謝。 – benji

回答

4

MSVC將波及寄存器到適當的存儲器:在堆棧時,它的溢出該保持與塊範圍的變量的寄存器,或當寄存器保持的全球的固定偏移。在任何時候,編譯器都知道哪些變量在哪個寄存器中。

你不能灑局部變量全局內存,因爲可能存在的不可預知號碼當中,由於線程和重入。編譯器可能會暫時將全局變量溢出到堆棧槽中,但在存在線程時這很複雜。

+0

啊,現在有道理。非常感謝。 – benji