C + + Linux assembly analysis of the parameters and return values

Source: Internet
Author: User
Tags mul

Note: All are compiled without optimizations. Because as long as open-o1 or-O2, then the assembly code is less pathetic, has been optimized out

Compiler version: x86-64 GCC 5.5

1 Pod type parameters 1.1 one pod parameter, pod return value
int square (int num) {    return num * NUM;} int main () {    int c=90;    int A=square (c);    ++a;}

  

Correspondence Assembly

1.2 Two pod parameters, pod return value
int mul (int v1,int v2) {    return v1 * v2;} int main () {    int c=90;    int A=mul (c,1);    ++a;}

  

When the second parameter is passed into the variable, edx is used, like EAX, to pass in. Then the return value is still returned using EAX.

1.3 A few parameters will be used to upload the stack parameter,
int mul (int v1,int v2,int v3,int v4,int v5,int v6,int v7) {    return v1 * V2*V3*V4*V5*V6*V7;} int main () {    int c1=90;    int c2=10;    int c3=10;    int c4=10;    int c5=10;    int c6=10;    int c7=10;    int A=mul (C1,C2,C3,C4,C5,C6,C7);    ++a;    c1++;c2++;c3++;c4++;c5++;c6++;c7++;    A=mul (A,C2,C3,C4,C5,C6,C7);}

  

As you can see, it is starting with the 7th argument and passing the arguments using the stack.

EDI is used as the first parameter, but EDI is used to daoteng the data when the stack is used.

Note that the stack is first rolled back, and then the return value is saved

Then look at the function call:

2 Structural Body parameters
Class A{public:    A ()    : I1 (1)    {} public    :    int i1;    char A;    int i2;    char CA;    Char C1;    ~a ()    {        i2=2;    }}; int func (A a1,a a2,a a3,a a4,a a5,a a6,a A7) {    a1.a++;    return 1;} void func1 (int i) {    if (i<10)    {    i++;    A A1;    A A2;    A A3;    A A4;    A A5;    A A6;    A A7;    int A=func (A1,A2,A3,A4,A5,A6,A7);    a1.a++;    int Bb=func (A1,A2,A3,A4,A5,A6);    }else{    //     return func1 (i+1);    // }}}

  

First look at the FUNC1 function

Pass the reference

Add a copy constructor:

    A (a& a)    {        ca=++a.ca;    }

  

Then look at the compilation of copy constructors:

That's right. Did you find that object A, the memory in which the parameter is located, is only initialized by the CA member, that is, it has been modified. None of the other data members have been modified.

then cooperate with

This, the stack pointer fallback practice, is not able to understand. Why is the variable inside the function, if not initialized, the value is undefined. Rather than 0.

Because, before using this memory space function, and did not clear that block of memory space 0, but directly sp+8 this form back.

So the subsequent function uses the same memory, then it is undefined!!! Not defined Ah!

Do you understand how it came about?

Why is it that the variables outside of the function do not. Because the memory used by the out-of-function variables is not recycled, there is no reuse. Must be 0. Its internal memory must be 0 AH ~ ~ (then if the other process uses memory that?) The operating system may be 0 clear. This is really not clear.)

Then continue to see the function call

Why above first rsp-8, name push when can automatically do rsp-8.

Why, I don't want to understand. However, when pressing into 8 parameters, that is, 2 parameters require a stack to pass the argument, then it will sp-8*2

Look again at the Func function. change, or some information can not be seen.

int func (A a1,a a2,a a3,a a4,a a5,a a6,a a7,a b) {    b.a++;    a1.a++;    return 1;}

  

Although the compiler is GCC, it is eventually compiled with g++ because of the class.

Therefore, from the above can be seen, in fact, the parameters of the construction of the place, is on the stack, but the function is actually used by the LEA command to take the valid address of the parameters, and then saved in the register, the called function through the Register access parameters.

In other words, there is no value passing. The intent of the value pass is that the parameter is copied using a copy constructor. And then takes its valid address through the register to pass in the called function

What is the meaning of the copy? Is that the value of the original variable is not changed. The parameter that should be passed in is the copy of the constructor. Changes don't matter.

If the 8th parameter is changed to a pointer, what happens:

The first change is that the copy constructor of the call is less once, that is, the 8th parameter is not copied, but the 8th parameter needs to pass the pressure stack to pass the parameter, so you can find the direct stack eighth parameter actual value of the valid address

2.2 Struct type return value

First of all

Class a{public  :    A ()        : I (1)    {    }    a (a& A)    {    }    int i;    int i1;    int i2;}; A func () {    a A;    return A;} void Func1 () {    A B = func ();}

  

This code compilation does not pass:

Why, because the SP pointer is directly rolled back after call ends, the processing of the return value starts only after the SP pointer is rolled back.

That is, if this code can be compiled, then it means that the EAX register stores the valid address of the return value, and the valid address is already under the SP pointer, that is, not in the stack.

In other words, this element is no longer available. Now that it's not available, how can you use an unusable variable to construct the value??

In fact, the problem is that this value is non-const transitive, a after the stack fallback is an anonymous variable, that is, a right value. She is no longer on the stack (under the SP) and therefore cannot be modified.

The copy constructor, inevitably, will carry some of the values of the previous variable, that is, the original variable can be modified. But a is already a right value, (in fact, under the SP), can not be modified. Therefore, the compiler rejects this construct.

However, if you change

Class a{public  :    A ()        : I (1)    {    }    A (const A &a)    {    }    int i;    int i1;    int i2;}; A func () {    a A;    return A;} void Func1 () {    A B = func ();}

  

Compilation can pass, why. Because the const a& is passed in means that the object constructed with this value does not modify the value. Or can not be modified. Therefore, you can use this value to construct other objects.

But the problem is still a, a is no longer on the stack, how to construct?

The answer is to set aside the memory space for the return value, and then pass the address in, constructed in the called function.

3 NOP is what

NOP effect

This, did not read, not very understand. Put a link on it.

C + + Linux assembly analysis of the parameters and return values

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.