C++:lamda:汇编查看实现

  • 时间:
  • 浏览:
  • 来源:互联网

文章目录

  • 代码
  • 汇编

代码

nclude <iostream>
using namespace std;
int main()
{
int b = 9;
auto d = [](int a){cout<<"a="<<a<<endl;};
auto c = [&](int a){cout<<"a="<<a<<"b="<<b<<endl;};
d(3);
c(4);
}

汇编

这个也会生成一个汇编函数。带入的参数。如果是捕获引用,就会将引用地址通过 rax 传递栈地址,到lamda函数里,使用。
lamda函数名包含:父函数名::{lamda(返回类型)编号}::operator()(参数类型)const

0000000000400876 <main::{lambda(int)#1}::operator()(int) const>:
  400876:       55                      push   %rbp
  400877:       48 89 e5                mov    %rsp,%rbp
  40087a:       48 83 ec 10             sub    $0x10,%rsp
  40087e:       48 89 7d f8             mov    %rdi,-0x8(%rbp)
  400882:       89 75 f4                mov    %esi,-0xc(%rbp)
  400885:       be 49 0a 40 00          mov    $0x400a49,%esi
  40088a:       bf 60 10 60 00          mov    $0x601060,%edi
  40088f:       e8 ac fe ff ff          callq  400740 <basic_ostream<char, char_traits<char> >& operator<< <char_traits<char> >(basic_ostream<char, char_traits<char> >&, char const*)@plt>
s 400894:       48 89 c2                mov    %rax,%rdx
  400897:       8b 45 f4                mov    -0xc(%rbp),%eax
  40089a:       89 c6                   mov    %eax,%esi
  40089c:       48 89 d7                mov    %rdx,%rdi
  40089f:       e8 cc fe ff ff          callq  400770 <std::ostream::operator<<(int)@plt>
  4008a4:       be 20 07 40 00          mov    $0x400720,%esi
  4008a9:       48 89 c7                mov    %rax,%rdi
  4008ac:       e8 9f fe ff ff          callq  400750 <std::ostream::operator<<(std::ostream& (*)(std::ostream&))@plt>
  4008b1:       90                      nop
  4008b2:       c9                      leaveq
  4008b3:       c3                      retq
00000000004008b4 <main::{lambda(int)#2}::operator()(int) const>:
  4008b4:       55                      push   %rbp
  4008b5:       48 89 e5                mov    %rsp,%rbp
  4008b8:       48 83 ec 10             sub    $0x10,%rsp
  4008bc:       48 89 7d f8             mov    %rdi,-0x8(%rbp)
  4008c0:       89 75 f4                mov    %esi,-0xc(%rbp)
  4008c3:       be 49 0a 40 00          mov    $0x400a49,%esi
  4008c8:       bf 60 10 60 00          mov    $0x601060,%edi
  4008cd:       e8 6e fe ff ff          callq  400740 <basic_ostream<char, char_traits<char> >& operator<< <char_traits<char> >(basic_ostream<char, char_traits<char> >&, char const*)@plt>
s 4008d2:       48 89 c2                mov    %rax,%rdx   将传进来的引用地址放到,rdx 
  4008d5:       8b 45 f4                mov    -0xc(%rbp),%eax 
  4008d8:       89 c6                   mov    %eax,%esi
  4008da:       48 89 d7                mov    %rdx,%rdi   将引用放到第一个参数
  4008dd:       e8 8e fe ff ff          callq  400770 <std::ostream::operator<<(int)@plt>
  4008e2:       be 4c 0a 40 00          mov    $0x400a4c,%esi
  4008e7:       48 89 c7                mov    %rax,%rdi
  4008ea:       e8 51 fe ff ff          callq  400740 <std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)@plt>
  4008ef:       48 89 c2                mov    %rax,%rdx
  4008f2:       48 8b 45 f8             mov    -0x8(%rbp),%rax
  4008f6:       48 8b 00                mov    (%rax),%rax
  4008f9:       8b 00                   mov    (%rax),%eax
  4008fb:       89 c6                   mov    %eax,%esi
  4008fd:       48 89 d7                mov    %rdx,%rdi
  400900:       e8 6b fe ff ff          callq  400770 <std::ostream::operator<<(int)@plt>
  400905:       be 20 07 40 00          mov    $0x400720,%esi
  40090a:       48 89 c7                mov    %rax,%rdi
  40090d:       e8 3e fe ff ff          callq  400750 <std::ostream::operator<<(std::ostream& (*)(std::ostream&))@plt>
  400912:       90                      nop
  400913:       c9                      leaveq
  400914:       c3                      retq

0000000000400915 <main>:
  400915:       55                      push   %rbp
  400916:       48 89 e5                mov    %rsp,%rbp
  400919:       48 83 ec 10             sub    $0x10,%rsp
  40091d:       c7 45 fc 09 00 00 00    movl   $0x9,-0x4(%rbp)   变量 b;9 
  400924:       48 8d 45 fc             lea    -0x4(%rbp),%rax  变量b的地址放到rax
  400928:       48 89 45 f0             mov    %rax,-0x10(%rbp)  变量的地址放到栈  0x100x8
  40092c:       48 8d 45 fb             lea    -0x5(%rbp),%rax   rbp - 5 放到rax 为什么要在栈上分配一个内存传递过去,因为只有一个参数。
  400930:       be 03 00 00 00          mov    $0x3,%esi    第二个参数
  400935:       48 89 c7                mov    %rax,%rdi    第一个参数
  400938:       e8 39 ff ff ff          callq  400876 <main::{lambda(int)#1}::operator()(int) const>
  40093d:       48 8d 45 f0             lea    -0x10(%rbp),%rax  将要捕获的引用放到rax
  400941:       be 04 00 00 00          mov    $0x4,%esi
  400946:       48 89 c7                mov    %rax,%rdi
  400949:       e8 66 ff ff ff          callq  4008b4 <main::{lambda(int)#2}::operator()(int) const>
  40094e:       b8 00 00 00 00          mov    $0x0,%eax
  400953:       c9                      leaveq
  400954:       c3                      retq

本文链接http://smartadmin.com.cn/smartadmin/show-16570.html