In-depth study of C language Article 1, in-depth study article 1

Source: Internet
Author: User

In-depth study of C language Article 1, in-depth study article 1

I. Study Process

1.Chapter 1: Create a compilation environment:

First download TC2.0 and find the program and file related to the compilation connection:

(1) compiler: TCC.exe

(2) connector: tllike.exe

(3) Related Files: c0s. obj, cs. lib, emu. lib, maths. lib

Put the file in the C: \ C directory.

Compile a program to test our compiling environment:

Here we can see that the program is compiled normally. The. exe file is generated. And can be correctly executed.

Of course, in TC, c0s. obj, cs. lib, emu. lib, maths. the four lib files are in the lib folder under the TC directory, but if we put the lib folder directly under the C: \ C directory, the program will prompt during compilation:

C0s. obj: Unable to open file

This is because the c0s. obj file in the lib directory is not found in TCC. we can infer that TCC searches for files only in the same-layer directory by default.

Here we found that TCC is used and TLINK is not used. So what is the role of TLINK?

We delete TLINK. Then compile the connection. We can see:

We can see that TLINK is actually called by TCC to implement the function.

The explanation in the book is: TCC. EXE compiles a. c into a. obj

Tcccall tlinkto connect the relevant code in c0s.objuncs.libuncemu.libuncmaths.libto generate the. exe file together.

Although we did not generate .exe, we found that 1.objwas generated. So we can't connect this 1.objfile to .exe after retrying the tlinkjob?

Let's try:

We found that the connection was successful.

When we compile with TCC, the program can have two segments up to 64 KB. One segment is a code segment, and the stack and data segments share one segment. How can we verify this? (Note: The following content is actually used here ). We compile such a program to display the CS, SS, and DS values when the program is running.

Let's compile and run and view the results:

We found that CS is a value, and DS and SS values are equal. This verifies that the code segment is a segment, and the stack and data segment share a segment. We also know that each segment address remains unchanged, and the offset address is 64 kB from bytes to FFFF. Therefore, the maximum value of the two segments is 64 K.

In addition, if you enter TCC directly in CMD, the TCC usage parameters are displayed, as shown below:

2.Chapter 2: show the segment address and offset address of the function:

We will continue to study the content of Chapter 2:

Add a statement to the main function so that the following program can print the segment and offset addresses of all functions.

The procedure is as follows:

The most direct idea is to use the address Retrieval Method for viewing. We know that in C language, the role of & is to take the address. For example:

The result is as follows:

Can the function also obtain the address like this? Let's try:

Here, can we directly add an address like & f1 and a function name? Analysis: In debug, we can see that subprogram calls are implemented in the CALL (Address) method. Here, function names and labels play a similar role in facilitating programming and compiler compilation and linking. It is essentially an address value.

Let's compile it directly to see if an error will be reported and confirm our conjecture.

We found no error. It means that the function name is translated or similar to the label.

For ease of viewing, We will display these addresses in hexadecimal format. The result is as follows:

Is the value we found the function entry address? Go to debug to view the information:

We can see that in 01fa, It is the statement in the F1 function we define, int a = 1; that is to say, our idea is correct, in printf, you can directly obtain the address output of the function.

However, we can see that the offset address of the function is printed. How can we print the segment address?

We know that in the C language, we can directly call some assembly registers, while in the Assembly, the CS register records the segment address of the program segment, that is, we only need to display CS, the segment address of the program segment is displayed.

We wrote:

The result is as follows:

But is the result correct? We have to verify from debug:

We can see that the-g running result in Debug is the same as the CS value. Both are 0b3b. This shows that the program is the segment address of the program during execution.

Why are the IP addresses of the program segments displayed in the two executions 2.exe different? We know that the first time we run 2.exe directly in the pipeline, the program is executed by the system call itself. For the second time, we loaded the system with debug and then executed it. During debugging, the program is loaded to the specified program segment by debug.

Ii. Additional research:

During the study, I found that when the C language shows the offset address of the variable, the address values of different variables are different.

For example, global variables are shown as follows:

Local variables are shown as follows:

This result made me confused, but I remembered the difference between local variables and global variables, and saw a value like-32, I guess this is the relative position rather than the absolute position recorded by local variables. Because absolute positions do not show negative numbers. The program I wrote is as follows:

In debug, I am-g to the second printf (); Before the function, that is, after displaying the address of variable.

Calculate the value of the current SS: SP and the value of-28, view the result unit, and find:

The number in this unit is the value of variable a, that is, the unit is the storage unit of variable.

Therefore, in C language, the address of the global variable is to record the offset address of its storage unit, while the address of the local variable is to record the relative location of its storage unit and the current top pointer of the stack.

(Annotation, important: Later I looked back and found that the % d method is used here, which is very important. Because the address is FFE4, which is a hexadecimal number, it should be displayed in % f mode. It is the two different expressions and display methods that have resulted in this phenomenon. Rather than the conclusion above. This problem is retained for the completeness of the process. Make corrections here .)
I want to study the C language in depth and recommend the book to read.

Works of The father of C Language <The C Programming Language> must be viewed
Then, C expert programming, C pointer, C traps and defects are all very classic.
After reading this, it's definitely a big bull!

First, let's look at the first line.

What should I read from the C language?

The C programming Language is a classic entry, because Dennis M. Ritchie is one of c developers.
However, you must have a certain c base. It is best to have a c base before. Otherwise, do not read it.
There is no nonsense in this book. We use examples to introduce some features of c. Some common algorithms are introduced.

Besides the appendix, It is very thin and only contains 150 pages. I can read it several times. I didn't see it once and it had different gains. It also had related exercises.
The exercises above are compiled on the machine. Basically, you have no problem with c. You should look at the algorithm and data structure.
Be sure to be rigorous when programming. You can refer to the various styles and then choose one to implement.

The books below have their own focus, and you will naturally know which one you need to read

Related Article

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.