2013-09-29 117 views
0

我已經編譯下面的程序:彙編語言標籤混淆

#include <iostream> 
using namespace std; 

// function declaration 
int max(int num1, int num2); 

int main() 
{ 
    // local variable declaration: 
    int a = 100; 
    int b = 200; 
    int ret; 

    // calling a function to get max value. 
    ret = max(a, b); 

    cout << "Max value is : " << ret << endl; 

    return 0; 
} 

// function returning the max between two numbers 
int max(int num1, int num2) 
{ 
    // local variable declaration 
    int result; 

    if (num1 > num2) 
     result = num1; 
    else 
     result = num2; 

    return result; 
} 

我已經產生下面的彙編語言:

.file "CallFunction.cpp" 
    .def __main; .scl 2; .type 32; .endef 
    .section .rdata,"dr" 
.LC0: 
    .ascii "Max value is : \0" 
    .text 
    .globl main 
    .def main; .scl 2; .type 32; .endef 
    .seh_proc main 
main: 
.LFB969: 
    subq $40 
    leaq .LC0(%rip, %rsp 
    .seh_stackalloc 40 
    .seh_endprologue 
    call __main), %rdx 
    movq .refptr._ZSt4cout(%rip), %rcx 
    call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 
    movl $200, %edx 
    movq %rax, %rcx 
    call _ZNSolsEi 
    movq %rax, %rcx 
    call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ 
    movl $0, %eax 
    addq $40, %rsp 
    ret 
    .seh_endproc 
    .globl _Z3maxii 
    .def _Z3maxii; .scl 2; .type 32; .endef 
    .seh_proc _Z3maxii 
_Z3maxii: 
.LFB970: 
    .seh_endprologue 
    cmpl %edx, %ecx 
    movl %edx, %eax 
    cmovge %ecx, %eax 
    ret 
    .seh_endproc 
    .def _GLOBAL__sub_I_main; .scl 3; .type 32; .endef 
    .seh_proc _GLOBAL__sub_I_main 
_GLOBAL__sub_I_main: 
.LFB980: 
    subq $40, %rsp 
    .seh_stackalloc 40 
    .seh_endprologue 
    leaq _ZStL8__ioinit(%rip), %rcx 
    call _ZNSt8ios_base4InitC1Ev 
    nop 
    addq $40, %rsp 
    ret 
    .seh_endproc 
    .section .ctors,"w" 
    .align 8 
    .quad _GLOBAL__sub_I_main 
    .text 
    .def _GLOBAL__sub_D_main; .scl 3; .type 32; .endef 
    .seh_proc _GLOBAL__sub_D_main 
_GLOBAL__sub_D_main: 
.LFB981: 
    subq $40, %rsp 
    .seh_stackalloc 40 
    .seh_endprologue 
    leaq _ZStL8__ioinit(%rip), %rcx 
    call _ZNSt8ios_base4InitD1Ev 
    nop 
    addq $40, %rsp 
    ret 
    .seh_endproc 
    .section .dtors,"w" 
    .align 8 
    .quad _GLOBAL__sub_D_main 
.lcomm _ZStL8__ioinit,1,1 
    .ident "GCC: (GNU) 4.8.1" 
    .def _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc; .scl 2; .type 32; .endef 
    .def _ZNSolsEi; .scl 2; .type 32; .endef 
    .def _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_; .scl 2; .type 32; .endef 
    .def _ZNSt8ios_base4InitC1Ev; .scl 2; .type 32; .endef 
    .def _ZNSt8ios_base4InitD1Ev; .scl 2; .type 32; .endef 
    .section .rdata$.refptr._ZSt4cout, "dr" 
    .globl .refptr._ZSt4cout 
    .linkonce discard 
.refptr._ZSt4cout: 
    .quad _ZSt4cout 

我期待兩個標籤(每個方法),但有似乎是六。爲什麼有六個?另外,命名約定是如何工作的,即「.LFB969」是什麼意思?

回答

0

優化器已經轉換了您main

int main() 
{ 
    cout << "Max value is : " << 200 << endl; 
    return 0; 
} 

它被允許這樣做,因爲它有完全一樣的效果。

您的兩個功能分別標記爲_Z3maxiimain。其他標籤純粹是爲了編譯器的好處 - 看起來像在每個函數的開頭和結尾處都貼一個標籤。

.Lxxxx標籤是機械生成的(不基於程序中的任何文本);這些名字沒有特別的意義。