title author date CreateTime categories
C# 使用汇编
lindexi
2019-08-31 16:55:58 +0800
2019-2-16 8:56:5 +0800
C#

本文告诉大家如何在 C# 里面使用汇编代码

请看

C#嵌入x86汇编——一个GPIO接口的实现 - 云+社区 - 腾讯云

C# inline-asm / 嵌入x86汇编 - 苏璃 - CSDN博客

通过这个方法在 dotnet core 获取 CPU 信息

  1. [StructLayout(LayoutKind.Sequential)]
  2. internal ref struct CpuIdInfo
  3. {
  4. public uint Eax;
  5. public uint Ebx;
  6. public uint Ecx;
  7. public uint Edx;
  8.  
  9. public static void AppendAsString(StringBuilder builder,uint value)
  10. {
  11. var val = value;
  12.  
  13. while (val != 0)
  14. {
  15. builder.Append((char) (val & 0xFF));
  16. val >>= 8;
  17. }
  18.  
  19. }
  20.  
  21. public string GetString()
  22. {
  23. StringBuilder ret = new StringBuilder(16);
  24. AppendAsString(ret,Ebx);
  25. AppendAsString(ret,Edx);
  26. AppendAsString(ret,Ecx);
  27.  
  28. return ret.ToString();
  29. }
  30. }
  31. internal sealed class CpuIdAssemblyCode
  32. : IDisposable
  33. {
  34. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  35. private delegate void CpuIDDelegate(int level, ref CpuIdInfo cpuId);
  36.  
  37. private IntPtr _codePointer;
  38. private uint _size;
  39. private CpuIDDelegate _delegate;
  40.  
  41. public CpuIdAssemblyCode()
  42. {
  43. byte[] codeBytes = (IntPtr.Size == 4) ? x86CodeBytes : x64CodeBytes;
  44.  
  45. _size = (uint) codeBytes.Length;
  46. _codePointer = NativeMethods.Kernel32.VirtualAlloc(
  47. IntPtr.Zero,
  48. new UIntPtr(_size),
  49. AllocationType.COMMIT | AllocationType.RESERVE,
  50. MemoryProtection.EXECUTE_READWRITE
  51. );
  52.  
  53. Marshal.Copy(codeBytes, 0, _codePointer, codeBytes.Length);
  54. #if NET40
  55. _delegate = (CpuIDDelegate) Marshal.GetDelegateForFunctionPointer(_codePointer, typeof(CpuIDDelegate));
  56. #else
  57. _delegate = Marshal.GetDelegateForFunctionPointer<CpuIDDelegate>(_codePointer);
  58. #endif
  59.  
  60. }
  61.  
  62. ~CpuIdAssemblyCode()
  63. {
  64. Dispose(false);
  65. }
  66.  
  67. public void Call(int level, ref CpuIdInfo cpuInfo)
  68. {
  69. _delegate(level, ref cpuInfo);
  70. }
  71.  
  72. public void Dispose()
  73. {
  74. Dispose(true);
  75. GC.SuppressFinalize(this);
  76. }
  77.  
  78. private void Dispose(bool disposing)
  79. {
  80. NativeMethods.Kernel32.VirtualFree(_codePointer, _size, 0x8000);
  81. }
  82.  
  83. // Basic ASM strategy --
  84. // void x86CpuId(int level, byte* buffer)
  85. // {
  86. // eax = level
  87. // cpuid
  88. // buffer[0] = eax
  89. // buffer[4] = ebx
  90. // buffer[8] = ecx
  91. // buffer[12] = edx
  92. // }
  93.  
  94. private readonly static byte[] x86CodeBytes =
  95. {
  96. 0x55, // push ebp
  97. 0x8B, 0xEC, // mov ebp,esp
  98. 0x53, // push ebx
  99. 0x57, // push edi
  100.  
  101. 0x8B, 0x45, 0x08, // mov eax, dword ptr [ebp+8] (move level into eax)
  102. 0x0F, 0xA2, // cpuid
  103.  
  104. 0x8B, 0x7D, 0x0C, // mov edi, dword ptr [ebp+12] (move address of buffer into edi)
  105. 0x89, 0x07, // mov dword ptr [edi+0], eax (write eax, ... to buffer)
  106. 0x89, 0x5F, 0x04, // mov dword ptr [edi+4], ebx
  107. 0x89, 0x4F, 0x08, // mov dword ptr [edi+8], ecx
  108. 0x89, 0x57, 0x0C, // mov dword ptr [edi+12],edx
  109.  
  110. 0x5F, // pop edi
  111. 0x5B, // pop ebx
  112. 0x8B, 0xE5, // mov esp,ebp
  113. 0x5D, // pop ebp
  114. 0xc3 // ret
  115. };
  116.  
  117. private readonly static byte[] x64CodeBytes =
  118. {
  119. 0x53, // push rbx this gets clobbered by cpuid
  120.  
  121. // rcx is level
  122. // rdx is buffer.
  123. // Need to save buffer elsewhere, cpuid overwrites rdx
  124. // Put buffer in r8, use r8 to reference buffer later.
  125.  
  126. // Save rdx (buffer addy) to r8
  127. 0x49, 0x89, 0xd0, // mov r8, rdx
  128.  
  129. // Move ecx (level) to eax to call cpuid, call cpuid
  130. 0x89, 0xc8, // mov eax, ecx
  131. 0x0F, 0xA2, // cpuid
  132.  
  133. // Write eax et al to buffer
  134. 0x41, 0x89, 0x40, 0x00, // mov dword ptr [r8+0], eax
  135. 0x41, 0x89, 0x58, 0x04, // mov dword ptr [r8+4], ebx
  136. 0x41, 0x89, 0x48, 0x08, // mov dword ptr [r8+8], ecx
  137. 0x41, 0x89, 0x50, 0x0c, // mov dword ptr [r8+12], edx
  138.  
  139. 0x5b, // pop rbx
  140. 0xc3 // ret
  141. };
  142.  
  143. }

