Is there a way to see the native code produced by theJITter for given C# / CIL?

前端 未结 3 418
时光取名叫无心
时光取名叫无心 2020-12-08 15:23

In a comment on this answer (which suggests using bit-shift operators over integer multiplication / division, for performance), I queried whether this would actually be fast

相关标签:
3条回答
  • 2020-12-08 16:02

    While you're debugging (and only while you're debugging) just click at Debug - Windows - Disassembly or press the corresponding shortcut Ctrl+Alt+D.

    0 讨论(0)
  • 2020-12-08 16:11

    You won't get meaningful results until you configure the debugger. Tools + Options, Debugging, General, turn off "Suppress JIT optimization on module load". Switch to the Release mode configuration. A sample snippet:

    static void Main(string[] args) {
      int value = 4;
      int result = divideby2(value);
    }
    

    You are doing it right if the disassembly looks like this:

    00000000  ret  
    

    You'll have to fool the JIT optimizer to force the expression to be evaluated. Using Console.WriteLine(variable) can help. Then you ought to see something like this:

    0000000a  mov         edx,2 
    0000000f  mov         eax,dword ptr [ecx] 
    00000011  call        dword ptr [eax+000000BCh] 
    

    Yup, it evaluated the result at compile time. Works pretty well, doesn't it.

    0 讨论(0)
  • 2020-12-08 16:28

    Yes. Visual Studio has a built in disassembler to do that. You have to add the command to your menu bar though. Go to Extras/Customize/Commands (I don't know if they are really called that way in the english version though) and add the command Dissassembly, which is unter Debugging, somewhere to your menu bar.

    Then, set a breakpoint in your program and when it breaks, click on this Disassembly command. VS will show you the disassembled machine code.

    Example output for a Divider-method:

    public static int Divider(int intArg)
        {
    00000000  push        ebp  
    00000001  mov         ebp,esp 
    00000003  push        edi  
    00000004  push        esi  
    00000005  push        ebx  
    00000006  sub         esp,34h 
    00000009  mov         esi,ecx 
    0000000b  lea         edi,[ebp-38h] 
    0000000e  mov         ecx,0Bh 
    00000013  xor         eax,eax 
    00000015  rep stos    dword ptr es:[edi] 
    00000017  mov         ecx,esi 
    00000019  xor         eax,eax 
    0000001b  mov         dword ptr [ebp-1Ch],eax 
    0000001e  mov         dword ptr [ebp-3Ch],ecx 
    00000021  cmp         dword ptr ds:[00469240h],0 
    00000028  je          0000002F 
    0000002a  call        6BA09D91 
    0000002f  xor         edx,edx 
    00000031  mov         dword ptr [ebp-40h],edx 
    00000034  nop              
        return intArg / 2;
    00000035  mov         eax,dword ptr [ebp-3Ch] 
    00000038  sar         eax,1 
    0000003a  jns         0000003F 
    0000003c  adc         eax,0 
    0000003f  mov         dword ptr [ebp-40h],eax 
    00000042  nop              
    00000043  jmp         00000045 
        }
    
    0 讨论(0)
提交回复
热议问题