C#代码如何转换成机器指令? [英] How C# code converted into machine instructions?

查看:202
本文介绍了C#代码如何转换成机器指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;



命名空间ConsoleApplication5

{

class Program

{

static void Main(string [] args)

{

int a = 10;

int b = 20;

int c = a + b;

Console.WriteLine(10 + 20之和为:+ c);

Console.ReadLine();

}

}

}

As我知道c#compiler csc.exe将此代码转换为字节代码,JIT将其转换为机器代码。但是当我在视觉工作室按f5时,我想知道整个过程是如何完成的?在此先感谢。

解决方案

正确。 C#代码转换为MSIL,这是.NET虚拟机可以理解的汇编语言。然后使用即时编译将MSIL编译为机器代码。



此过程不仅在您按F5时完成。这是一个非常漫长的过程,当你构建项目时, C#源代码被编译为程序集,字节代码,然后这些程序集由.NET的虚拟机执行,使用JIT编译。不需要总是使用JIT将MSIL编译为本机代码。因为如果您只想编译代码(构建它)而不运行代码,Visual Studio将只为您构建程序集。哪个出现在 / build / 文件夹下。



这是一个漫长的过程,当编译器将代码编译为MSIL时,它也会创建元数据。因为元数据定义了代码是什么,它应该如何执行等等。



你应该考虑给这些MSDN看一下:



1. 编译到MSIL [ ^ ]

2. 编译MSIL到本地代码 [ ^ ]



这些资源将帮助您了解该过程。还有另一种方法,它只为您提供汇编代码。不是MSIL代码。为此,调试应用程序,设置断点以暂停应用程序。一旦应用程序到达断点。转到调试 - >在窗口下,显示反汇编。它会告诉你这个代码! 该代码将是汇编语言代码。阅读本MSDN指南,了解如何:使用反汇编窗口 [ ^ ]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int c = a + b;
Console.WriteLine("Sum of 10+20 is : " + c);
Console.ReadLine();
}
}
}
As i know c# compiler csc.exe convert this code to Byte code and JIT convert it to machine code. But when i press f5 in visual studio i want to know how the whole process done? Thanks in advance.

解决方案

Correct. The C# code is converted to MSIL, which is the assembly language that .NET's virtual machine understands. The MSIL is then compiled to machine code using Just-in-time compilation.

The process is done not just when you press F5. It is a very long process, when you build the project, at that moment C# source code is compiled to assemblies, the byte codes, then those assemblies are executed by .NET's virtual machine, using JIT compilation. It is not required to always use JIT to compile the MSIL to native code also. Because if you want to only compile the code (build it) and not run it, Visual Studio will only build the assemblies for you. Which are present under the /build/ folder.

It is a long process, when compiler compiles the code to MSIL, it also creates the meta-data. Because meta-data defines what your code is, how it should be executed and so on and so forth.

You should consider giving these MSDN a look:

1. Compiling to MSIL[^]
2. Compiling MSIL to Native Code[^]

These resources will help you understand the process. Also there is another method, which only provides you with the assembly code. Not the MSIL code. For that, debug your application, set a breakpoint to pause the application. Once application hits the breakpoint. Go to Debug --> and under Window, show Disassembly. It would show you the code for this! That code would be assembly language code. Read this MSDN guide for learning "How to: Use the Disassembly Window[^]".


这篇关于C#代码如何转换成机器指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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