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 信息

[StructLayout(LayoutKind.Sequential)]
internal ref struct CpuIdInfo
{
public uint Eax;
public uint Ebx;
public uint Ecx;
public uint Edx; public static void AppendAsString(StringBuilder builder,uint value)
{
var val = value; while (val != 0)
{
builder.Append((char) (val & 0xFF));
val >>= 8;
} } public string GetString()
{
StringBuilder ret = new StringBuilder(16);
AppendAsString(ret,Ebx);
AppendAsString(ret,Edx);
AppendAsString(ret,Ecx); return ret.ToString();
}
}
internal sealed class CpuIdAssemblyCode
: IDisposable
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void CpuIDDelegate(int level, ref CpuIdInfo cpuId); private IntPtr _codePointer;
private uint _size;
private CpuIDDelegate _delegate; public CpuIdAssemblyCode()
{
byte[] codeBytes = (IntPtr.Size == 4) ? x86CodeBytes : x64CodeBytes; _size = (uint) codeBytes.Length;
_codePointer = NativeMethods.Kernel32.VirtualAlloc(
IntPtr.Zero,
new UIntPtr(_size),
AllocationType.COMMIT | AllocationType.RESERVE,
MemoryProtection.EXECUTE_READWRITE
); Marshal.Copy(codeBytes, 0, _codePointer, codeBytes.Length);
#if NET40
_delegate = (CpuIDDelegate) Marshal.GetDelegateForFunctionPointer(_codePointer, typeof(CpuIDDelegate));
#else
_delegate = Marshal.GetDelegateForFunctionPointer<CpuIDDelegate>(_codePointer);
#endif } ~CpuIdAssemblyCode()
{
Dispose(false);
} public void Call(int level, ref CpuIdInfo cpuInfo)
{
_delegate(level, ref cpuInfo);
} public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} private void Dispose(bool disposing)
{
NativeMethods.Kernel32.VirtualFree(_codePointer, _size, 0x8000);
} // Basic ASM strategy --
// void x86CpuId(int level, byte* buffer)
// {
// eax = level
// cpuid
// buffer[0] = eax
// buffer[4] = ebx
// buffer[8] = ecx
// buffer[12] = edx
// } private readonly static byte[] x86CodeBytes =
{
0x55, // push ebp
0x8B, 0xEC, // mov ebp,esp
0x53, // push ebx
0x57, // push edi 0x8B, 0x45, 0x08, // mov eax, dword ptr [ebp+8] (move level into eax)
0x0F, 0xA2, // cpuid 0x8B, 0x7D, 0x0C, // mov edi, dword ptr [ebp+12] (move address of buffer into edi)
0x89, 0x07, // mov dword ptr [edi+0], eax (write eax, ... to buffer)
0x89, 0x5F, 0x04, // mov dword ptr [edi+4], ebx
0x89, 0x4F, 0x08, // mov dword ptr [edi+8], ecx
0x89, 0x57, 0x0C, // mov dword ptr [edi+12],edx 0x5F, // pop edi
0x5B, // pop ebx
0x8B, 0xE5, // mov esp,ebp
0x5D, // pop ebp
0xc3 // ret
}; private readonly static byte[] x64CodeBytes =
{
0x53, // push rbx this gets clobbered by cpuid // rcx is level
// rdx is buffer.
// Need to save buffer elsewhere, cpuid overwrites rdx
// Put buffer in r8, use r8 to reference buffer later. // Save rdx (buffer addy) to r8
0x49, 0x89, 0xd0, // mov r8, rdx // Move ecx (level) to eax to call cpuid, call cpuid
0x89, 0xc8, // mov eax, ecx
0x0F, 0xA2, // cpuid // Write eax et al to buffer
0x41, 0x89, 0x40, 0x00, // mov dword ptr [r8+0], eax
0x41, 0x89, 0x58, 0x04, // mov dword ptr [r8+4], ebx
0x41, 0x89, 0x48, 0x08, // mov dword ptr [r8+8], ecx
0x41, 0x89, 0x50, 0x0c, // mov dword ptr [r8+12], edx 0x5b, // pop rbx
0xc3 // ret
}; }

使用方法

var asmCode = new CpuIdAssemblyCode();
CpuIdInfo info = new CpuIdInfo();
asmCode.Call(0, ref info);
asmCode.Dispose();
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. 脚本_根据 md5 校验码,检测文件是否被修改

    #!bin/bash#功能:根据 md5 校验码,检测文件是否被修改#作者:liusingbon#本示例脚本检测的是/etc 目录下所有的 conf 结尾的文件,根据实际情况,您可以修改为其他目录或文 ...

  2. DB count check for TABLES VIEWS PROCEDURES TRIGGERS

    SELECT DISTINCT(TABLESPACE_NAME) FROM ALL_TABLES; SELECT COUNT(*) FROM ALL_TABLES where TABLESPACE_N ...

  3. python搭建服务,传输文件

    一.进入需要共享的文件目录 #cd /home #python -m SimpleHTTPServer 二.浏览器访问资源 再浏览器地址栏输入: 服务器IP:8000 如图:

  4. Strcpy,strcpy使用注意

    一.char *strcpy(char *dest, const char *src) 参数 dest -- 指向用于存储复制内容的目标数组. src -- 要复制的字符串. 注意: 1.dest需要 ...

  5. Flutter-RaisedButton

    RaisedButton({ Key key, //点击按钮的回调出发事件 @required VoidCallback onPressed, //水波纹高亮变化回调 ValueChanged< ...

  6. 前端每日实战:50# 视频演示如何用纯 CSS 创作一个永动的牛顿摆

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qKmGaJ 可交互视频教程 此视频 ...

  7. Facebook的利润创下历史新高,不受最近的丑闻影响

    外媒:Facebook周三表示,其第一季度收入主要由广告支出,增长49%至120亿美元.净利润同比增长65%,创纪录的49亿美元. 尽管很多用户表示,他们在3月中旬发生的隐私丑闻导致他们删除了Face ...

  8. ForkJoinPool线程池--分支执行

    import java.util.ArrayList; import java.util.concurrent.ExecutionException; import java.util.concurr ...

  9. vue部署后刷新404问题

    为什么会404NotFound Internet Information Services (IIS) 第一步:安装 IIS UrlRewrite 第二步:配置重写URL规则 在你的网站根目录中创建一 ...

  10. 【FPGA】 007 --Verilog中 case,casez,casex的区别

    贴一个链接:http://www.cnblogs.com/poiu-elab/archive/2012/11/02/2751323.html Verilog中  case,casez,casex的区别 ...