Start with HelloWorld, understand the program

xiaoxiao2021-03-06  18

Say in front: Although notes should be more and better. But who called me a rookie, if it is too simple to write today, I can't understand it in tomorrow. There are other reasons, I see the English version, I have already afraid that my understanding has an error. If I streamline, it may be confused tomorrow. And everyone sees that the error is also easier to point out. The rookie's notes are relatively long, in fact, I also try to write less, I hope you will take a lot. #include

INT main () {std :: cout << "helloworld"; return 0;} Even this simplest program, computer and compiler to make it work, the basic action and a large program is implemented the same. Since it is a learning programming, it is necessary to understand how the computer works. I want to know how the program I have written is using computer, although it is a primary unable to primary program. In this understanding process, I have to mention the common sense of computer system, and I think this is good, at least makes this very abstract, how much it seems to be specific, image points. 1 Information stores write this source, I saved it as a text file, saved as hello.cpp in the VC environment. Then, for the computer, this file is just some bit (BIT) signal, each bit is either 0, or 1. Typically, one byte is formed every 8 bits, each byte is used to represent a character in the file. It is easy to think that 0 and 1 will be combined into larger binary numbers, and a number indicated by one byte can range from 0 to 256. The characters are represented by numbers, usually the ASC code. This file is actually like this, all are numbers: # i nclude space

Tell the preprocessor read the content about iostream in the system, and then insert it directly into the text file of the source program, resulting in a new file. The second step is to compile. This new file is converted into a low-level machine language instruction, and it is still saved as a text file. The third step is to link, there is something like library functions in the program, such as Cout is a class, which exists in the library. The link is to merge it into the compiled file. This generates a target file, and finally generates an executable. Take open vc / myproject / hello / debug /, you can see an intermediate file generated with a target file .Obj, pre-compiled head file .PCH, link .ilk. 3 Execution of the program When we double-click the executable file hello, what is the opportunity? This signal from I / O (here is the mouse) will contact the CPU through the bus and I / O Bridge, and the CPU issued an instruction, and the computer immediately copied the target file on the hard disk to memory. The process of replication will not disturb the CPU, but use direct storage (DMA). When the code and data are completely copied to memory, the CPU performs compilation machine instructions, first is the main program main. The Hello's instruction is to copy "HelloWorld / N" string to the register within the CPU and send it to the display from here. 4 Operating System From Write Programs to the executor, never left the help of the operating system. The operating system can be used as an application and hardware interface because the computer is managed by the operating system. The application must use the hardware to operate the system. This has to mention the process because you may be listening to MP3 while programming. The operating system works this way. When you want to run Hello, the operating system will assume that only this hello is running, Hello will completely account for CUP, memory, I / O devices, Hello will not be disturbed, this concept is the process. The operating system is now handed over to the Hello program. This process is completely Hello owned, and other programs cannot use this process to access hardware. Why is your MP3 still stops because the computer uses a multi-process. Every process is very exclusive, only loyal to a program. A multi-process makes all running programs that it seems to be on. In fact, they are alternate, and after the CPU has finished an instruction, it will wait for other hardware to feedback. This time, the CPU will perform instructions for other processes. This is also true of other hardware. 5 Virtual Memory Since the operating system always recognizes that there is only one program in using all computer resources, in order to meet the operating system, virtual memory makes each process thinks only yourself in using memory. The reason why the virtual memory is placed on the hard disk, and allocated different areas, each process has its own unique area. At this time, memory is used as a cache. Virtual memory space makes each process consisting of some defined, target-clear areas. I. Data and Code Area: The code is always starting at a fixed address, which is tightly following the address of the global variable of the C language. The area of ​​code and data is initialized by executable programs, such as Hello. Second, the heap: The data and the code area are followed by running the heap, the area of ​​the data and data is unless, and the size of the heap is not fixed. It will telescopic, as for "stretching" or "zoom", look at C , this is a dynamic allocation problem, when using new, the stack is "stretched", delete "shrink".

转载请注明原文地址:https://www.9cbs.com/read-42279.html

New Post(0)