点击上方蓝字 江湖评谈设为关注
前言
经常有人问:怎么查看.NET汇编代码?一般的来说查看ASM,可以通过VS菜单栏-》调试-》汇编,来查看。此处查看的是ASM是未优化,粗糙的代码。专业点的会通过设置环境变量,Release编译查看。
C:\test>set DOTNET_JitDisasm=Main
C:\test>dotnet run -c Release
; Assembly listing for method ConsoleApplication.Program:Main(System.Collections.Generic.IEnumerable`1[System.String]):System.String (Instrumented Tier0)
; Emitting BLENDED_CODE for X64 with AVX512 - Windows
; Instrumented Tier0 code
; rbp based frame
; fully interruptible
G_M000_IG01: ;; offset=0x0000
push rbp
sub rsp, 208
lea rbp, [rsp+0xD0]
xor eax, eax
mov qword ptr [rbp-0x98], rax
vxorps xmm4, xmm4, xmm4
vmovdqa xmmword ptr [rbp-0x90], xmm4
vmovdqa xmmword ptr [rbp-0x80], xmm4
vmovdqa xmmword ptr [rbp-0x70], xmm4
vmovdqa xmmword ptr [rbp-0x60], xmm4
vmovdqa xmmword ptr [rbp-0x50], xmm4
mov qword ptr [rbp-0x40], rax
mov qword ptr [rbp-0xB0], rsp
mov gword ptr [rbp+0x10], rcx
更专业的可以通过LLDB来查看,但这些过程比较麻烦,有无简化的方法?当然有,即是VS2022+.NET顶级技术合成插件:Disasmo,后端通过LLVM操控。
Disasmo
插件用法:
VS2022菜单栏-》扩展-》管理扩展,搜索:Disasmo,下载即可
如果你在VS2022里面下载扩展插件速度非常慢,则可以在以下地址直接下载vsix插件:
https://marketplace.visualstudio.com/items?itemName=EgorBogatov.Disasmo
下载下来之后,双击Disasmo.Vsix.vsix,选择:Microsoft Visual Stuido Version Selector,弹出安装对话框
安装下即可,这里需要注意如果你用命令行安装:
PS C:\> code --install-extension .\Disasmo.Vsix.vsix
Installing extensions...
Error: extension/package.json not found inside zip.
at ZipFile.<anonymous> (D:\visusl_studio_code\Microsoft VS Code\resources\app\out\vs\code\node\cliProcessMain.js:18:15678)
at Object.onceWrapper (node:events:628:28)
at ZipFile.emit (node:events:514:28)
at FdSlicer.<anonymous> (D:\visusl_studio_code\Microsoft VS Code\resources\app\node_modules.asar\yauzl\index.js:202:10)
at Object.onceWrapper (node:events:628:28)
at FdSlicer.emit (node:events:514:28)
at onCloseDone (D:\visusl_studio_code\Microsoft VS Code\resources\app\node_modules.asar\fd-slicer\index.js:74:12)
at D:\visusl_studio_code\Microsoft VS Code\resources\app\node_modules.asar\graceful-fs\graceful-fs.js:61:14
at FSReqCallback.oncomplete (node:fs:202:23)
Failed Installing Extensions: file:///c%3A/Disasmo.Vsix.vsix
个人实测,会出现上面提示的一些错误。所以这里建议按照双击的方式,选择选择:Microsoft Visual Stuido Version Selector安装。最后安装完成如下:
重启下所有打开的VS即可生效。
用法
如下代码:
static int test(int a, int b) => a * b;
把鼠标放到test函数名称上面,出现灯泡形状的图标,点击图标-》Disasm This
就会打开Disasm窗口,即可看到结果,如下图
往期精彩回顾
新版.Net性能有没有达到C++90%?