使用方法

  1. var asmCode = new CpuIdAssemblyCode();
  2. CpuIdInfo info = new CpuIdInfo();
  3. asmCode.Call(0, ref info);
  4. asmCode.Dispose();
  5. string ret= info.GetString();

c# - How can I get CPU name in .NET Core? - Stack Overflow

2019-8-31-C#-使用汇编的更多相关文章

  1. agentzh 的 Nginx 教程(版本 2019.07.31)

    agentzh 的 Nginx 教程(版本 2019.07.31) agentzh 的 Nginx 教程(版本 2019.07.31) https://openresty.org/download/a ...

  2. ARTS Challenge- Week 1 (2019.03.25~2019.03.31)

    1.Algorithm - at least one leetcode problem per week(Medium+) 986. Interval List Intersections https ...

  3. 2020届京东秋招正式批一面记录-Java开发-2019.08.31

    京东一面总结 总共时间持续时间约40分钟 1.你用过集合类里面哪些是线程安全的,哪些是线程不安全的?分别举两个例子? 线程安全:HashTable以及ConcurrentHashMap 非线程安全:A ...

  4. 牛客CSP-S提高组赛前集训营2 ———— 2019.10.31

    比赛链接 期望得分:100+20+20 实际得分:40+20+30 awa  cccc T1 :基于贪心的思路,然后开始爆搜(雾 那必然是会死的,好吧他就是死了 #include<iostrea ...

  5. Beta冲刺(9/7)——2019.5.31

    作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Beta冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 队 ...

  6. lyc——2019.10.31

    10:判决素数个数 总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个整数X和Y,输出两者之间的素数个数(包括X和Y). 输入 两个整数X和Y(1 <= X,Y <= ...

  7. Office 2019 2016 安装破解教程

    声明:工具由蓝点网提供支持,密钥为本人收集内容,非转载部分 GVLKs for Office 2019     Product GVLK Office Professional Plus 2019  ...

  8. 工具软件集合 Adobe AE PS Pr CC 2018 2019 破解教程

    来源https://mp.weixin.qq.com/s/zeq1sTmaPsKt7Bsok0Ldrg(若链接失效,请关注软件安装管家公众号) 相关链接 Office 2019破解教程 Adobe 2 ...

  9. NOI2019退役记 upd:2019.12.1

    (我把原来写的东西全部删掉了) AFO. 我退役了,\(\mbox{yyb}\)退役了. 至少,在接下来的日子里,我得投身到文化课,度过快乐的高三生活了. 这两年的\(OI\)生涯给了我很多,让我学会 ...

  10. UCore-Lab0

    日期:2019/3/31 内容:UCore-Lab0 一.UCore实验 实验 说明 关键词 Lab1 bootloader的实现 中断 Lab2 物理内存管理 x86分段/分页模式 Lab3 虚拟内 ...

随机推荐

  1. Shell06--数组应用

    目录 Shell06---数组应用 1. 数组基本概述 2. 数组基本使用 3. 数组遍历与循环 Shell06---数组应用 1. 数组基本概述 01. 什么是数组? 数组其实也算是变量,传统的变量 ...

  2. setup PC not sleep when turn off display

  3. ppt怎么制作抖音快手快闪效果的倒计时动画?

    ppt怎么制作快闪效果的倒计时动画? 1.首先,我们新建一个ppt,如下图: 2.然后我们在ppt中插入一个文本,文本内容为3,如下图: 3.然后我们将我们的文本设置为“Arial Black”,如下 ...

  4. spring boot启动异常:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver

    项目启动时提示:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represen ...

  5. PyQt5界面上调用subprocess.Popen会闪命令窗口的问题

    最近再做一个界面开发,主要实现的点击一个按钮,会执行adb安装应用程序的功能,在调试阶段一切都正常,但打包成一个exe安装程序,安装之后运行,点击按钮会闪一下adb的命令窗口 先列出subproces ...

  6. centos 6.5 解压 tar

    只查看 tar 文件内容而不解压 tar -tvf filename.tar 解压到指定目录(没有指定则为当前目录) tar xvf filename.tar -C /usr/file 压缩为 tar ...

  7. Docker安装RMQ

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11752934.html 进入rabbitmq的docker hub镜像仓库地址:https://hub ...

  8. Mac OS 10.15系统入门教程 系统语言输入法详解

    对于一些Mac新手来说呢还不知道偏好设置到底是什么?有什么用处?其实Mac系统内的几乎所有的系统相关的设置都会在系统偏好设置内出现. 切换系统语⾔在语言与地区设置中拖拽左侧的语言条目就可以切换系统的语 ...

  9. Delphi Treeview 用法(概念、属性、添加编辑插入节点、定位节点、拖拽等)

    今天再细研究了一下Treeview的用法,网上虽然总结了很多,但是还是有很多节点没有讲到了,也给使用中遇到很多问题.特地总结一下: 1.概念 Treeview用于显示按照树形结构进行组织的数据.Tre ...

  10. Angular JS - 4 - Angular JS 作用域与控制器对象

    1. 控制器对象使用 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&quo ...