C Tip: Compilation code for VC function calls

Source: Internet
Author: User
Mainly talk about VC inside the function call assembly into the assembly code of the case, first for a small program before, say VC compiler optimization.
Example program:
#include <iostream>
using namespace Std;
int main (int argc, char* argv[])
{
int i=10;
int a = i;
cout << "i=" << a << Endl;
The function of the following assembly statement is to change the value of I in memory, but it does not let the compiler know
__asm
{
mov dword ptr [ebp-4], 20h
}
int b = i;
cout << "i=" << b << Endl;
return 0;
}
This code is very concise, but the assembly code is not concise, first look at the release mode of the assembly code Overview:
......
; 14:
; 15://The function of the assembler statement is to change the value of I in memory, but does not let the compiler know
; : __asm
; 17: {
; 18:mov DWORD ptr [ebp-4], 20h
mov DWORD PTR [ebp-4], 32; 00000020H
; 19:}
; 20:int B = i;
; 21:cout << "i=" << b << Endl;
Push 10; 0000000aH
Push OFFSET FLAT:?? _c@_02hdok@i $DN? $AA @; ' String '
Push OFFSET flat:?cout@std@@3v? $basic _ostream@du? $char _traits@d@std@@@1@a; Std::cout
Call?? 6std@ @YAAAV? $basic _ostream@du $char _traits@d@std@@@0@aav10@pbd@z; std::operator<<
Add ESP, 8
mov ecx, eax
Call?? 6. $basic _ostream@du $char _traits@d@std@@ zzfcthotfixz @ @QAEAAV01 @h@z; Std::basic_ostream<char,std::char_traits<char> >::operator<<
mov esi, eax
Push 10; 0000000aH
mov ecx, ESI
Call put@? $basic _ostream@du $char _traits@d@std@@ zzfcthotfixz @ @QAEAAV12 @d@z; std::basic_ostream<char,std::char_traits<char>;::p UT
mov ecx, DWORD PTR [esi]
XOR EDI, EDI
......
Call cout front, directly a push 10, which is the function of the pre-pressure parameters of the process, the pressure of a constant in the inside, hehe, actually I has been modified, but the compiler does not know that I is still 10, Gu did the optimization, the test, the big hint pressure the constant in the inside.
Then look at the assembly code in debug mode:
: __asm
17: {
18:mov DWORD ptr [ebp-4], 20h
004017DE mov dword ptr [ebp-4],20h
19:}
20:int B = i;
004017E5 mov edx,dword ptr [ebp-4]
004017E8 mov dword ptr [Ebp-0ch],edx
21:cout << "i=" << b << Endl;
004017EB Push offset @ILT +195 (Std::endl) (004010C8)
004017F0 mov eax,dword ptr [ebp-0ch]
004017F3 push EAX
004017F4 Push offset string "i=" (0046c01c)
004017F9 Push offset std::cout (00477A10)
004017FE call @ILT +640 (std::operator<<) (00401285)
00401803 Add esp,8
00401806 mov ecx,eax
b = I, the sentence is assigned from the address of I to take the value sent to B,
mov Edx,dword ptr [ebp-4]
mov DWORD ptr [Ebp-0ch],edx
Note that the release version is not the two sentences, so now the B value is definitely correct, the back of the pressure parameter, also put the value of the B address point to the stack, hehe, this is also said before why the two version of the results of different operating reasons.
To modify this program slightly, we begin the following function call assembly analysis:
#include <iostream>
using namespace Std;
int changnum (int, int);
int main (int argc, char* argv[])
{
int i=10;
int a = i;
cout << "i=" << a << Endl;
The function of the following assembly statement is to change the value of I in memory, but it does not let the compiler know
__asm
{
mov dword ptr [ebp-4], 20h
}
int b = i;
cout << "i=" << b << Endl;
Changnum (50, 100);
return 0;
}
int changnum (int nparam, int nW)
{
int i = 10;
int a = i;
cout << "i=" << a << Endl;
__asm
{
mov dword ptr [ebp-4], 20h
mov dword ptr [EBP +], 0h
}
int b = i;
cout << "i=" << b << Endl;
return 0;

}


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.