2019-8-31-C#-使用汇编
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();
2019-8-31-C#-使用汇编的更多相关文章
- agentzh 的 Nginx 教程(版本 2019.07.31)
agentzh 的 Nginx 教程(版本 2019.07.31) agentzh 的 Nginx 教程(版本 2019.07.31) https://openresty.org/download/a ...
- 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 ...
- 2020届京东秋招正式批一面记录-Java开发-2019.08.31
京东一面总结 总共时间持续时间约40分钟 1.你用过集合类里面哪些是线程安全的,哪些是线程不安全的?分别举两个例子? 线程安全:HashTable以及ConcurrentHashMap 非线程安全:A ...
- 牛客CSP-S提高组赛前集训营2 ———— 2019.10.31
比赛链接 期望得分:100+20+20 实际得分:40+20+30 awa cccc T1 :基于贪心的思路,然后开始爆搜(雾 那必然是会死的,好吧他就是死了 #include<iostrea ...
- Beta冲刺(9/7)——2019.5.31
作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Beta冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 队 ...
- lyc——2019.10.31
10:判决素数个数 总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个整数X和Y,输出两者之间的素数个数(包括X和Y). 输入 两个整数X和Y(1 <= X,Y <= ...
- Office 2019 2016 安装破解教程
声明:工具由蓝点网提供支持,密钥为本人收集内容,非转载部分 GVLKs for Office 2019 Product GVLK Office Professional Plus 2019 ...
- 工具软件集合 Adobe AE PS Pr CC 2018 2019 破解教程
来源https://mp.weixin.qq.com/s/zeq1sTmaPsKt7Bsok0Ldrg(若链接失效,请关注软件安装管家公众号) 相关链接 Office 2019破解教程 Adobe 2 ...
- NOI2019退役记 upd:2019.12.1
(我把原来写的东西全部删掉了) AFO. 我退役了,\(\mbox{yyb}\)退役了. 至少,在接下来的日子里,我得投身到文化课,度过快乐的高三生活了. 这两年的\(OI\)生涯给了我很多,让我学会 ...
- UCore-Lab0
日期:2019/3/31 内容:UCore-Lab0 一.UCore实验 实验 说明 关键词 Lab1 bootloader的实现 中断 Lab2 物理内存管理 x86分段/分页模式 Lab3 虚拟内 ...
随机推荐
- Shell06--数组应用
目录 Shell06---数组应用 1. 数组基本概述 2. 数组基本使用 3. 数组遍历与循环 Shell06---数组应用 1. 数组基本概述 01. 什么是数组? 数组其实也算是变量,传统的变量 ...
- setup PC not sleep when turn off display
- ppt怎么制作抖音快手快闪效果的倒计时动画?
ppt怎么制作快闪效果的倒计时动画? 1.首先,我们新建一个ppt,如下图: 2.然后我们在ppt中插入一个文本,文本内容为3,如下图: 3.然后我们将我们的文本设置为“Arial Black”,如下 ...
- 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 ...
- PyQt5界面上调用subprocess.Popen会闪命令窗口的问题
最近再做一个界面开发,主要实现的点击一个按钮,会执行adb安装应用程序的功能,在调试阶段一切都正常,但打包成一个exe安装程序,安装之后运行,点击按钮会闪一下adb的命令窗口 先列出subproces ...
- centos 6.5 解压 tar
只查看 tar 文件内容而不解压 tar -tvf filename.tar 解压到指定目录(没有指定则为当前目录) tar xvf filename.tar -C /usr/file 压缩为 tar ...
- Docker安装RMQ
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11752934.html 进入rabbitmq的docker hub镜像仓库地址:https://hub ...
- Mac OS 10.15系统入门教程 系统语言输入法详解
对于一些Mac新手来说呢还不知道偏好设置到底是什么?有什么用处?其实Mac系统内的几乎所有的系统相关的设置都会在系统偏好设置内出现. 切换系统语⾔在语言与地区设置中拖拽左侧的语言条目就可以切换系统的语 ...
- Delphi Treeview 用法(概念、属性、添加编辑插入节点、定位节点、拖拽等)
今天再细研究了一下Treeview的用法,网上虽然总结了很多,但是还是有很多节点没有讲到了,也给使用中遇到很多问题.特地总结一下: 1.概念 Treeview用于显示按照树形结构进行组织的数据.Tre ...
- Angular JS - 4 - Angular JS 作用域与控制器对象
1. 控制器对象使用 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&quo ...