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

Source: Internet
Author: User

1. Regarding the following procedure, where is the copy of the structure, which is copied into memory?

We enter debug to disassemble, single-step and other operations to track the view. Found:

In main, we see that call 0266 should correspond to the jump to the Func execution.

Here, after the Func assignment is completed, it is also call to the 0b3d:13ea where it should be the function that it replicates to memory. We look.

First we look at LDS: The instruction to remove the 32-bit address from the memory. The function of the les:les (load ES) directive is to load the low word of the double-word operand in the specified position in the memory with the register specified in the instruction, and the high-order character into the ES register.

We perform in the view:

We see here that Di and Si are put in two numbers, presumably to replicate the numbers on both sides. And through the context of the program, we can assume that the di is the FFD0 at the same place data copied to 0428. Let's look at the data at FFD0:

Look at the data after execution:

We see that the data is actually placed in 0428 places, and there are programs in subsequent programs that are assigned values from 0428 to the stack. So we are sure that the structure is copied to 0428 places.

So what is the reason for the program to do so? We know that when the normal variable is returned, the program returns with the value of the variable in ax (a long type, such as a four-byte variable, is returned with Dx,ax, which is no longer mapped). But the structure variable, his length is indeterminate, the program does not know how long the length is, and, in most cases, the length of the structure is greater than four bytes, the normal return does not meet this requirement. So the program chooses the first address of the struct variable returned by AX. So why should there be a replication process? We know that the variables inside the function are local variables and are in the stack. After the function call is complete, it is freed, and if we have an address in the stack, the data disappears. So, there is a mechanism to copy the address from the stack to the data segment, in the data segment, to the previous layer function.

2. About the jump statement in the assembler, why use jump, about the return value of the study (1), the return value of one, jump directly to the next instruction? (2), how is the return value handled when it is multiple?

We write the following procedure:

We see how it is implemented in debug as follows:

As we can see, when the F function returns, after the function return (after putting the return value in Ax), the next sentence is jmp 020f. And 020f is a RET statement, what does that mean? It is easy to think that, in a function, after the return statement has been executed, the function ends up returning. Let's write the following function:

Then we debug into the view:

As we can see, there are jmp statements after the two return, and all of them are the jmp-to-ret statement. That is, in any case, the function exits whenever a return statement is executed. The exit is exited with a RET statement. I feel that the benefit is that, first of all, the function has a single start address, and there is a single end address, and secondly, this reduces the amount of a portion of the statement, avoiding multiple RET statements in the function.

So, what if you're returning multiple values? We edit to view.

We see that the same JMP statement is still used when we return here.

From this we can conclude that the JMP statement returns after the execution of the function, relying on the JMP to RET instruction.

3. Make a comparative summary of the global variables, local variables, when the parameters are applied for memory, and when to release the memory, respectively.

Global variables

Local variables

Parameters

Request Memory

is assigned in the statement of the program before the main function is called

is allocated in the stack when the function is called

is created in the prepare statement before the main function calls a function with parameters

Freeing memory

After the main function call is complete, the program is freed during the release of the resource.

When the function ends the call, the space of the local variable in the stack is freed.

When the function call finishes, the argument's space is freed.

4. modifier static, auto, const, register role?

First we look at the description of the four modifiers:

extern external variables

Static statically variable

Auto Storage variables

Register variable of Register

We then write a program that looks at their respective roles.

We found that:

Say a can no longer be defined in main. We change the program:

After compiling the connection we view:

We found in conjunction with their respective names that the Register register variable has its value in the register, and the auto auto-store variable is automatically converted to a local variable.

Let's look at this more intuitively:

We see:

extern external variables are placed in data segments similar to global variables

Static variables are placed in data segments similar to global variables

Auto storage variables are placed in a stack similar to local variables

Register variable placed in register is a register variable

5. Static local variables always occupy memory space, what is the purpose of C language design?

For static local variables, we write a program like this:

We look at the results of their execution:

We see that the static variables in F1 are not eliminated at two calls, but continue to stack upward (this is due to a++). Moreover, this variable is not the same as the variable a value in the upper function f, which can be assumed to be not a variable. Let's look at what happens when we define a static variable in two functions:

We look at the results of their operation:

Here we see that the static variable in F1 is still different from the value of the static variable in the upper function f, and we look at the result of the disassembly:

Here we can see clearly that these two static variables are adjacent to the two variables. The F1 function, when calling the static variable A, is always a variable at 042c. It does not want local variables to be released at the end of the function, but is always present in memory.

What are the benefits of doing this? This makes the variable types in the C language richer and more free, and when we want to define certain counts of variables, it is more convenient to use the static variable directly. In other words, C language in the design, take into account the different procedures for the use of different variables in the C language to be implemented.

6. Are the variables assigned in the same order as they were stored? What is the definition of different types of variables? (Assigns the initial value, does not assign the initial value, one part assigns the value)

Let's start by looking at the relationship between the order of assignment and the order of storage when defining the same type:

We can see that in this case the variables are assigned in the same order as the stored order, but it is not clear whether the order of assignment is the same as the order of storage, or the order in which they are stored.

We then look at the relationship between the different types of variables, the order of assignment and the Order of storage:

It is found here that in this case the order of the variable assignment is still the same as the order of storage, but it is still unclear whether the order of assignment is the same as the order of storage, or the order in which it is stored.

We then look at whether the order of assignment is the same as the order of storage, or the order in which it is defined, as in the order in which it is stored.

Here we see:

So we can explain that variables are defined in the same order as they are stored. And their order is contiguous.

We write a function like this:

After careful study of its disassembly instructions, we found that the space allocated was biased. We analyzed and found that the int b,long D,long e did not allocate space. What is this for? We see here that these variables are defined and are not used. Is it because there is no use, so the program is not assigned to it? We will do an assignment:

We found that after D has an assignment statement, the program assigns d a 4-byte memory space between C and F.

So what good is the program doing? No memory is allocated to variables that are not used throughout the program, so it does not cause the program to have logical or run-time errors because the program does not use that variable. It can also reduce the allocation of unused memory, making the program more efficient and more space-saving. This is a mechanism by which C language is a variable assignment.

7. What is the function declaration? Does the function declaration have corresponding code?

First we write a program:

We look at its disassembly code:

found that the function declaration does not correspond to the function code (if any, it should be 01FA, because from the previous program we know that the length of the statement before 01FA is fixed, if the previous function declaration of the Code, the Mian function corresponding to the 01FA address will be shifted accordingly, there is no back-shift, There are no statements at 01FA. The function declaration does not correspond to the function code. )

So what is the function declaration for? Here, I think (here no longer shows that there are return values, the disassembly code of functions called by parameters) function declaration is to take different implementations of different functions at compile time. For example, for a function with parameters, you should pass in the parameter before you begin, and you should release the parameter after the end. Functions that have return values should not be allowed to have unprotected values in ax before they are called, and so on.

8. Study the three forms of structural parameter transfer: (1) structure variable as a whole, (2) struct variable Some member variable is passed as parameter, (3) pointer passing of struct body.

We have already studied the structure variables as a whole, and the principle is to pass the data in the data segment to the stack, so as to transfer the structure variables, we study the structure variables and some member variables to do the parameter passing.

We see that such parameters are the same as the usual parameters, which are called before the stack, and then used in the stack when used within the function.

We're looking at the passing of the pointer type:

We see that the passing of a pointer type is to pass the first address of the struct variable into the function.

In-depth study of the second chapter of 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.