Deep understanding of computer programs

Table of contents

program storage

Program compilation process


Hello everyone, this time we will talk about how programs are stored and compiled from the bottom of the computer.

program storage

Let’s take the simplest program as an example:

#include<stdio.h>
int main()
{
  printf("hello world");
  return 0;
}

Take the code above as an example. The hello program cycle starts from a source program (also called a source file), which is also a text file we create and save through the editor. The file name is hello.c. The source program is actually a bit sequence composed of 0 and 1, and 8 bits are a byte. Each byte represents some text character in the program.

Modern computer systems basically use ASCII to represent text characters. In this way, each character is represented by a unique single-byte integer value.

The above code is expressed in ASCII as follows: (SP means space)

The hello.c program is stored in a file as a sequence of bytes. Each byte has an integer value corresponding to some character.

For example: the integer value of the first byte is 35, which corresponds to the character "#". The second byte has an integer value of 105, its corresponding character is "i", and so on.

Pay special attention to the fact that each text line ends with an invisible newline character "\n", whose corresponding integer value is 10.

A file like hello.c consisting of ASCII characters is called a text file, and all other files are called binary files.

Program compilation process

The life cycle of the hello program starts with a high-level C language program, because this form can be read by humans. However, humans can read it, but computers cannot. Therefore, in order to run our hello.c program on the computer system, each c statement must be converted into a series of low-level machine language instructions by other programs (that is, the machine language, a language that the computer can understand) These instructions are then packaged in a format called an executable object program and stored as a binary disk file. An object program is also called an executable object file.

Here, the compiler driver reads the source program file hello.c and translates it into an executable object file hello. This process can be completed in four stages, as shown in the figure, in which the programs ( preprocessor, compiler, assembler, linker ) that execute these four stages together constitute the compilation system.

  1. preprocessing stage. The preprocessor (cpp) modifies the original C program based on commands starting with the # character. For example, the #include<stdio.h> command in line 1 of hello.c tells the preprocessor to read the contents of the system header file stdio.h and insert it directly into the program text. The result is another C program, usually with an .i file extension.
  2. Compilation stage. The compiler (ccl) translates the text file hello.i into the text file hello.s, which contains an assembly language program. The program contains the definition of function main as follows:
  3. Assembly stage. Next, the assembler (as) translates hello.s into machine language instructions, packages these instructions into a format called a relocatable object program, and saves the result in the object file hello.o. The hello.o file is a binary file, and the 17 bytes it contains are the instruction codes of the function main. If we open the hello.o file in a text editor, we will see a bunch of gibberish.
  4. link stage. Note that the hello program calls the printf function, which is a function in the standard C library provided by every C compiler. The printf function exists in a separate precompiled object file called printf.o, and this file must be merged into our he11o. in some way. in process. The linker (1d) is responsible for handling this merging. The result is a he11o file, which is an executable object file (or simply an executable file) that can be loaded into memory and executed by the system.

The ASCII reference chart is as follows:

Guess you like

Origin blog.csdn.net/zsd2829568515/article/details/134635362
Recommended