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. Git--03 git分支

    目录 Git分支 1.新建testing分支 2.合并分支 3.合并冲突 4.删除分支 Git标签使用 1.查看标签 02.删除标签 Git分支 ​ 分支即是平行空间,假设你在为某个手机系统研发拍照功 ...

  2. python发行包 IDE

    https://blog.csdn.net/qq_38188725/article/details/80624004 https://blog.csdn.net/qq_38188725/article ...

  3. Gradle构建SpringBoot并打包可运行的jar配置

    使用Gradle构建项目,继承了Ant的灵活和Maven的生命周期管理,不再使用XML作为配置文件格式,采用了DSL格式,使得脚本更加简洁. 构建环境: jdk1.6以上,此处使用1.8 Gradle ...

  4. LeetCode(力扣)——Search in Rotated Sorted Array2 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array2 搜索旋转排序数组   中文: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0 ...

  5. $Dsu$ $on$ $Tree$ 复习

    \(Dsu\) \(on\) \(Tree\) 复习 发现最近有点头晕,突然这东西就不会了,头疼了很久,决定写一份记录啊. 大致认识 适用范围一般在处理树上子树统计问题,不支持在线回答询问以及修改. ...

  6. String、StringBuffer、StringBuilder详解

    String类 字符串广泛应用在java编程中,String类在java.lang包中,String类是final修饰的,不能被继承,String类对象创建后不能修改,由0或多个字符组成,包含在一对双 ...

  7. boost tuple

    boost::tuple is a generalized version of std::pair. While std::pair can only store exactly two value ...

  8. 7.zabbix常用item

    zabbix常用item zabbix常用item vfs.file.md5sum[/etc/crontab] {basic:vfs.file.md5sum[/etc/crontab].diff()} ...

  9. Linux后台执行脚本 &与nohup

    Linux后台执行脚本的方式: 0.脚本代码 [root@VM_1_3_centos apps]# cat test.php <?php sleep(5); echo "hello w ...

  10. C#链接mysql出现 One of the identified items was in an invalid format

    这个问题在tolist查询结果的时候就会出现但是count就不会出现,后来才发现是数据生成工具生成出来的ID有问题导致的,只要保证iD不重复并且按照指定的类型建立ID就可以了