Machine instruction Programming Method

Source: Internet
Author: User

The machine commands that the processor can understand are a sequence of 0 and 1, which requires a lot of effort to write. Therefore, people have been working hard to develop various systems, programming is easy to understand. This section briefly describes how to compile a machine language program that instructs the processor to work.

Programming in assembly language

It is difficult for humans to write the series 0 and 1 that the processor can understand, and it is also prone to errors. In addition, after writing the script, let's look back at a pile of 0 and 1. I don't even know what commands will be sent to the processor. Even if a bug occurs, it's impossible to run normally.

Therefore, we write the addition command as ADD, And the subtraction command as sub (Subtract), Write the load command for reading data from the memory into LD (Load), Write the storage commands for writing data to the memory as ST (Store). This easy-to-understand writing is called a mnemonic (Mnemonic). In addition, the operands used by these commands can also be specified in a way that is easier for humans to understand.

Write programs using these symbols that are easy to understand, and then use the assembler (CyclerThe software can be converted into machine commands. Programs in this form are called "assembler", sometimes referred to as "assembler.

However, although it is easy for humans to write and understand, it is actually necessary to write machine commands one by one. For example, to add the result of adding A and B to C, perform the following steps.

LD R1, [a] Reading data from memory that saves data a is placed in r1

LD R2, [B] read data from memory that saves data B and put it into r2

Add R3, R1, R2 R1 and R2, and put the result into R3.

St [c], R3 writes the content of R3 to the memory address of the stored data c

Here R1, R2, and R3 are registers (Register), The processor uses them to save temporary data in computing and other operations.

Programming in assembly language is sometimes called machine language programming, but after all, it is not directly written into 0 and 1, and strictly speaking it is not programmed using machine commands.

In addition, after converting the assembler into machine commands, it is a bit sequence, so it can read data from the input device to the memory like data. The operation of reading a program into memory is called "loading a program ".

Use compilers for advanced language programming

In order to make programming more convenient, people imagine the following solution: Write the above computation into a well-known form (Advanced Programming Language) C = a + B, and write the program in this form, then through the compiler (Compiler) To the assembler (Figure 1.17 left ). In addition, the program is usually divided into a set of main programs and multiple subprograms. In this case, the main program and each subprogram must be compiled and assembled separately (Figure 1.17 (Linker) Synthesize a machine language program.

Figure 1.17 using compilers and assembler translators

In 1954, IBM's John Backus invented the Fortran language for scientific and technological computing and compiled the compiler. Later, the Fortran language added many functions, even now it is also used for the development of scientific and technological computing programs.

In 1972, Dennis Ritchie, a researcher at the Bell Research Institute, conceived the C language and developed the C language compiler to compile UNIX operating systems. Later, C language absorbed the object-oriented idea and derived C ++, C #, and other languages, becoming one of the main advanced programming languages.

Optimization

When a high-level language program is translated into an assembly or machine instruction, c = a + B is converted into the Assembly form described above. However, if the program saves A or B to a register in advance, it does not need to read it from the memory again. In addition, if the Add result is used only in the followed command, it does not need to be saved to the memory, but directly transmitted to the next command through the register, so that the operation can be faster. In this way, unnecessary operations are omitted according to the context to generate faster assembler programs, which is called optimization (Optimization).

When compiling an advanced language program, it can indicate how optimized and optimized the compiler. In general, the initial debugging phase is not optimized. After the debugging phase is complete, the compilation option is set to-O2 or-O3, which indicates optimization as much as possible to improve performance.

***

Currently, almost all programs are written in advanced languages and then converted into machine commands using compilers and compilers. However, some parts of the operating system and hardware operations that control the input/output devices cannot be expressed in advanced languages, so assembler programs are also useful.

Interpreted Language Programming

At first glance, interpreted language programming is very similar to using a compiler, but it does not compile, compile, or perform other operations on the string-written high-level language program. Instead, it interprets the program content during execution, execute the corresponding machine command sequence to complete the specified c = a + B operations. This method is called an explanation (Interpreter) Language.

Java and Ruby in programming languages are interpreted languages. Windows batch processing files, UNIX and Linux shell scripts can also be used as interpreted languages.

The interpreter language compiles the program at runtime and converts the source code into machine commands. Therefore, the execution performance is not high, but the program can be executed without the need to compile or compile the program, after modifying the program, you do not need to re-compile and assemble the program, which is very convenient and widely used.

Both the compiler and interpreter input are hardware-independent high-level languages, but the compiler output results are assembler programs or machine commands that correspond to the assembler programs one by one, therefore, the compilation result cannot be run on the processor of other instruction architectures. On the contrary, the interpreter uses high-level language strings as input execution programs, so as long as the computer system of the program is installed with Java, Ruby and other interpreters, no matter which command architecture computer, java and Ruby programs can be normally executed without having to prepare different binary programs for different command architectures, which is time-saving and labor-saving.

JIT compilation

As the number of interpreter programs increases, it is imperative to improve the performance of interpreter programs. Therefore, JIT compilation (Just In Time compile) (Figure 1.18), during the runtime, the part of the program that is repeatedly executed is converted to the machine language. JIT compilation method: first, explain the execution of a string-type high-level language program. Once the execution is found to be executed repeatedly, compile this part during the running process and convert it into machine commands with high execution efficiency.

1.18 how the interpreter with the JIT compilation function executes the source program

That is to say, the lower part of the program is interpreted and interpreted, while the higher part is compiled into the machine language to improve the execution efficiency. The program itself is still a string and will not become a sequence of machine commands, so it still retains the advantages unrelated to the processor instruction architecture, as long as the corresponding language of the processing program is installed, the program can run normally.

However, even if the execution frequency is very high, it can only be interpreted as a string before repeated execution is determined. In addition, the JIT compiler will increase the compilation time, so the performance is generally inferior to the machine commands compiled by the compiler. However, because it does not rely on the processor architecture, it is also applied in the business backbone system.

 

This article is excerpted from "Technology Supporting processors-a world that never ends up pursuing speed"

 

(US) hisa anteng;

Translated by Li Jianhe

Published by Electronic Industry Publishing House

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.