Before explaining the process, you need to understand some basic concepts.
Basic concept explanation:
CPU ：The central processing unit, the brain of a computer, is made up of millions to hundreds of millions of transistors. It is the place where interpretation and operation are finally converted into machine language (binary code). Machine language is processed by the registers in CPU memory. Different types of CPU have different numbers and types of internal registers and the range of values stored in the registers. According to different functions, it can be roughly divided into eight categories
For programmers, CPU is a collection of registers with various functions. Among them, there is only one program counter, accumulation register, flag register, instruction register and stack register, while there are many other registers.
It should be noted that the type of CPU is a particularly important parameter. CPU can only interpret its own inherent machine language (instruction set), and different CPUs can interpret different kinds of machine language. For example, the main manufacturers of CPU are Intel and AMD. The former is based on X86 instruction set, while the latter is based on ARM instruction sethttps://zhuanlan.zhihu.com/p/95028674
At the same time to buy CPU can refer to the core / thread, frequency, multi-level cache and other parameters to choose.
Memory: the main memory of a computer, which is responsible for storing instructions and data, is connected with the CPU through a control chip, and is composed of readable and writable elements. Each byte has an address number, through which the CPU can read instructions and data in the main memory or write data. Instructions and data are time effective and will be automatically cleared with the shutdown of the computer.
Operating system:Programmers don’t need to write operating instructions to complete computer hardware related operations, such as keyboard, mouse, display input and output, and so on. They don’t need different structures of memory and IO, because these are completed by operating system instructions. The operating system overcomes the differences of other related hardware except CPU, An operating system can only support specific CPU (for example, the mainstream version of Windows operating system supports x86 architecture CPU, arm architecture is suitable for embedded Linux operating system, and Linux generally uses x86). At the same time, the operating system needs to provide different versions for different models. The same model can also install different operating systems, and the application software must be specially developed according to different types of operating systems, because the types of operating systems are different, and the API of the application program to the operating system is different. Here are some features of windows that are meaningful to programmers:
- 32 / 64 bit version of the operating system, the number represents the most efficient data size
- The system call is provided by API function set, and the API is provided by each DLL file. The entities of each API are functions written in C language
- A user interface with graphical user interface is provided
- Print out by WYSIWYG
- Multi task function is provided. Multi task function is realized by clock division technology, that is, multiple applications run simultaneously (multiple programs switch to run in a short time interval)
- Provide network function and database function
- Automatic setting of device driver by plug and play
Operating environment:Operating system + hardware. Each time you download a software, you will be asked to choose the running environment (operating system + Hardware: for example, based on Windows 7 version or above, more than 1G of memory and 500m of disk space) to download different installation packages. The code needs to be executed in a specific running environment.
Code running process:
C # is a high-level programming language, which is used to write programs, and the program is a set of instructions indicating each step of the computer. It is composed of instructions and data (for example, in the writeline (“hello”) program, writeline is an instruction, and “hello” is data). It needs to be converted into a machine language (binary code) that can be directly recognized and used by the CPU. Program (code) is stored in hard disk, disk and other media, while the program executed by CPU needs to be stored in memory (copied from disk to memory). Memory is the place where commands and data are stored, marked and specified by address.
It is necessary to write a good program with programming languageOperating environmentIn order to run, the running environment needs the support of operating system and computer hardware. If the running environment is different, the program cannot run. For example, the application running on MacOS, windows and Linux system can’t run in another operating system. Because the APIs provided by different operating systems are different, to transplant the same code to other operating systems, it is necessary to rewrite the parts of the application that use the API, such as keyboard input, mouse input, display output, file input and output and other peripheral devices for input and output functions, The operating system provides APIs for programmers to call. Under the same type of operating system, no matter how the hardware is, the API is basically the same. Therefore, the program written for the API of a specific operating system can run on any hardware. Of course, due to different types of CPU, machine language is not the same, so the local code is of course different. In this case, we need to use the compiler which can generate the local code for each CPU to recompile the source code. So is there any way for programmers to write code that can be executed in different environments? Experience tells you that it is certainly possible. The implementation principle is that we only need to develop different CLR runtimes according to different environments to realize cross platform. For example, Java language corresponds to Java virtual machine JVMs in different environments.
How does the code written by C? Become the machine code that can be executed by CPU? The flow chart is as follows:
C # source code file generates assembly file through compiler (vs or built-in msbuild). The file contains the following contents
- The standard Windows PE32 or PE32 + header can run on the 32-bit or 64 bit version of windows. If it is a PE32 + file, it can only run on the 64 bit version
- CLR header. The header contains the required CLR version, some identification flags, managed module entry methods, etc
- Metadata includes two kinds of metadata tables, one describes the types and members defined in the source code, and the other describes the types and members referenced by the source code.
- IL intermediate language code, compiler source code generated code.
Microsoft has created several language compilers for “runtime”, which can compile c + + / cli, C #, visual basic, f #, etc. into IL language for CLR,It carries the characteristics of multi languageLater, we can only develop high-level language for CLR programming.
Where: the process of C # source code compiling into assembly isCompile timeThe assembly is compiled into local code by JIT compiler, and the process executed by CPU is calledRuntime。
The JIT compiler in CLR compiles IL into CPU instructions on the platform
Under. Net platform, Microsoft officially only develops CLR running under windows, but does not develop CLR of other platforms, so it can not achieve cross platform. However, in order to make. Net cross platform, xamarin company develops mono virtual machine, which includes a real-time compilation engine, which can be used for x86, SPARC, PowerPC, arm and other processors, Later, it was acquired by Microsoft. Later, due to the change of strategy, Microsoft embraced open source and cross platform, redeveloped and defined the new generation version of. Net framework — dotnetcore. If you need to run the core code on different platforms (cross platform), you need to download the runtimes of different environments from the official website, as shown in the following figure:
The implementation of Java cross platform is attached
CLR is the core component of. Net. It is at the top level of the operating system and is responsible for managing the execution of programs. Its functions are as follows: