C# 什么是本地代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/151952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:47:29  来源:igfitidea点击:

What is Native Code?

提问by Guy

The Project's Web section (under project properties in VS2008) has a list of debuggers: ASP.NET, Native Code, SQL Server. What is Native Code?

项目的 Web 部分(在 VS2008 中的项目属性下)有一个调试器列表:ASP.NET、本机代码、SQL Server。什么是本地代码?

采纳答案by John Millikin

Native code is machine code executed directly by the CPU. This is in contrast to .NET bytecode, which is interpreted by the .NET virtual machine.

本机代码是由 CPU 直接执行的机器代码。这与由 .NET 虚拟机解释的 .NET 字节码形成对比。

A nice MSDN hit: Debugging Native Code

一个不错的 MSDN 热门:调试本机代码

回答by JeffFoster

Native code doesn't run on the Common Language Runtime (CLR). An example would be a non-managed C++ application.

本机代码不在公共语言运行时 (CLR) 上运行。一个示例是非托管 C++ 应用程序。

回答by JeffFoster

Native code is essentially data in memory that the central processing chip in the computer can read and execute directly. Think of the CPU sucking in data, and that data flipping switches as it goes through, turning things off and on:

本地代码本质上是计算机中的中央处理芯片可以直接读取和执行的内存中的数据。想想 CPU 吸收数据,数据翻转会随着它的经过而切换,关闭和打开:

   [  CPU  ] ==================================== [  RAM  ]
     ^^^^^
     |   |

     LOAD _memoryAddress12, D1   ; tells the CPU to get data from slot 12
                                 ; in RAM, and put it in register D1 inside the CPU

     ^^^^^
     |   |

     ADD D1, 24                  ; tells the CPU to do an internal calculation

     ^^^^^
     |   |

     STORE R0, _memoryAddress23 ; tells the CPU to put the answer into slot 23 in ram

You can think of the instructions like punch cards, or those musical piano roll things, that flip switches in the CPU as they go in. The important part is that this is in HARDWARE: it's literallyhappening on wires / circuitry, almost at the speed of light. But there area lot of switches to flip. So, each of those "native instructions" going into the machine gets executed at the "clock speed" of the machine (around 2.5 billion times per second on a modern PC). In reality, it's a bit more complex, with some instructions taking a bit longer, some being done at the same time, etc.

你可以想到像打孔卡或那些音乐钢琴卷曲之类的指令,当它们进入时,它们会在 CPU 中翻转开关。重要的部分是这是在硬件中:它确实发生在电线/电路上,几乎以速度的光。但是,很多开关翻转的。因此,进入机器的每条“本机指令”都以机器的“时钟速度”执行(在现代 PC 上每秒大约执行 25 亿次)。实际上,它有点复杂,有些指令需要更长的时间,有些指令同时完成,等等。

Now, in contrast, virtual machines run non-native code (often called bytecode), literally on a virtual, fake machine. When it comes to languages, a virtual machine is a program that runs ANOTHER program, instead of the program just running directly in hardware. So, where the above program loads data, adds to it, and stores a result in just three native instructions a virtual program might do something more like this (Disclaimer: this is rusty, pseudo-assembly code):

现在,相比之下,虚拟机运行非本机代码(通常称为字节码),实际上是在虚拟的、伪造的机器上。就语言而言,虚拟机是运行另一个程序的程序,而不是直接在硬件中运行的程序。因此,在上述程序加载数据、添加数据并将结果存储在三个本机指令中的地方,虚拟程序可能会做更多类似的事情(免责声明:这是生锈的伪汇编代码):

   load _firstInstruction, D1
   if_equal D1, 12
   jump _itsAnAddInstructionHandleIt
   if_equal D1, 13
   jump _itsASubstractInstructionHandleIt
   if_equal D1, 14
   jump _itsAMultiplyInstructionHandleIt
   if_equal D1, 15
   jump _itsADivideInstructionHandleIt
   if_equal D1, 16
   jump _itsALoadInstructionHandleIt
   ...

_itsALoadInstructionHandleIt:
   load D1, D2
   add 4, D2
   load D2, D3
   return

And so on. This is just an example of handling ONEof the above native instructions in a non-native way: about 10 instructions (depending on implementation) instead of the first, single, native instruction, and I left out important details, like unboxing data! The point is that, probably in an average of about 20-30 instructions later, you'll have accomplished the same thing as ONE line from the native code above.

等等。这只是处理的例子ONE在非原生方式的上述原生指令:约10个指令(取决于实现),而不是第一个,单,原生指令,我忽略的重要细节,如拆箱数据!关键是,可能在平均大约 20-30 条指令之后,您将完成与上面本机代码中的一行相同的事情。

NOW. All that said, GOOD virtual machines have a JIT, which can convert SOME of this into native code as they're executed, or just before executing them. But there are a lot of things, like Boxed types, which can't be directly converted, because the whole point of a virtual machine is that it doesn't work in the a low level, circuitry-friendly way, that native code would. Virtual machines are easier to program, but much slower.

现在。总而言之,好的虚拟机有一个 JIT,它可以在执行时或在执行它们之前将其中的一些转换为本机代码。但是有很多东西,比如盒装类型,不能直接转换,因为虚拟机的全部意义在于它不能以低级的、电路友好的方式工作,本地代码会. 虚拟机更容易编程,但速度要慢得多。

Another huge disadvantage of virtual machines is that they often have big memory overheads, which makes them pretty useless if you want to code up millions of items of data all in memory at the same time. At times like that, a VM, though intended to make code higher-level and more readable, can force you to do LOWER-level, nastier things than native code, because the benefits start to become drawbacks.

虚拟机的另一个巨大缺点是它们通常具有很大的内存开销,如果您想同时在内存中编码数百万项数据,这使得它们非常无用。在这种情况下,虚拟机虽然旨在使代码更高级和更具可读性,但可以迫使您做比本机代码更底层、更讨厌的事情,因为好处开始变成缺点。

回答by Dr Munyaradzi Mhaka

For starters, the native code is just an Intermediate Language tailored to run on a particular assembly. It resembles the object code as in other HLLs.

对于初学者来说,本机代码只是一种为在特定程序集上运行而量身定制的中间语言。它类似于其他 HLL 中的目标代码。