In-depth study of c language article 2, in-depth study of c Article 2

Source: Internet
Author: User

In-depth study of c language article 2, in-depth study of c Article 2

1. Procedure 1:

First, we will study the following procedures:

Answer the following questions:

1. In which register is the segment address of n, a, B, and c running?

What is the storage space of global variables in? What is the storage space of local variables? What are the parameters in? Where is the function return value stored?

When will the storage space of global variables be allocated? When will it be released?

When will the storage space of local variables be allocated? When will it be released?

2. How does function f3 call and return function different from function f1 and function f2?

After the compilation is complete, go to debug.

First, we execute the main function and then start the single-step execution. We can see that each time a single step is executed, operations involving data or data assignment are involved. debug displays the address and current value of the address to be operated. Let's verify whether the address and value are correct:

Since the value of n in the source program is 0, it is difficult to verify whether the value is correct. We will temporarily change the value of n to n = 10; we verify the following:

(DS: 01a6 value after the statement is executed)

(The value at DS: 01a6 before the statement is executed)

As you can see, when the n = 10 statement is executed, the address to be operated is DS: 01A6 on the Right of debug. After the program is executed, the value at DS: 01a6 is changed to 0A, that is to say, the n address assigned is here. Therefore, we can determine that the address and current value of the debug step are credible.

The position of each variable is determined based on the debug value of each step:

Here, n = 0; the address of the global variable is DS: 01A6, And the DS segment value is 0B96.

After running this command, we can see the address of the global variable n.

Continue to execute. When we call the f2 function. We can see that parameters 2 and 1 in f2 (); are pushed into the stack. In addition, the order in which parameters are pushed is from right to left. The segment address is of course SS.

When a parameter is called in f2, it is called by BP plus offset. Finally, the result is given to AX. Here, the variable C is not assigned a value in the memory, but stored directly using SI and AX. When a parameter is called, the segment register is SS.

In addition, when exiting the f2 function, we can see that:

In the inbound stack, the values are AX (the second parameter), AX (the first parameter), BP, and si. While the output stack only outputs SI and BP from the stack, and does not output AX from the stack. At this time, the parameter is released.

Then the program returns, so here we determine that the return value of the function is returned through AX.

When n is assigned, the value of the variable AX is assigned to n.

N is also assigned here.

We are writing such a program:

Here we can see that local variables are defined in the stack segment. In addition, we can see that there is sub sp + 02 when the function is called, so we can see that the variable space is allocated when the program calls it.

Therefore, when the program is running, the segment address of n is DS, the segment address of A is SS, the segment address of B is SS, and C is not in the data segment, and the Register AX is used.

The storage space of global variables is in the data segment, the storage space of local variables is in the stack segment, the storage space of parameters is in the stack segment, and the return value of functions is stored in AX.

The storage space of the global variable is allocated when the statement declaring the variable is executed, and is released after the program ends.

Local variables are allocated when the function is called and released after the function is completed.

The parameter storage space is allocated when the function is called and released after the function call is complete.

We compared the differences between f1 and f3 calls, and found that: 1. During the call, f1 is the call offset address, while f3 is the call segment address + offset address. 2. When the result is returned, f1 is a direct RET, f3 is a first RETF and then RET.

2. Procedure 2:

We compile the following program:

Q: How is the storage space allocation method for Variable n and a different?

After compilation, go to debug tracing.

We can see that A is allocated at the beginning of the program, and A is stored at the offset of 0194. N is pushed into the stack, which is allocated in the stack at this time. It was previously stored in SI. In addition, the C program compiler optimizes the program and simplifies the statement.

We do the Verification:

Program writing:

First, we can see that the C language is not translated in the order of statements in the C language. The underlying implementation method is, put n = 2 and n ++ together. And Variable n is placed in register SI.

3. Procedure 3:

The program is written as follows:

Problem:

1. Is the storage space of all variables in the program adjacent? In tc2.0, what are the storage spaces of integer, string, and long integer data?

2. What is the impact of different data types on Data calculation methods?

Debug to view the address of a, B, c, a1, and a2 when the main function is executed.

Note: The addresses of a, B, c, a1, and a2 are respectively 0194, 0196, 0198, 0199, and 019B.

The differences between them are: 2, 2, 1, and 2. We know that the size of int variables is 2 bytes, so we can see that these variables are continuous.

