新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 匯編技術內幕(1)

        匯編技術內幕(1)

        作者: 時間:2016-11-24 來源:網絡 收藏
        最簡單C代碼分析
        為簡化問題,來分析一下最簡的c代碼生成的匯編代碼

        # vi test1.c
        int main()
        {
        return 0;
        }

        編譯該程序,產生二進制文件:
        # gcc test1.c -o test1
        # file test1
        test1: ELF 32-bit LSB executable 80386 Version 1, dynamically linked, not stripped
        test1是一個ELF格式32位小端(Little Endian)的可執行文件,動態鏈接并且符號表沒有去除。
        這正是Unix/Linux平臺典型的可執行文件格式。

        用mdb反匯編可以觀察生成的匯編代碼:
        # mdb test1
        Loading modules: [ libc.so.1 ]
        > main::dis ; 反匯編main函數,mdb的命令一般格式為 <地址>::dis
        main: pushl %ebp;ebp寄存器內容壓棧,即保存main函數的上級調用函數的棧基地址
        main+1: movl %esp,%ebp ; esp值賦給ebp,設置main函數的棧基址main+3:subl $8,%esp
        main+6:andl $0xf0,%esp
        main+9:movl $0,%eax
        main+0xe:subl %eax,%esp
        main+0x10: movl $0,%eax ; 設置函數返回值0
        main+0x15:leave; 將ebp值賦給esp,pop先前棧內的上級函數棧的基地址給ebp,恢復原棧基址
        main+0x16: ret ; main函數返回,回到上級調用

        注:這里得到的匯編語言語法格式與Intel的手冊有很大不同,Unix/Linux采用AT&T匯編格式作為匯編語言的語法格式

        問題:誰調用了 main函數?
        在C語言的層面來看,main函數是一個程序的起始入口點,而實際上,ELF可執行文件的入口點并不是main而是_start。
        mdb也可以反匯編_start:
        > _start::dis ;從_start 的地址開始反匯編
        _start: pushl $0
        _start+2: pushl $0
        _start+4: movl %esp,%ebp
        _start+6: pushl %edx
        _start+7: movl $0x80504b0,%eax
        _start+0xc: testl %eax,%eax
        _start+0xe: je +0xf <_start+0x1d>
        _start+0x10: pushl $0x80504b0
        _start+0x15: call -0x75
        _start+0x1a: addl $4,%esp
        _start+0x1d: movl $0x8060710,%eax
        _start+0x22: testl %eax,%eax
        _start+0x24: je +7 <_start+0x2b>
        _start+0x26: call -0x86
        _start+0x2b: pushl $0x80506cd
        _start+0x30: call -0x90
        _start+0x35: movl +8(%ebp),%eax
        _start+0x38: leal +0x10(%ebp,%eax,4),%edx
        _start+0x3c: movl %edx,0x8060804
        _start+0x42: andl $0xf0,%esp
        _start+0x45: subl $4,%esp
        _start+0x48: pushl %edx
        _start+0x49: leal +0xc(%ebp),%edx
        _start+0x4c: pushl %edx
        _start+0x4d: pushl %eax
        _start+0x4e: call +0x152 <_init>
        _start+0x53: call -0xa3 <__fpstart>
        _start+0x58: call +0xfb
        ;在這里調用了main函數
        _start+0x5d: addl $0xc,%esp
        _start+0x60: pushl %eax
        _start+0x61: call -0xa1
        _start+0x66: pushl $0
        _start+0x68: movl $1,%eax
        _start+0x6d: lcall $7,$0
        _start+0x74: hlt


        關鍵詞: 匯編技術匯編代

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 珲春市| 精河县| 浮梁县| 余江县| 鄂州市| 长丰县| 灵璧县| 鄂伦春自治旗| 德令哈市| 海门市| 岳普湖县| 积石山| 正定县| 香港| 许昌县| 枝江市| 涟水县| 灌南县| 广西| 万盛区| 大安市| 旬邑县| 慈利县| 崇文区| 汉川市| 丰镇市| 阆中市| 台中县| 常山县| 铜川市| 定襄县| 丹江口市| 玛多县| 通河县| 进贤县| 镇原县| 佳木斯市| 洛浦县| 金寨县| 长泰县| 桦甸市|