Compilation Analysis of VC ++ code (1), compilation Analysis of vc code

Source: Internet
Author: User

Compilation Analysis of VC ++ code (1), compilation Analysis of vc code

VC ++ code is a high-level language closest to assembly instructions. To better understand many technical concepts and compiler parameters involved in VC ++ coding, analyzing and interpreting assembly instructions helps developers to understand the true meaning of many concepts and technologies in advanced languages more accurately, intuitively, and profoundly, it has very practical value for program optimization and coding. As there is a lot of content, I will break it down into many chapters to explain examples.

Start from the main entry and use the ancient VC6.0 compiler for compilation. First, start from the simplest example to gradually expand, so that you can get started.

VC ++ source code:

Int main (int argc, char * argv [])

{
Printf ("Hello World! \ N ");
Return 0;
}

The simplest code. What is the assembly code? You can read the 32-bit X86 register online first.

No optimized assembly code in the debug status:

Int main (int argc, char * argv [])
20 :{
00401030 55 push ebp = save EBP
00401031 8B EC mov ebp, esp = save ESP
00401033 83 EC 40 sub esp, 40 h = when no temporary variables are defined, 64 bytes of stack memory space is reserved by default.
00401036 53 push ebx = save EBX
00401037 56 push esi = save ESI
00401038 57 push edi = save edi
00401039 8D 7D C0 lea edi, [ebp-40h] = put the top stack address in EDI
0040103C B9 10 00 00 00 mov ecx, 10 h = counter 16 put into ECX
00401041 B8 CC mov eax, 0 CCCCCCCCh === the initial value is directed to the INT3 address
00401046 F3 AB rep stos dword ptr [edi] === initialize 16 DWORD stack Spaces
26: printf ("Hello World! \ N ");
00401048 68 1C 20 42 00 push offset string "Hello World! \ N "(0042201c) = add string parameter addresses to the stack
0040104D E8 EE 00 00 call printf (00401140) = call a function
00401052 83 C4 04 add esp, 4 = recover ESP
27: return 0;
00401055 33 C0 xor eax, eax = EAX cleared
28 :}
00401057 5F pop edi = restore EDI
00401058 5E pop esi = restore ESI
00401059 5B pop ebx = restore EBX
0040105A 83 C4 40 add esp, 40 h = recover ESP
0040105D 3B EC cmp ebp, esp = check ESP and EBP
0040105F E8 5C 01 00 call _ chkesp (004011c0) = compare the check results. If not equal, an exception is thrown.
00401064 8B E5 mov esp, ebp = recover esp
00401066 5D pop ebp = restore ebp
00401067 C3 ret

Summary: The above Assembly command sequence mode is actually a typical function call process. Simply put, it is to save the stack pointer, save the Register, and open up the stack space and initialize according to the declaration of the temporary variables, press the stack to pass parameters, call functions, recover registers, recover stack pointers, and exit the program.

If you use a later version of VC for compiling, the default stack size will be different. If the size of the stack is 64-bit, the difference will be greater. We will discuss it later.

Next, I will add more content to the program to demonstrate the underlying implementation process of the corresponding assembly code.

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.