使用 gcc 命令把C語言程序反匯編


之前看過一點匯編,不過現在都忘記得差不多了。最近又很蛋疼地想起反匯編這個東西。這里使用 gcc 命令對 .c 文件進行反匯編,把 C語言 翻譯成匯編語言

先准備一個簡單的 C 程序

sum.c

#include <stdio.h>

int add(int, int);
int mode(int, int);

int main()
{
    int a = 3, b = 2;
    int s = add(3, 2);
    int m = mode(3, 2);
    return 0;    
}

int add(int a, int b)
{
    return a + b;
}

int mode(int a, int b)
{
    return a % b;
}

在控制台中先進到 保存 sum.c 的文件夾下,當然你也可以用絕對路徑,使用以下命令

gcc -S sum.c -o sum_at.s

這個命令默認生成的是 AT&T 匯編,生成的 sum_at.s 如下

    .file    "sum.c"
    .text
    .def    ___main;    .scl    2;    .type    32;    .endef
    .globl    _main
    .def    _main;    .scl    2;    .type    32;    .endef
_main:
LFB13:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $32, %esp
    call    ___main
    movl    $3, 28(%esp)
    movl    $2, 24(%esp)
    movl    $2, 4(%esp)
    movl    $3, (%esp)
    call    _add
    movl    %eax, 20(%esp)
    movl    $2, 4(%esp)
    movl    $3, (%esp)
    call    _mode
    movl    %eax, 16(%esp)
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE13:
    .globl    _add
    .def    _add;    .scl    2;    .type    32;    .endef
_add:
LFB14:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    movl    8(%ebp), %edx
    movl    12(%ebp), %eax
    addl    %edx, %eax
    popl    %ebp
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE14:
    .globl    _mode
    .def    _mode;    .scl    2;    .type    32;    .endef
_mode:
LFB15:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    movl    8(%ebp), %eax
    cltd
    idivl    12(%ebp)
    movl    %edx, %eax
    popl    %ebp
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE15:
    .ident    "GCC: (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 7.3.0"

 

我之前看的是intel匯編 也就是8086這種 intel 芯片,要轉成 intel 匯編 使用以下命令

gcc -S -masm=intel sum.c -o sum_intel.s

生成的 sum_intel.s 文件內容如下

    .file    "sum.c"
    .intel_syntax noprefix
    .text
    .def    ___main;    .scl    2;    .type    32;    .endef
    .globl    _main
    .def    _main;    .scl    2;    .type    32;    .endef
_main:
LFB13:
    .cfi_startproc
    push    ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    mov    ebp, esp
    .cfi_def_cfa_register 5
    and    esp, -16
    sub    esp, 32
    call    ___main
    mov    DWORD PTR [esp+28], 3
    mov    DWORD PTR [esp+24], 2
    mov    DWORD PTR [esp+4], 2
    mov    DWORD PTR [esp], 3
    call    _add
    mov    DWORD PTR [esp+20], eax
    mov    DWORD PTR [esp+4], 2
    mov    DWORD PTR [esp], 3
    call    _mode
    mov    DWORD PTR [esp+16], eax
    mov    eax, 0
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE13:
    .globl    _add
    .def    _add;    .scl    2;    .type    32;    .endef
_add:
LFB14:
    .cfi_startproc
    push    ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    mov    ebp, esp
    .cfi_def_cfa_register 5
    mov    edx, DWORD PTR [ebp+8]
    mov    eax, DWORD PTR [ebp+12]
    add    eax, edx
    pop    ebp
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE14:
    .globl    _mode
    .def    _mode;    .scl    2;    .type    32;    .endef
_mode:
LFB15:
    .cfi_startproc
    push    ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    mov    ebp, esp
    .cfi_def_cfa_register 5
    mov    eax, DWORD PTR [ebp+8]
    cdq
    idiv    DWORD PTR [ebp+12]
    mov    eax, edx
    pop    ebp
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE15:
    .ident    "GCC: (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 7.3.0"

等我復習下匯編再回來看這段代碼到底干了什么

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM