First little research Compilation

Source: Internet
Author: User
Tags emit
I tried to use the rdtsc command of Intel CPU to count the program. I checked the relevant information and found that the rdtsc command under GCC could not obtain the correct result, so I want to see how C's code is compiled into an assembly:

In GCC, G ++-O main. s main. cpp-S
Microsoft's Cl compiler is like this: cl.exe/Fas main. cpp

The compilation produced by GCC is in at&t format, while Microsoft is in Intel format. From the annotation point of view, the compilation generated by Microsoft Cl is much more intimate, and the source code and line number information are all in it, which is clear at a glance.

Of course, the compilation cannot be understood. For example, if rdtsc is used in cl, why does the result returned by the command be stored in edX: eax and finally can be used as the return value of the function? It seems that I am a pure idiot in the compilation field !!! This is my future direction.

OK. Finally, paste the code called by rdtsc that can be used in both Cl and GCC:/*** // call the Intel CPU command rdtsc to obtain the time count, easy to obtain performance metrics of code segments
/// @ Return 64 indicates the CPU clock value of the unsigned integer
Unsigned long rdtsc ()
...{
# Ifdef _ msc_ver/** // * msvc compiler */
_ ASM _ emit 0x0f
_ ASM _ emit 0x31
# Else/* GCC compiler */
Unsigned long temp;
Unsigned int low, high;
_ ASM _ volatile _ ("rdtsc": "= A" (low), "= D" (high ));
Temp = high;
Temp <= 32;
Temp + = low;
Return temp;
# Endif
}

 

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.