C # Intermediate code and inline assembly

Source: Internet
Author: User

The Intermediate code (IL) is an internal representation of the source program for example C language compiling a program then C language compilationThe Translator will translate all the code into machine instructions that can be machine-recognized. The same is true of the C # compiler. It is a directive called intermediate code that is compiled by C # (CSC) into an instruction that can be recognized by the CLR. C # can be assembled inline but needs to pass through Emit and one through mono but typically Microsoft Emit.IL ADD:
        static void Main (string[] args)        {            DynamicMethod add = new DynamicMethod ("Add", typeof (int), new type[] {typeof (in T), typeof (int)});            ILGenerator il = Add. GetILGenerator ();            Il. Emit (OPCODES.LDARG_0); ldarg.0            IL. Emit (opcodes.ldarg_1); Ldarg.1            IL. Emit (Opcodes.add); Add            il. Emit (Opcodes.ret); RET            int num = (int) Add. Invoke (Add, New object[] {1, 2});        }
ldarg.0 pressing into parameter 0 to the evaluation stackldarg.1 pressing into parameter 1 to the evaluation stackAdd two number additionret return
ASM ADD:
<span style= "FONT-SIZE:12PX;" >int Add (int x, int y) {int Ret;_asm{mov         eax, DWORD Ptr[x]add         eax, DWORD Ptr[y]mov         DWORD Ptr[ret], Eax}retu RN ret;} </span>
the number of MOV source operands is transferred to the target operandAdd two number additionEAX 32-bit register DWORD ptr four byte address
IL while:
        static void Main (string[] args) {DynamicMethod _while = new DynamicMethod ("while", typeof (int),            NULL); ILGenerator il = _while.            GetILGenerator (); Il. DeclareLocal (typeof (int)); int i Label il_0004 = IL. Definelabel (); il_0004 Label il_0008 = IL. Definelabel (); Il_0008 IL. Emit (OPCODES.LDC_I4_0); Ldc.i4.0 IL. Emit (OPCODES.STLOC_0); stloc.0 IL. Emit (opcodes.br_s, il_0008); Br.s il_0008 IL.            Marklabel (il_0004); Il. Emit (OPCODES.LDLOC_0); ldloc.0 IL. Emit (Opcodes.ldc_i4_1); ldc.i4.1 IL. Emit (Opcodes.add); Add IL. Emit (OPCODES.STLOC_0); stloc.0 IL.            Marklabel (il_0008); Il. Emit (OPCODES.LDLOC_0); ldloc.0 IL. Emit (opcodes.ldc_i4_s, 100); Ldc.i4.s-il. Emit (opcodes.blt_s, il_0004); Blt.s il_0004 IL. Emit (OPCODES.LDLOC_0); ldloc.0 IL. EMIT (Opcodes.ret); ret int num = (int) _while.        Invoke (_while, NULL); }
ldc.i4.0 pressing into __int32 0 to the evaluation stackstloc.0 from the compute stack pop-up value to the local variable 0BR.S unconditionally transfer ldloc.0 press into local variable 0 to the evaluation stackldc.i4.1 pressing into __int32 1 to the evaluation stack
Add two number additionLdc.i4.s pressed into __int8 but as __int32 to the evaluation stack BLT.S if less than the transfer RET returns the return value (if present) pressed into the caller's evaluation stack (WIN32-EAX)
ASM while:
void Main (int argc, char* argv[]) {        __int32 I;_asm{mov         dword ptr [i],0 _loop_beige:cmp         dword ptr [i],64hjge< C7/>_loop_endmov         Eax,dword ptr [i]  add         eax,1 mov         dword ptr [I],eax  jmp         _loop_beige _loop _end:}}
Jge when the transfer is greater than or equal
JMP Unconditional Transfer
The code of the two is not very difficult to understand, but you want it to write programs that are painful.But IL is an exception .it's powerful. Bypass compiler check get hidden objectsinthe member Il will be easy AOP in C # isthe use of emit to achieve someThe time we need to optimize code from the middle code is a good idea. It's just that every instruction language has a problem. Doing a little loop involvesto theA lot of instructions can make peopleI 'm bored, but the middle code is better than the assembly.too much. Look at these two different compilations.do you still feel mdil to be more relaxed at least I think so.

C # Intermediate code and inline assembly

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.