我编写了以下程序:
#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 :(得分:0)
优化工具已将您的main
转换为
int main ()
{
cout << "Max value is : " << 200 << endl;
return 0;
}
允许这样做,因为它具有完全相同的效果。
您的两个功能标记为_Z3maxii
和main
。其他标签纯粹是为了编译器的好处 - 看起来它在每个函数的开头和结尾都有一个。
.Lxxxx标签是机械生成的(不基于程序中的任何文本);这些名字没什么特别的意义。