Compilation 3-How a computer program works

Source: Internet
Author: User
Tags arithmetic
1. Introduction

We have already mentioned in the previous register and the CPU of some basic working principle, then in a program run, register, CPU, memory, etc. are the role of what? The purpose of our study is to better understand how the bottom of the computer works, so I think it is necessary to talk about how a computer program is running on a computer before learning the assembly instructions. This article describes, as much as possible, what is going on inside the computer from writing a source program to the process it is executing. 2. How does the computer work?

From the macroscopic point of view, the computer is to provide services, to provide convenient. Humans want to tell computers something, by what means. input Devices (such as mouse, keyboard, etc.); the computer wants to tell humans that something will also go through the output device (e.g., screen, printer, etc.); When the computer reads the user's data, the data is processed, and the operator is used , and the result of the operation may not be immediately output to the user, but temporarily stored, then the computer will use the memory , regardless of the process of operations or stored procedures, logic flow arrangements, sequence of steps need a controller . Human interaction with computers is through the co-operation of these five components, or the work of computers is achieved through these five components.

Modern computers are basically designed and manufactured in accordance with the von Neumann architecture. The core concept of the architecture is that the computer of the storage program, that is, the execution of the program stored in the computer, is implemented by the execution of one instruction (three stages: fetch instruction, instruction decoding, execution instruction). This is how the computer works from a microscopic point of view.
————-excerpt from How the computer Works 3. The process of the source program to the target program

1. Storage of the source program

Let's describe this process in the best known program, Hello World.


It is believed that the reader knows that the computer is represented by 0 or 1 strings, so our Hello World program exists in this form on the computer, 8 bits are one byte, and one byte represents some characters in the program, that is, each character corresponds to a 8-bit integer value. This integer value is the ASCII code.
So our source program is represented by ASCII code:

These ASCII codes are now displayed in decimal notation in front of us, but in the computer there is a string of 8-bit strings consisting of 0 and 1. This ASCII-saved file is a text file, the other way to save the file is a binary file.

2. Compiling the source program
Although we can easily understand the source program, but the computer does not understand what these things, not to speak of running our hello.c, so we have to let the computer can also understand our meaning, this time we need to compile the system to help. The role of the compiling system: translating the source program into low-level machine instructions that some computers can understand. Composition of the compiled system: preprocessor, compiler, assembler, connector
Workflow for compiling the system:
3.

1. Preprocessor : Modify the source program according to the command starting with #. For example, based on the # include stdio.h> line, the preprocessor reads the contents of the system header file Stdio.h in place of this line content. After the source program is preprocessed, another C program is obtained, and this program is usually saved with a. i suffix.

2. compiler : Converts the preprocessed. I file into a assembler. The compiler translates different high-level languages, such as C and C + +, into tightly-consistent assembly-language formats for output. The assembly language is exactly described in the standard text format per machine language instruction. The files that the compiler obtains are usually saved with a. s suffix.

3. Assembler : Translates the assembly language (. s file) into machine language instructions and packages these instructions into a format that can be used to locate the target program. The resulting file is a binary file, usually with an. o suffix. linker : The printf function is called in the Hello,world program, which is a function in the C standard library. The printf function is stored in a separately precompiled file named PRINTF.O. This file must be incorporated into our program in an appropriate manner, and this work is done by the linker. After merging the external. o Files, you get a complete Hello,world executable file. After the executable is loaded into storage, it is executed by the system copy.

In Unix systems, the conversion of the source program to the destination program is performed by the roommate compiler driver.
Input command:gcc hello.c-o Hello
The above steps are performed and the target binaries are generated. 4. Running the target program

Now that we have the target program, it's time to run it.
Enter the program name in the UNIX system:
When the user enters a line of commands, the shell first determines whether it is a shell built-in command, and if not, the shell assumes that the user entered the name of an executable file to load and execute the file.
Note:

The source program is placed on the disk.
Load: Copies a byte or a word from main memory to a register, overwriting the original value in the register.

Now let's take a look at the details and what the computer hardware does.

The image above is a computer with some basic hardware components.
Some of these hardware require special instructions:

1. Bus : Transfer data between individual parts.

2.I/O device : IO device is the channel that the system communicates with the outside world, such as mouse, keyboard, monitor, disk are typical IO devices.

3. main memory : for short, the processor is used to temporarily store the program and its data when executing the program. Main memory consists of a set of dynamic random memory chips. Which is what we usually call memory, which is not the same thing as a disk, it's a memory bar in our computer.

4. Processor : That is, the CPU, explaining the execution of instructions stored in main memory. Its internal PC always points to the address of the next instruction to be executed. The operation of the processor is mainly around the PC, ALU (arithmetic \ logic unit), main memory to operate

The actions that the CPU may perform in accordance with the instructions are:

load : Copies a byte or a word from main memory to a register, overwriting the original value in the register.

Storage : Copies one byte or one from the register to main memory and overwrites the original value in main memory.

action : Copy the contents of the two registers to a register in which the original value of the register is overwritten after the arithmetic operation of two characters is Alu,alu.

Jump : From the instructions executed by the CPU to extract the contents of a word into the PC, overwriting the original value, thereby changing the next command to execute, to achieve the purpose of the jump.

Initially the shell program is in the execution state, waiting for the user to enter commands. When we enter ". \hello" in the shell environment, the characters entered are read into the register and sent to the master.

After you press ENTER, the shell program knows that we have entered the completed.
Because our executable file is on disk. So the next thing to do is to copy the code and data of the disk into the main memory (by means of DMA loading the program, you do not need to pass the CPU, but the hello executable directly from the disk to memory), as shown below

After the executable program is loaded into main memory, the CPU executes the machine instruction of the Hello program, which is done by copying the "hello,world\n" characters from the main memory to the register file. Then copy it from the deposit file to the display device for display, as shown in figure:

In this case, the Hello World program has been completed from the source program to the successful execution of the entire process. 5. Expand

1. Cache

In fact, the above hardware composition does not meet the current computer, from the above can be seen in memory directly with the register for data exchange, which is actually a waste of resources, because the CPU is now processing speed is very fast, but the memory of the read with it is particularly slow, if the register directly with the memory to deal with, The efficiency will be very low, so the predecessors introduced the cache. Cache is a transition between register and main memory, and it combines the advantages of both. The cache can hold more data than registers, and reads faster than main memory. So many of the data that needs to be used frequently can be placed in the cache, so that it does not have to visit main memory every time, greatly improving the efficiency.
Later, the idea of inserting a transition device between a faster processor and a larger, slower memory was popularized, resulting in a later memory hierarchy:

2. Operating System Management hardware

In our Hello program, we did not directly operate the keyboard, monitor, disk, etc., these things are the operating system to help us do. The operating system is actually a layer of software between the hardware and our referencing program.

The operating system has two main functions: to prevent the hardware from being manipulated by runaway programs. Provides an easy and consistent way for applications to manipulate complex low-level devices.

Progress a little bit every day

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.