Function call and Function point -- assemble and C

這一篇主要看C的function call 和 function point在gcc產生出來的assemble code
上是有什麼不同。
 
OS   : Ubuntu x86_64
GCC : 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)

1. C Code

下面分別列出Function Call跟Function Point的C Code的例子,如果懂的話就可以
直接跳過這部份。因為真的很簡單 :D 。

-- Function Call --
#include <stdio.h>
#include <stdlib.h>

void test()
{
    printf("Hello World\n");
}

int main(int argc, const char *argv[])
{
    test();
    return 0;
}

-- Function Point --
#include <stdio.h>
#include <stdlib.h>

void test()
{
    printf("Hello World\n");
}

int main(int argc, const char *argv[])
{
    void (*pfunc)() = {test};
    pfunc();

    return 0;
}


2. Assemble


這邊Assemble code是用了gcc最佳化的參數 -O0,意思就是沒有最佳化啦。
從圖的左邊136行是Function Call從這部份來看assemble是直接用callq來呼叫test
這個function

而Function Point步驟就比較多了。從圖右邊135行開始,先將test的位址(0x40052C)
配置到pfunc變數( -0x8(%rbp) )上,然後再將pfunc變數的內容配置到rdx這個register上,
接著在用callq來呼叫rdx所指到的test這個function。



















3. 最佳化

如果你gcc的參數下了 -O3(最佳化提升到Level 3)就可以從下圖看到compiler很聰明
知道這兩個程式其實是作同一件事,所以他直接用callq來呼叫test
 


 

留言

熱門文章