新聞中心

        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


        關鍵詞: 匯編技術匯編代

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 锡林浩特市| 景德镇市| 镇远县| 嫩江县| 清徐县| 冕宁县| 定襄县| 汉源县| 当雄县| 文化| 泰来县| 涪陵区| 威远县| 和顺县| 洮南市| 龙江县| 闻喜县| 岳普湖县| 庆阳市| 资中县| 湖北省| 陈巴尔虎旗| 扶余县| 西和县| 理塘县| 太仓市| 屏东市| 东明县| 丰台区| 罗江县| 阿拉善左旗| 双流县| 连南| 信丰县| 普格县| 临夏市| 阿拉善右旗| 高密市| 周宁县| 文成县| 宁国市|