Compilation 3-Return and optimize

Source: Internet
Author: User

As mentioned earlier, the function return value is stored in the EAX register, which is then returned and then read from the program. But what if the return value is large?

The blogger intended to use a double test, and later found this to use a completely different set of instructions for the x87. Had to use the structure of the body. Construct C Program

#include <stdio.h>struct myrd{    int i;    Int J;    int k;}; struct Myrd myrdfunc () {    struct myrd myrd1;    myrd1.i = 1;    MYRD1.J = 2;    MYRD1.K = 3;    return myrd1;} int main () {    struct myrd myrd2;    Myrd2 = Myrdfunc ();}

  

Set power off at two function entrances. At the Myrdfunc () entrance (before the call statement), you can see

00bb36e8 8D The FF FF FF Lea EAX,[EBP+FFFFFF18H]00BB36EE push EAX

  

This shows that in order to return a specific area of memory and put the address into eax. Records the value of the EAX. Enter the function. Observe the paragraph

00BB3CDD 8B mov eax,dword ptr [ebp+8]00bb3ce0 8B 4D EC mov ecx,dword ptr [ebp-14h]00bb3ce3] mov dword ptr [EAX] , Ecx00bb3ce5 8B F0 mov edx,dword ptr [Ebp-10h]00bb3ce8-A-vid-mov-DWORD ptr [EAX+4],EDX00BB3CEB 8B 4D F4 mov ecx,dwor D ptr [Ebp-0ch]00bb3cee] mov dword ptr [eax+8],ecx00bb3cf1 8B mov eax,dword ptr [ebp+8]

  

Check the value of the ebp+8, just for EAX. By Ecx,edx, three values are saved to the memory area pointed to by EAX. Finally, the value of ebp+8 is copied back to EAX. The function then returns.

Jump back to main after the first statement is

00bb36f4 C4 Add esp,4

Observe the value of esp+4, just as the EAX is pressed into the stack, this operation will clear the stack.

This is followed by retrieving the return value of the function from the EAX.

00BB36F7 8B mov ecx,dword ptr [eax]00bb36f9 $4D EC mov dword ptr [EBP-14H],ECX00BB36FC 8B. mov edx,dword ptr [EA] X+4]00BB36FF F0 mov dword ptr [ebp-10h],edx00bb3702 8B mov eax,dword ptr [eax+8]00bb3705] F4 mov DWORD PT R [Ebp-0ch],eax

  

Since there is no other action in main, and the lifetime of the local variable is short, do we suppose the code is redundant?

Change Debug to release

At this point debug:

The generator has only two lines, without any redundancy. This shows that the debug and release programs are completely different in use.

Compilation 3-Return and optimize

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.