本机代码、机器代码和汇编代码有什么区别? [英] What is the difference between native code, machine code and assembly code?

查看:57
本文介绍了本机代码、机器代码和汇编代码有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 .NET 语言上下文中的机器代码和本机代码感到困惑.

I'm confused about machine code and native code in the context of .NET languages.

它们之间有什么区别?它们是一样的吗?

What is the difference between them? Are they the same?

推荐答案

这些术语确实有点令人困惑,因为它们有时使用不一致.

The terms are indeed a bit confusing, because they are sometimes used inconsistently.

机器代码:这是定义最明确的代码.它是使用处理器(执行实际工作的物理金属片)理解并直接执行的字节码指令的代码.在您的机器可以执行之前,所有其他代码必须被翻译或转换为机器代码.

Machine code: This is the most well-defined one. It is code that uses the byte-code instructions which your processor (the physical piece of metal that does the actual work) understands and executes directly. All other code must be translated or transformed into machine code before your machine can execute it.

本机代码:该术语有时用于表示机器代码(见上文)的地方.但是,它有时也用于表示非托管代码(见下文).

Native code: This term is sometimes used in places where machine code (see above) is meant. However, it is also sometimes used to mean unmanaged code (see below).

非托管代码托管代码:非托管代码是指用C或C++等编程语言编写的代码,直接编译转化为机器代码.它与托管代码形成对比,后者用 C#、VB.NET、Java 或类似语言编写,并在虚拟环境(例如 .NET 或 JavaVM)中执行,后者模拟"了软件中的处理器.主要区别在于,托管代码通过使用垃圾收集和保持对对象的引用不透明来为您管理"资源(主要是内存分配).非托管代码是一种需要您手动分配和取消分配内存的代码,有时会导致内存泄漏(当您忘记取消分配时),有时会导致分段错误(当您也取消分配时)很快).非托管通常还意味着没有针对常见错误(例如空指针取消引用或数组边界溢出)的运行时检查.

Unmanaged code and managed code: Unmanaged code refers to code written in a programming language such as C or C++, which is compiled directly into machine code. It contrasts with managed code, which is written in C#, VB.NET, Java, or similar, and executed in a virtual environment (such as .NET or the JavaVM) which kind of "simulates" a processor in software. The main difference is that managed code "manages" the resources (mostly the memory allocation) for you by employing garbage collection and by keeping references to objects opaque. Unmanaged code is the kind of code that requires you to manually allocate and de-allocate memory, sometimes causing memory leaks (when you forget to de-allocate) and sometimes segmentation faults (when you de-allocate too soon). Unmanaged also usually implies there are no run-time checks for common errors such as null-pointer dereferencing or array bounds overflow.

严格来说,大多数动态类型语言(例如 Perl、Python、PHP 和 Ruby)也是托管代码.然而,它们并不常被这样描述,这表明托管代码实际上是一个针对真正大型、严肃的商业编程环境(.NET 和 Java)的营销术语.

Strictly speaking, most dynamically-typed languages — such as Perl, Python, PHP and Ruby — are also managed code. However, they are not commonly described as such, which shows that managed code is actually somewhat of a marketing term for the really big, serious, commercial programming environments (.NET and Java).

汇编代码:这个术语通常是指人们真正想编写字节码时编写的那种源代码.汇编程序 是将源代码转换为真正字节码的程序.它不是编译器,因为转换是一对一的.然而,该术语对于使用哪种字节码是不明确的:它可以是托管的,也可以是非托管的.如果它是非托管的,则生成的字节码是机器代码.如果对它进行管理,则会产生由 .NET 等虚拟环境在幕后使用的字节码.托管代码(例如 C#、Java)被编译成这种特殊的字节码语言,在 .NET 中称为通用中间语言 (CIL),在 Java 中称为 Java 字节-代码.普通程序员通常很少需要访问此代码或直接用这种语言编写,但是当人们这样做时,他们通常将其称为汇编代码,因为他们使用汇编程序 将其转换为字节码.

Assembly code: This term generally refers to the kind of source code people write when they really want to write byte-code. An assembler is a program that turns this source code into real byte-code. It is not a compiler because the transformation is 1-to-1. However, the term is ambiguous as to what kind of byte-code is used: it could be managed or unmanaged. If it is unmanaged, the resulting byte-code is machine code. If it is managed, it results in the byte-code used behind-the-scenes by a virtual environment such as .NET. Managed code (e.g. C#, Java) is compiled into this special byte-code language, which in the case of .NET is called Common Intermediate Language (CIL) and in Java is called Java byte-code. There is usually little need for the common programmer to access this code or to write in this language directly, but when people do, they often refer to it as assembly code because they use an assembler to turn it into byte-code.

这篇关于本机代码、机器代码和汇编代码有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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