In-depth study of the first C language (cont.)

Source: Internet
Author: User

Readers who have not read the first article can click here to read the first in-depth study of the C language.

Question one: How do I print the address of a variable?

We use the address character &, we can take the offset address of the variable, and DS can take the segment address of the variable.

1. Global variables:

We see that the global variables here are in the data segment.

2. Local Variables:

We see that the local variables here are in the stack.

question two: study main The offset address of the function is the source code in the main The relationship between the defined positions of the function.

We print the offset address of the function, in the process of printing we can find:

When the program is coded as follows, the result of the program running is:

The program's F1 function and the F3 function are exchanged, and the result of the program operation is as follows:

As you can see, the position of F1 and F3 has changed, and the change is reversed.

We also know that the C language has such a way of defining function declarations:

We look at his results:

We see that, in the first way, the offset address of the f1-main increases in order, and in the second way, the offset position of the F1 and the F3 is reversed. In the third way, the offset address of the f1-main is still increased sequentially. From this we can conclude that the function of C language program starts at 01FA. are arranged in the order in which the functions are implemented. Here, the function starts at 01FA because a portion of the fixed-length content is added before the function we write, due to the process of compiling and joining.

Question three:

Read TC2.0 "helpme!" in the full directory. DOC"To resolve the following issues:

A) What is the difference between TCC.exe and TC.exe?

b) What is the difference between EXE files generated by TCC.exe and TC.exe?

First we refer to the documentation for the description:

The difference between TCC.exe and Tc.exe: TC.exe is an integrated environment that is a command-line compiler integration editor, linker, and debugger. And TCC.exe is just a command-line compiler.

TCC.exe and TC.exe generated EXE files are different: Q: why. The EXE generated by TC is larger than the file generated by TCC.EXE. In the default configuration, the TC.EXE generated EXE contains debugging information. And the TCC.EXE generated No.

Question four:

further through the debug two programs were observed by TC.exe and TCC.exe the generated EXE document, Understanding TC.exe and TCC.exe different optimizations for the code. The two programs are:

a) print only "Helloworld!" "The procedure;

b) a program with a sub-function with parameters.

First we look at the print "Hello world! "Program:

Source:

Look at the size of their compiled file:

Since it's a different point, we'll try the disassembly: The disassembly code at the beginning is the same, we're directly to the 01FA. Found:

On the left, after the TC compilation, the right image is the TCC after compiling

There is a noticeable difference here:

Let's go ahead and look at some:

On the left, after the TC compilation, the right image is the TCC after compiling

We can see the TC compiled program, the Register protection is more comprehensive than the TCC after the compilation.

Let's look at the program with the parametric function:

Compile separately and debug disassembly to view:

On the left, after the TC compilation, the right image is the TCC after compiling

We see that the left figure is more of a register-protected statement than the right figure.

For code optimization, TC discards the efficiency and file size to ensure the security of the program. TCC, however, has abandoned the program's security line to generate a streamlined C program that makes the program shorter and more efficient.

question five: 2nd Chapter, the program needs to print the function's segment address and offset address in the command run directly in the debug The offset address is the same as the segment address in which the print is run, what is the reason?

In this case, debug is used to debug the program, he can control the program to step through, and to see the status of the various registers in the run. To do this, debug must have his own way of controlling it. He needs to download the program from Debug Nega. And the CMD runs the program, is the system execution way. He only needs to accept the system call to execute. Because they operate in different ways, their segment addresses are different. However, after the program is compiled, the offset address within his program is determined (as if we were able to print the offset address of the main function, indicating that the address is deterministic). And each program has a maximum of 64K of the program segment and 64K data segment and the stack segment of the mixing segment. So each time the system is allocated, each program is assigned a fixed size but different memory (a 64K program segment, a 64K data segment and a mixed segment of the stack segment), that is, the stack address fixed, offset address from 0000-ffff memory.

question six: 2nd multiple DOS at the same time What is the reason why the window loader, which prints the same segment address and offset address, is the same?

In the assembly language book, note 1 describes the 3 modes of operation of the Inter series microprocessors.

(1) Real mode: work equivalent to a 8086.

(2) Protection mode: Provide the working mode of supporting multi-tasking environment and establish protection mechanism.

(3) Virtual 8086 mode: You can switch from protected mode to one of 8086 modes of operation. This approach provides a convenient way for users to run one or more of the original 8086 programs in protected mode.

And our Windows is based on 80386. We can work so easily, open two windows, one is working in protected mode of Word, one is working in the virtual 8086 mode of dBASE.

That is to say, our command is now operating in virtual 8086 mode. Since it is virtual, there is no correlation between the two command. The two command will not share a real memory (their memory is virtual). So the printed segment address and offset address are the same.

question seven: We use tc2.0 based streamlined development environment for a comprehensive study of why, how does this help us?

Using a streamlined development environment can reduce the problems we face and focus on solving the basic problems of C that I am currently studying and solving. And, in doing so, we can further study the deep-seated, necessary processes that C programs use in compiling connections.

issue eight: statement printf ("%x%x%x\n", Main, &main, *main); The result of printing is the same value, try to explain why.

We write a program like this:

This can indicate that printf can display constants. and & and * constants, the values of constants are displayed.

We know that in the process of compiling the connection, in fact, is carried out two times, the first time is to compile a variety of machine code, which is not known where the label is located, the second is the first time after the completion of the translation will be translated the address of the compiled label.

C is also possible in this case, main is translated into the main function of the offset address (a constant, the value of this constant is determined by the first compilation). In this way, it is also possible to indicate that (long) when main comes out, it shows the problem of the address offset of the segment.

issue nine: Use more methods to finish printing main function offset Address

We can use this method to print:

We also have this way to print:

Issue 10: TCC compact Environment compilation generated EXE the program in the file can have two maximum of 64k Segment , then when we need a code snippet or data segment that exceeds 64k What to do?

We verify the data segment by first calculating the size of the data segment:

As we know, an int variable takes up two bytes in memory, and the data segment and stack segment share a segment. The size of the segment is 64K, and we calculate how many int data the 64K data segment can hold. The answer is to store up to 32,767 int types. We write the following procedure:

We deliberately set the number of arrays to 32768, and the result is that the TCC error when compiling: saying that the data we define is out of range. This means that at compile time, the program automatically checks the length of the data you write, and if it is exceeded, the compilation does not pass. (Note that there are no TLINK.exe files at this time)

We set the value of the array to 32767 and compile it with the following error message:

This indicates that the build of the TCC has been completed normally, but because there is no tlink. EXE file, cannot be generated. EXE. We put in Tlink. EXE, compile again.

We found that it still indicates that the segment has exceeded 64K. Until the value is roughly changed to about 32500, no more error.

What is this for? Two possibilities are presented here:

1. The compile connector automatically keeps the stack for the program: Because the program inevitably uses the stack during the execution, the compiler automatically retains a portion of the stack memory for the program at the time of the connection. When it finds that this part of memory plus its own defined data exceeds 64K, an error is made.

2. The compiler connector writes content to a data segment during a compile connection: the process of connecting the program is TLINK.exe connecting the code in the C0s.obj, Cs.lib, Emu.lib, Maths.lib, and the program to build the. exe file. In this process, it is possible to add data to the program segment. Causes the program segment to exceed 64K.

For two guesses I have not yet thought of a very ingenious method to prove that only for the time being conjecture.

And: When our program has to be larger than 64K. We can request memory from the system, or use secure memory that no one uses, to write data or programs into this memory, to use the data from this memory, or to invoke code.

In-depth study of the first C language (cont.)

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.