In addition, we can also see that the inc command is used for both int and char row data ++, while the long command is used for the ADD command. It indicates that different types of data have different calculation methods.

Here we can see that long variables are first added and adc commands are used. This is because long variables are 4 bytes in length, and there is no addition of four bytes in assembly. Only the lower two digits can be added first, and then the upper two digits and the overflow flag bit can be added (this is because the lower two digits may overflow when they are added, the value should be 1 to the upper two, while the adc Command performs operations with overflow bits. In this way, we can ensure that the value of the two digits is correct ). This completes a four-byte addition.

We query the variable length of TC:

We can infer that the variable address after a2 is 019F. We verify that:

Variables of different types are continuous, and long occupies 4 bytes.

4. Procedure 4:

Write the program as follows:

Q: How are the buckets of variables a, B and their data items allocated?

First, we analyze the Custom Data Type: It is composed of an int type and three char type variables. Such a data type should occupy 5 bytes in memory. Two custom data types, a and B, are global variables and local variables. During allocation, one must be in the Data Segment and the other must be in the stack segment.

After the compilation is complete, debug the variable in the Structure Body for verification:

We can see that in the global variable a of the stu type of the custom struct, the data items are arranged consecutively. The entire variable a is in the Data Segment and occupies 5 bytes.

In the local variable B of the stu type of the custom struct, the data items are arranged continuously. The entire variable is in the stack segment, and the Occupied byte is 5. In addition, we found that although the stack segment is operated, the PUSH command is not used here, And the MOV command is used. And we can see that at the beginning of the program,

Sp makes the corresponding modification and allocates the space of B.

5. Procedure 5:

We compile the following functions to see how struct variables are passed and returned:

Disassembly:

We can see that there is the LEA command in the Assembly, and LEA is the target address transfer command: Write a near address pointer to the specified register. Format: LEA reg16, mem16.

In the program, we can see this call. Combined with the context, we can determine that it is the called func function. Let's look at its disassembly code:

We can see that the program has given 0 b28 to ax, but we know that ax stores the register of the subfunction return value. So is ax a custom data type variable? Obviously, ax cannot store up to five bytes. So there is no possible offset address? For more information, see.

Sure enough, we found the storage address of the custom type a in the data segment. That is to say, the func function returns a variable of the custom type by returning its offset address in the data segment.

Let's look at the statement of the f function and see that the offset address of the f function is 0256.

Find the call statement:

We can see that F is a struct variable passed in with the stack.

Therefore, we can know that when a custom data type variable is returned in a function, it is returned by its offset address in the data segment. When it is called as a parameter, it uses the stack mechanism to pass in the function.


C language deep learning

After learning C, it is best to continue to learn C ++, because C is structured programming, and C ++ is surface-like object programming, and now C ++ is popular.
After good linguistics, we should first learn data structures and algorithms, because this is the soul of programming, so we must learn well and learn well.
As for the compilation principle, there should be no need to learn. If you want to crack the software, you can learn about the compilation principle, which is mainly about the working principle of the compiler, that is to say, I learned how to perform lexical analysis, syntactic analysis, and Syntactic Analysis for C language compilation software such as VC, which should be useless for game development.
The database should learn a little bit, because writing game programs also requires a lot of data, such as map data in any game, which is stored in a file, therefore, it is inevitable to deal with such large-scale data during programming, and processing of large-scale data is generally a matter of database, so it is necessary to understand.
You should also take a look at the operating system. Because the game you develop must go through an operating system, you should understand the operating system principles, however, we should focus on learning windowAPI, because most games are still dealing with windows.
Then, game development is the content of OpenGL and DirectX mentioned above, which is also very important. However, it is the next thing to learn well.

In-depth C language problems

Let me talk about it,
First, the language is just a tool. From this point on, I do not agree to change to another language. Since a language is always useful, it is always suitable for you to learn it.
Then, data structures and algorithms must be learned. If you have not read introduction to algorithms, we recommend that you take a look. If you have learned it, you should know what programming you are engaged in? If you only want to write a small program trainer, it is easier to do. You need to learn basic theoretical knowledge, such as networks and codec. If you want to do something, you can learn related things. Then you need to learn software engineering, develop software to follow some routines, and you should not write them in disorder without rules.
In general, what do you need to learn when you learn and write small programs on your own. Because of the strong connectivity of knowledge and strong cross-discipline, the application of computer will inevitably involve various aspects. It is impossible to finish learning only one programming language.

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.