x86 - 为什么编译器会在下一条指令中插入看似毫无意义的JMP?

Bob*_*Bob 2 c++ x86 assembly

我正在尝试学习C++和x86之间的关系,并且一直在研究一些C++代码的相应汇编指令.引起我注意的是,下一条指令似乎有多个JMP.这不仅仅是浪费空间和时钟周期吗?

源代码非常简单,只是来自教科书的简单游戏.

// Lost Fortune
// A personalized adventure!

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    const int GOLD_PIECES = 900;
    int adventurers, killed, survivors;
    string leader;

    // Get info.
    cout << "Welcome fo Lost Fortune\n\n";
    cout << "Please enter the following for your personalized adventure\n";

    cout << "Enter a number: ";
    cin >> adventurers;

    cout << "Enter a number, smaller than the first: ";
    cin >> killed;

    survivors = adventurers - killed;

    cout << "Enter your last name: ";
    cin >> leader;


    // Tell story.
    cout << "\nA brave group of " << adventurers << " set out on a quest ";
    cout << "-- in search of the lost treasure of the Ancient Dwarves. ";
    cout << "The group was led by that legendary rogue,  " << leader << ".\n";
    cout << "\n Along the way, a bang of marauding ogres ambushed the party. ";
    cout << "All fought bravely under the command of " << leader;
    cout << ", and the ogres were defeated, but at a cost. ";
    cout << "Of the adventurers, " << killed << " were vanquished, ";
    cout << "leaving just " << survivors << " in the group.\n";

    cout << "\nThe party was about to give up all hope. ";
    cout << "But while laying the deceased to rest, ";
    cout << "they stumbled upon the buried fortune. ";
    cout << "So the adventurers split " << GOLD_PIECES << " gold pieces. ";
    cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
    cout << " pieces to keep things fair of course.\n";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

组装的摘录如下.请注意,有几个JMP转到下一条指令,地址为00002053,00002076等.如果有帮助,这些JMP的操作码为E9 00 00 00 00.

00002035         mov        eax, dword [ebp+var_64]                             ; CODE XREF=_main+148
00002038         lea        ecx, dword [eax-0x1f91+0x3c60]                      ; "Welcome fo Lost Fortune\\n\\n"
0000203e         mov        edx, esp
00002040         mov        dword [edx+4], ecx
00002043         mov        ecx, dword [eax-0x1f91+__ZNSt3__14coutE_400c]       ; __ZNSt3__14coutE_400c
00002049         mov        dword [edx], ecx
0000204b         call       imp___symbol_stub___ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc ; std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*)
00002050         mov        dword [ebp+var_74], eax
00002053         jmp        loc_2058

             loc_2058:
00002058         mov        eax, dword [ebp+var_64]                             ; CODE XREF=_main+211
0000205b         lea        ecx, dword [eax-0x1f91+0x3c7a]                      ; "Please enter the following for your personalized adventure\\n"
00002061         mov        edx, esp
00002063         mov        dword [edx+4], ecx
00002066         mov        ecx, dword [eax-0x1f91+__ZNSt3__14coutE_400c]       ; __ZNSt3__14coutE_400c
0000206c         mov        dword [edx], ecx
0000206e         call       imp___symbol_stub___ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc ; std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*)
00002073         mov        dword [ebp+var_78], eax
00002076         jmp        loc_207b

             loc_207b:
0000207b         mov        eax, dword [ebp+var_64]                             ; CODE XREF=_main+246
0000207e         lea        ecx, dword [eax-0x1f91+0x3cb6]                      ; "Enter a number: "
00002084         mov        edx, esp
00002086         mov        dword [edx+4], ecx
00002089         mov        ecx, dword [eax-0x1f91+__ZNSt3__14coutE_400c]       ; __ZNSt3__14coutE_400c
0000208f         mov        dword [edx], ecx
00002091         call       imp___symbol_stub___ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc ; std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*)
00002096         mov        dword [ebp+var_7C], eax
00002099         jmp        loc_209e

             loc_209e:
0000209e         mov        eax, esp                                            ; CODE XREF=_main+281
000020a0         lea        ecx, dword [ebp+var_44]
000020a3         mov        dword [eax+4], ecx
000020a6         mov        ecx, dword [ebp+var_64]
000020a9         mov        edx, dword [ecx-0x1f91+__ZNSt3__13cinE_4008]        ; __ZNSt3__13cinE_4008
000020af         mov        dword [eax], edx
000020b1         call       imp___symbol_stub___ZNSt3__113basic_istreamIcNS_11char_traitsIcEEErsERi ; std::__1::basic_istream<char, std::__1::char_traits<char> >::operator>>(int&)
000020b6         mov        dword [ebp+var_80], eax
000020b9         jmp        loc_20be
Run Code Online (Sandbox Code Playgroud)

为什么编译器会这样做?任何帮助是极大的赞赏 :)

Jes*_*uhl 5

在没有优化的情况下构建时,编译器的优先级是生成易于调试的代码,并尽可能地匹配原始源代码,而不是快速运行的代码.

在优化(例如使用-O3)时,编译器的工作就是使代码快速运行,并且不会JMP像你观察到的那样做愚蠢的事情.

优化的代码可能运行得更快,但调试非常困难.因此,如果您打算调试代码以查找错误,那么未经优化的版本几乎总是更可取.

  • 这一切都是真的,但我认为它没有回答这个问题 - 为什么首先插入跳跃? (2认同)
  • 优化的代码不难调试.它只需要一点点习惯. (2认同)