I 've talked a lot of nonsense in the previous article, so today we will not talk nonsense. In short, we need to realize that the IT industry is a very large system, from hardware, welding capacitor resistance engineers to circuit design, to circuit board production, to software programming, to communications, network communications, serial communication, interface programming, WEB programming, database programming, database systems, audio programming, video programming, security programming, system Programming, application development, IT sales in the Computer City, system installation, computer maintenance, data recovery, graphic design, three-dimensional design, advanced special effects design, server management, network setup and so on, and every specific direction is very complicated. given so many examples, we don't want everyone to understand this. One person's energy is limited and we can only attack one direction, however, it is only one direction to be refined. If you do not want to learn this, you can also learn it. If you do not learn the real skills at the end of the course, now there is no shortage of second-class Talents in China. what you lack is first-class talents. All right, let's talk about it again.
Back to the topic, what do you want to talk about today? I feel that the person who learns C can find me in this article. It should not be a little white. At least you know about variable loops. If you do not understand this, you have to look for the basic syntax in this book.
Let's take a look at the host's knowledge points in 01, including the memory, address, byte, storage and range of integers, and the logical address. Let's talk about some program details today.
When we write a hello world Program, click build To Get A. exe file. How is it organized? What memory does it have. We call this kind of .exe program for PE file, it is Microsoft's COFF extension, about COFF, You Can http://en.wikipedia.org/wiki/COFF to view its detailed introduction, E is not good don't read it, it is a standard for the format of executable files. Microsoft has released its own COFF, namely PE, which has the location of a windows pe plugin. Just take a look at it, but you need to understand that there is a specification that limits it. If your program does not comply with this specification, it cannot be recognized by the operating system, that is, you can change an image to any image. EXE and then double-click the execution, the system will not execute, because it does not know, about PE You can understand the structure as long as you have the ability. You don't have to understand it if you don't have time. Just know what it is.
So what memory does the hello world.exe system allocate to it? Let's give you an answer first.
Stack, heap, code, and data can be regarded as the four main memory blocks for the moment, and these addresses are relatively stable in the XP VC6 environment.
Memory block name |
Start address |
Stack Zone |
Around 0X0012E000, but relatively stable |
Heap Area |
0X00380000 or so, but relatively stable |
Code Area |
Zero X 00400000 |
Data Zone |
Located in the Code Zone High address zone |
For the above table, we do not provide the range size, but only the starting address. It does not mean that there is no, but the size range is not necessarily. We can move the cursor over the code of printf, then press F9 next breakpoint, and then press F5 to start debugging. At this time, the program runs to the breakpoint just set. By default, a small window named debugging (toolbar) is displayed ), you can drag it and place it in the appropriate position. There are some buttons in the small window. We can place the mouse over it and there will be a prompt after a pause. The first one we need to find is the memory window, click it and the memory data window will pop up. The memory window can be adjusted. We recommend that you extend the window at the beginning, because we should check the data here.
1. Observe the logical address space of 4 GB. The memory window has an address text, which is an editing box on the right. You can enter an address. There are three columns below, the left column is the address, the middle column is the data in the address (hexadecimal), and the right column is the ASCII character corresponding to the data, so we can take a look at the address range for convenience, the range of the Logical Address Space in 01 is from 0 to 2 ^ 32-1 (32 1, that is, FF (I will name it 8 F ), if it is regarded as a 32-bit signed number, it is-1. What is not counted? See note 1). Therefore, you only need to enter 0 in the edit box to locate the start logical address, and enter-1 to locate the end logical address, we can drag the scroll bar to check it out.
Note 1: Here is a small review of 01 knowledge. First, this is a 32-bit signed number, so we only need to sum up their values.
1111 1111 1111 1111 1111 1111 1111
Actually it is (2 ^ 0 + 2 ^ 1 + 2 ^ 2 + .... + 2 ^ 30) + (-2 ^ 31) = 2 ^ 31-1-2 ^ 31 =-1 is still the formula in 01.
When we locate 0 or-1, we find that the values in these regions are all ?? What does it mean? Do you still remember that the address is mapped from the physical memory in 01, that is, the logical address space is a virtual space, and the real space is in the physical memory, the logical address is only MMU ing physical memory ,?? It indicates that this region does not correspond to the actual physical memory. which addresses are mapped to the physical memory? We have already shown in the table above. You can enter 0X12E000 in the edit box, then, check whether there is any data in this area. If not, click (activate window) in the memory window and press the PAGEDOWN key on the disk until you see the data, then look for the boundary of this area, as shown in Figure 1. It is the starting position.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U02114-0.jpg "title =" BMP 1.bmp "alt =" 124539502.jpg"/>
Well, we have seen the starting position of the stack area. Next, we need to locate the Ending position of the stack area. How can we locate it, in the memory window, click it to activate the memory window and press the PAGEDOWN button on the disk to flip down the page. What do you see ?? It turns out that you have already exceeded the lower limit of the stack area, and then slowly adjust it by PAGEUP. Finally, we find the location as shown in the figure. Of course, this address is not fixed, that is, it may not be the address on your computer.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U03001-1.jpg "title =" BMP 2.bmp "alt =" 124622661.jpg"/>
Well, I think everyone understands that the stack is a continuous memory. Since it is a memory block, there is a starting address and ending address.
2. Let's take a look at the heap area. The system applies for a heap area by default. When we use functions such as malloc to apply for memory, we actually apply for it in this area, so where is it in our logical address space? Let's modify the code a little, as shown in figure
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U01D8-2.jpg "title =" BMP 3.bmp "alt =" 124707932.jpg"/>
Then press F5 to start debugging. At this time, the program will normally run to the printf function and pause. We will move the cursor over the p variable for a moment, the value of p is displayed. If it is not displayed, try it several times. Here, the value of p is 0x0000007b8. Well, open the memory window now, enter this address in the address editing box. Do not forget 0X, as shown in figure
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U0F96-3.jpg "title =" BMP 4.bmp "alt =" 124803316.jpg"/>
Then we can flip the page up through PAGEUP to find the upper limit of the memory area, and flip the page down through PAGEDOWN to find the lower limit of the memory area.
Upper Limit chart
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U0Ab-4.jpg "title =" BMP 5.bmp "alt =" 124839358.jpg"/>
Lower limit chart
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U043D-5.jpg "title =" BMP 6.bmp "alt =" 124919594.jpg"/>
In this way, we locate the second important memory area, which is called the heap area. We use some functions such as malloc calloc to apply for memory in this area. Remember.
3. Next, let's take a look at the code area. In fact, part of the program we write is organizing machine commands. To put it bluntly, we are arranging programs at 01, which is the lowest level of understanding, let's take a look at what the machine code generated by the program we write looks like? In the above example, the debugging status continues. Click the last button in the debugging window, called the disassembly code window. This will pop up the disassembly code area corresponding to the source code, as shown in figure
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U03J1-6.jpg "title =" BMP 7.bmp "alt =" 124950729.jpg"/>
Here, we can see some assembly commands, such as push, call, add, mov, etc. We don't care what they mean at the moment. In this window, right-click and select Code Bytes, then some additional information is displayed in the window, such
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U0Da-7.jpg "title =" BMP 8.bmp "alt =" 125015784.jpg"/>
We will see some hexadecimal data in the black area (what, I don't know how to select it, please let us know, hold down the ALT key and then select it, this method is implemented in many software, and is also supported in VS, and in PUTTY, You out la). So what are they? They are the legendary machine bytecode, but they are represented in hexadecimal notation. If you want to, you can convert them into binary notation, which is actually tianshu, so there is an address in front of each row. This address is the logical address. That is to say, these bytecode are in our memory now. In what region, in the code area, haha, you know. As we have already said above, the Code area starts at 0X00400000, we can see that the address in front of each line is basically located in this region. Now let's locate it.
Code area limit
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U03311-8.jpg "title =" BMP "alt =" 125047843.jpg"/>
Lower limit of code Zone
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U02W6-9.jpg "title =" BMP 10.bmp "alt =" 125213163.jpg"/>
The Code area is still relatively large.
Is it slow to locate the problem? Is there any quick way? The answer is yes. Remember, these operations are only available in the debugging status. Click debug in the menu bar ), the following is a module. A dialog box is displayed, as shown in figure
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U02455-10.jpg "title =" BMP 11.bmp "alt =" 125128446.jpg"/>
As you can see, the address of Hello World is 0X00400000-0X0042BFFF. This is the code area.
Have you ever wondered where the data is loaded into the memory? You can guess, okay, let me tell you, in the PE file, that is, our. EXE file,
Let's just write down a piece of instruction and write down a piece of instruction with a hacker on it. Let's stop the program. Click Open file. The following dialog box is displayed.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U04150-11.jpg "title =" BMP 12.bmp "alt =" 125240458.jpg"/>
In the file type, select all files, select Open as Binary, locate HelloWorld.exe (in the Debug directory), and click Open. If everything works properly, you will see the content shown.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U0L93-12.jpg "title =" BMP 13.bmp "alt =" 125310642.jpg"/>
Press CTRL + F to search. What is the content in the Black Block? 6A 0C E8 01 6E FF 83 C4 04 89 45 FC. Search Results:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U04358-13.jpg "title =" BMP 14.bmp "alt =" 125334813.jpg"/>
The machine code generated by the program is written into .exe. When the operating system loads our program, it allocates memory space for our program, implements address ing, and loads our bytecode into the memory, in fact, not just the machine bytecode is written into it, but the string is also written into it. If you don't believe it, you can try to search for the string that our printf function also enters,
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U04351-14.jpg "title =" BMP 15.bmp "alt =" 125357795.jpg"/>
See. This is the introduction of the PE file. It should be understood that the data, code, and resources generated by the code we write are all written into the PE file, that is to say, they are organized and disciplined standard documents. You will not forget the rules we mentioned earlier to constrain them.
Well, another one is the data zone. The data zone is relatively simple. When we define some global variables in the program and some static variables in the function, these variables exist in the static data zone. Let's demonstrate them.
Modify the code. The result is shown in figure
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192U06300-15.jpg "title =" BMP 16.bmp "alt =" 125435618.jpg"/>
Enter the global variable name in the address bar to locate the address of our global variables. We can clearly see their values.
Do you still remember the small-end mode? We define 0X99887766, but it is displayed in the memory window 66 77 88 99. This is the small-end mode, we can also see the two values below. One is 0X11223344, and the other is 0X55667788. It is two other static variables we define. If we enter val directly in the address bar, it does not calculate the address, so you must write the name of the global variable. Our focus is on their addresses. In the 0X00427318 format, it is actually a large address end in the code area.
Well, this section introduces so many things. I feel that if you have understood these things thoroughly, your foundation can be said to be very strong. My advice is to help you lay a solid foundation, there is a saying that is good. The foundation is not strong and the ground is rolling. Haha, it's a joke. Today we are not talking about all this. The key is to understand the program partition. As for the role of several regions, let's talk about it later. 88 goodbye !!!!!!!!!!!!!
This article from the "Qian song" blog, please be sure to keep this source http://qianqianquege.blog.51cto.com/8004200/1304552