c++ - 与C++和汇编相关,什么是ebp+8?

标签 c++ assembly x86 g++ reverse-engineering

我有以下 C++ 代码:

#include <tuple>
std::tuple<int, bool> foo()
{
    return std::make_tuple(128, true);
}
int main()
{
    auto result = foo();
}

以下是 foo() 函数的反汇编版本:

push    ebp
mov     ebp, esp
sub     esp, 24
mov     BYTE PTR [ebp-13], 1  // second argument
mov     DWORD PTR [ebp-12], 128 // first argument
mov     eax, DWORD PTR [ebp+8] // what is this? why we need this here?
sub     esp, 4
lea     edx, [ebp-13]   
push    edx                   // second
lea     edx, [ebp-12]
push    edx                   // first
push    eax                  // same as "ebp+8", what is this?
call    std::tuple<std::__decay_and_strip<int>::__type, std::__decay_and_strip<bool>::__type> std::make_tuple<int, bool>(int&&, bool&&)
add     esp, 12
mov     eax, DWORD PTR [ebp+8]
leave
ret     4

据我所知,ebp+X 用于访问函数参数,但foo 没有这样的东西,那么编译器为什么要使用它呢? 它似乎是 std::make_tuple() 的第一个参数。

编辑:

我不是在使用优化,我只是想学习 RE。

Assembly 的主要部分:

lea     eax, [ebp-16]  // loaction of local variable
sub     esp, 12
push    eax           // as hidden argument for foo
call    foo()
add     esp, 12

最佳答案

调用约定指定通过作为参数传递的隐藏指针返回非平凡对象。这就是你所看到的。从技术上讲,您的代码是这样实现的:

std::tuple<int, bool>* foo(std::tuple<int, bool>* result)
{
    *result = std::make_tuple(128, true);
    return result;
}
int main()
{
    std::tuple<int, bool> result;
    foo(&result);
}

关于c++ - 与C++和汇编相关,什么是ebp+8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41451618/

相关文章:

c++ - 无符号整数转换为小端形式

c++ - 使用字符串文字在构造函数中初始化 std::array<char,x> 成员。海湾合作委员会错误?

c++ - 虚拟成员函数和 std::tr1::function:这是如何工作的?

gcc - 内联汇编错误,阻止 gcc 编译尝试

c++ - 使用 cin 关闭 while 循环

c - gcc 堆栈优化?

assembly - 调用堆栈如何工作?

assembly - 英特尔 AVX2 组装开发

c++ - valgrind 未处理的指令字节 : 0xF 0xB 0xFF 0x85

assembly - 链接器如何找到主函数?