简体   繁体   English

分解一个简单的C ++程序

[英]Disassembling a simple C++ program

I have a simple C++ program: 我有一个简单的C ++程序:

#include <iostream>
using namespace std;

int main(){
    string s;
    cin >> s; 
    if (s == "almafa")
        cout << "ok";
}

In gdb I disassembled main and started the program, entered 'testing' as input, and stopped at the comparison 0x0000000000400bb7: 在gdb中,我反汇编main并启动程序,输入“ testing”作为输入,并在比较0x0000000000400bb7处停止:

   0x0000000000400bab <+53>:    lea    -0x40(%rbp),%rax
   0x0000000000400baf <+57>:    mov    $0x400d24,%esi
   0x0000000000400bb4 <+62>:    mov    %rax,%rdi
   0x0000000000400bb7 <+65>:    callq  0x400c6a <bool std::operator==<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*)>

I inspected the esi and rdi registers: 我检查了esi和rdi寄存器:

(gdb) x/s $esi
0x400d24:   "almafa"
(gdb) x/s $rdi
0x7fffffffddc0: "\320\335\377\377\377\177"

What is in the rdi register? rdi寄存器中有什么? I expected it will contain my input. 我希望它将包含我的输入。

rdi is the pointer to your std::string object. rdi是指向std::string对象的指针。

Note that the function call is to an operator== that takes a const char * parameter. 请注意,函数调用是对使用const char *参数的operator==进行的。

The std::string class defines an operator== that takes a const char * parameter, and this is what's being invoked here, with rdi pointing to an in-memory representation of the std::string object, and with the parameter in esi. std::string类定义一个带const char *参数的operator== ,这就是这里要调用的内容,其中rdi指向std::string对象的内存表示形式,并且参数位于esi中。

What is in the rdi register? rdi寄存器中有什么? I expected it will contain my input. 我希望它将包含我的输入。

It is not your input. 这不是您的输入。 Your input was stored in the std::string object, and that's what's being pointed to, here. 您的输入存储在std::string对象中,这就是这里指向的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM