in Education by
I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, uint64_t b) { a_ = a; b_ = b; } uint64_t a_, b_; }; When I look at the compiler output (x86-64 gcc 8.3 and x86-64 clang 8.0.0, on 64-bit Linux), with no optimizations enabled, I see the following code for the constructor. x86-64 gcc 8.3: X::X(unsigned long, unsigned long): push rbp mov rbp, rsp mov QWORD PTR [rbp-8], rdi mov QWORD PTR [rbp-16], rsi mov QWORD PTR [rbp-24], rdx mov rax, QWORD PTR [rbp-8] mov QWORD PTR [rax], 0 mov rax, QWORD PTR [rbp-8] mov QWORD PTR [rax+8], 0 mov rax, QWORD PTR [rbp-8] mov rdx, QWORD PTR [rbp-16] mov QWORD PTR [rax+8], rdx mov rax, QWORD PTR [rbp-8] mov rdx, QWORD PTR [rbp-24] mov QWORD PTR [rax], rdx nop pop rbp ret x86-64 clang 8.0.0: X::X(unsigned long, unsigned long): push rbp mov rbp, rsp mov qword ptr [rbp - 8], rdi mov qword ptr [rbp - 16], rsi mov qword ptr [rbp - 24], rdx mov rdx, qword ptr [rbp - 8] mov qword ptr [rdx], 0 mov qword ptr [rdx + 8], 0 mov rsi, qword ptr [rbp - 16] mov qword ptr [rdx + 8], rsi mov rsi, qword ptr [rbp - 24] mov qword ptr [rdx], rsi pop rbp ret Does anyone know why the output is so complex? I would have expected two simple "mov" statements, even with no optimizations enabled. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Un-optimized code always stores all C++ variables (including function args) into their memory location between statements, so that the values are available for the debugger to read and even modify. (And because it didn't spend any time doing register allocation.) This includes storing register args to memory before the first C++ statement of a function. This is Intel-syntax assembly like from gcc -masm=intel, so it's using destination, source order. (We can tell based on using PTR, square brackets, and lack of % on register names.) The first 3 stores are the function arguments (this, a, b) that were passed in registers RDI, RSI, and RDX as per the x86-64 System V ABI's calling convention. mov QWORD PTR [rbp-8], rdi # this mov QWORD PTR [rbp-16], rsi # a mov QWORD PTR [rbp-24], rdx # b Now it is loading this into rax and writing zeros into a_ and b_ because you did not use proper constructor initialization. Or possibly you added initialization to zero with some code you did not show here, or an odd compiler option. mov rax, QWORD PTR [rbp-8] mov QWORD PTR [rax], 0 # this->a_ = 0 mov rax, QWORD PTR [rbp-8] mov QWORD PTR [rax+8], 0 # this->b_ = 0 Then it loads this into rax again and a into rdx, then writes this->a_ with rdx aka a. Same again for b. Wait, actually that has to be a write to b_ first then a write into a_ because structs are required to match declaration and memory order. So [rax+8] has to be b_, not a_. mov rax, QWORD PTR [rbp-8] mov rdx, QWORD PTR [rbp-16] # reload a mov QWORD PTR [rax+8], rdx # this->b_ = a mov rax, QWORD PTR [rbp-8] mov rdx, QWORD PTR [rbp-24] # reload b mov QWORD PTR [rax], rdx # this->a_ = b So your asm doesn't match the C++ source in your question.

Related questions

0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    In a simple linear regression model (One independent variable), If we change the input variable by 1 unit. How ... of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    It is common practice to declare stack variables (variables allocated on the excecuting stack rather than dynamically ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to understand the usage of private const in the class. My understanding is that private const is used to make ... (void) { int const MyExample::x = 3; std::cout...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    When compiling boost filesystem (1_46_1) with Intel 12 Release 4, and Visual Studio 10, I get this error ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    What does a compiler do? It translates source code into activities (which may comprise groups of machine instructions) ... only enables the programmer to edit the program files....
asked May 1, 2021 in Education by JackTerrance
0 votes
    I'm trying to create two dimensional std::vector, that will hold objects of this class I inherited. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I'm looking for a nice and simple GUI Toolkit that I can use with MSVC++ 2008. I need something ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm looking for a nice and simple GUI Toolkit that I can use with MSVC++ 2008. I need something ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
...