我可以在.NET中使用Assembly吗 [英] Can i use Assembly within .NET

查看:146
本文介绍了我可以在.NET中使用Assembly吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我们可以将汇编语言与C ++混合在一起
喜欢

_asm mov eax,ebx


我们可以在vb.NET或C#.NET中使用汇编语言吗?如果是,那么如何?

As, we can intermix assembly language with C++
like

_asm mov eax, ebx


Can we use assembly language in vb.NET or C#.NET. If yes, then how??

推荐答案

您不能,不能直接.

该平台是完全不同的:它不编译为CPU指令集代码;该代码被编译为CIL(通用中间语言),因此,除非应用了一些限制,否则代码可以在实现SLR 的许多不同平台上运行,而无需重新编译. (例如,这样的限制之一就是通过P/Invoke使用本机代码.)的确如此:我经常在Linux上运行.NET应用程序而无需重新编译,即使是那些使用System.Windows.Forms(不是标准库)的应用程序.

加载后,.NET程序集将在使用JIT(即时编译器)执行的每个平台上编译为CPU指令.最典型的是,当每种方法都将首次被调用时,JIT编译是按方法执行的.这样,永远不会在本地代码中编译从未调用的方法.

请参阅:
http://en.wikipedia.org/wiki/Common_Language_Runtime [ http://en.wikipedia.org/wiki/Common_Intermediate_Language [ http://en.wikipedia.org/wiki/Common_Language_Infrastructure [ http://en.wikipedia.org/wiki/Just-in-time_compilation [ ^ ].

这应该可以解释为什么在用C#,VB.NET或IL编写的.NET程序集中没有汇编语言的地方.

但是在这里有一个微妙的区别:这并不意味着可执行模块不能同时放置本机代码和.NET程序集.可以同时用C ++和C ++/CLI编写的所谓的 mixed-mode (托管+非托管)项目来实现此相当奇特但相对广泛使用的选项.您甚至可以在C ++/CLI中使用C ++代码,反之亦然.

在-瞧! —您可以在此类项目的非托管C ++部分中使用asm指令.

关于C ++/CLI,请参阅:
http://en.wikipedia.org/wiki/C%2B%2B/CLI [ ^ ],
http://www.ecma-international.org/publications/standards/Ecma-372.htm [^ ],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ],
http://www.gotw.ca/publications/C++CLIRationale.pdf [ ^ ].

我不知道是否需要提到您可以在.NET应用程序中间接使用汇编语言:您可以使用汇编语言或某些嵌入式嵌入式Assembler和高级语言编写非托管(本机)可执行模块(DLL).使用P/Invoke在.NET程序集中使用此模块.

如果需要学习,请参阅:
http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [^ ].

此CodeProject文章也可能有用:
基本P/调用 [
You cannot, not directly.

The platform is absolutely different: it does not compile to CPU instruction-set code; the code is compiled into CIL (Common Intermediate Language), so the code can run on many different platforms implementing SLR without recompilation, unless some limitation are applied. (One such limitations is using the native code through P/Invoke, for example.) This is really so: I regularly run my .NET applications on Linux without recompilation, even those using System.Windows.Forms (which is not a standard library).

When loaded, the .NET assembly is compiled to the CPU instructions on each platforms it''s being executed using JIT (Just-in-Time Compiler). Most typically, JIT compilation is performed on the per-method basis, when each method is about to be called for the very first time. This way, the methods never called may never be compiled in the native code.

Please see:
http://en.wikipedia.org/wiki/Common_Language_Runtime[^],
http://en.wikipedia.org/wiki/Common_Intermediate_Language[^],
http://en.wikipedia.org/wiki/Common_Language_Infrastructure[^],
http://en.wikipedia.org/wiki/Just-in-time_compilation[^].

This should explain why there is not a place for assembly language in a .NET assembly written in C#, VB.NET or IL.

However, there is a delicate difference here: it does not mean that an executable module cannot have a place for both native code and .NET assembly at the same time. This pretty exotic but relatively widely used option can be implemented with so-called mixed-mode (managed+unmanaged) project written in C++ and C++/CLI at the same time. You can even use C++ code in C++/CLI and visa versa.

In — voila! — you can use asm directives in unmanaged C++ parts of such projects.

About C++/CLI, please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^].

I don''t know if it needs to mention that you can use Assembly language in a .NET application indirectly: you can write some unmanaged (native) executable module (DLL) using Assembly language or some high-level language with embedded Assembler and use this module in .NET assembly using P/Invoke.

If you need to learn it, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be useful:
Essential P/Invoke[^].

—SA


我认为此代码项目文章
在C#中使用非托管代码和汇编器 [ ^ ]
可能会有所帮助.
I think this Code Project article
Using Unmanaged code and assembler in C#[^]
may be helpful.


这篇关于我可以在.NET中使用Assembly吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