Function parameter transfer mechanism in CC ++

zhaozj2021-02-11  190

Function parameter passing mechanism of C / C are: Yang Ning Published: 2000/11/30

Thesis:

This article discusses the parameter transfer mechanism called in C and C ; there are some issues of the function return value. The examples of this article use VC 6.0.

text:

Function parameter transfer mechanism in C / C , the basic theoretical function parameter transfer mechanism of the function parameter delivery mechanism is inherently a method of communicating when the calling function (process) and the modified function (process) are invoked. Basic parameter transfer mechanisms have two types: value transfer and reference delivery. The following discussion claims that the function of the other functions is the main adjustment function, the function called is called function. During a passl-by-value process, the formal parameters of the modulated function as partial variable processing of the modulated function, that is, the memory space is opened in the stack to store the value of the arguments that are placed in the main adjustment, thus Become a copy of the argument. The value transfer is characterized by any operation of the modulated function to the formal parameters as a local variable, and does not affect the value of the real parameters variable. During the pass-by-reference process, the formal parameters of the modulated function also opens up memory space as a local variable in the stack, but the address of the real parameters that are placed in the main adjustment function is stored. Any operation of the modulus of the modulus is processed into indirect addressing, that is, the address of the horses in the horses in the stack. Because of this, any operation of the modulus of the modulus is used affects the real parameters in the main adjustment function. Second, the function parameter transfer mechanism in the C language is in the C language, and the value passed is the only parameter transfer mechanism available. However, according to the author, many friends also believe that this is the reference to pass due to the influence of the pointer variable as the function parameters. This is wrong. Please see the following code: int sw * x, int * y) {int TEMP; TEMP = * x; * x = * y; * y = Temp; return temp;} void main () {int a = 1 , b = 2; int * p1 = & a; int * p2 = & b; SWAP (P1, P2)} function SWAP is used as a parameter in two pointer variables, and when the main () calls SWAP, the pointer is transmitted. The value of the variables P1, P2 (that is, the address of variables A, B) is placed in the memory cell that SWAP is in the form of forms X, Y in the stack. This can be seen from the following assembly code (the comment is the author added): 22: void main () 23: {......................................................................................................... ], 10040108F MOV DWORD PTR [EBP-8], 214: INT * P1 = & A; 00401096 Lea Eax, [EBP-4] 00401099 MOV DWORD PTR [EBP-0CH], EAX15: INT * P2 = & B; 0040109C LEA ECX , [EBP-8] 0040109F MOV DWORD PTR [EBP-10H], ECX16: SWAP (P1, P2); 004010A2 MOV EDX, DWORD PTR [EBP-10H]; Parameter P2 Value Inn 004010A5 PUSH EDX004010A6 MOV Eax, DWORD PTR [EBP-0CH]; Parameter P1 Value Inn 004010A9 PUSH EAX004010AA Call @ ilt 15 (SWAP) (00401014); call SWAP function 004010af Add ESP, 8; Cleanup stacks 17:} Read the above code To pay attention The Intel80x86 series CPU's processing is generated downward, i.e., from the high address unit to the low address unit. From the above assembly code, it is known that main () before calling SWAP, press the value of the arguments from the right to the left, first P2, and then press the stack.

After the call is over, the main adjustment main () is responsible for cleaning the parameters in the stack. SWAP will use these to enter the stack variable. The following is a compilation code of the SWAP function: 14: Void swap (int * x, int * y) 15: {00401030 Push EBP00401031 MOV EBP, ESP; EBP points to the top ................................... X; 4: int Temp; 5: Temp = * x; 00401048 MOV EAX, DWORD PTR [EBP 8]; operation has been stored in P1 in the stack, set P1; into EAX0040104B MOV ECX, DWORD PTR [EAX]; P1 is placed in ECX0040104D MOV DWORD PTR [EBP-4], ECX; via ECX will be placed in the TEMP variable; save unit. The following is similar to 6: * x = * y; 00401050 MOV EDX, DWORD PTR [EBP 8] 00401053 MOV EAX, DWORD PTR [EBP 0CH] 00401056 MOV ECX, DWORD PTR [EAX] 00401058 MOV DWORD PTR [EDX], ECX7 : * Y = Temp; 0040105A MOV EDX, DWORD PTR [EBP 0CH] 0040105D MOV Eax, DWORD PTR [EBP-4] 00401060 MOV DWORD PTR [EDX], EAX8: Return Temp; 00401062 MOV Eax, DWORD PTR [EBP- 4] 9:} Basically, the above assembly code will basically explain the principle of the C language median transmission, but only the value of the pointer is the value of the pointer. This article also discusses the SWAP function transmitted by the reference. From these assembly code analysis, here we can get the following points: 1. The stack storage area of ​​the process is the main area of ​​the primary and the modulated function. 2. The parameter in the C language is from right to left. 3. The stack area structure used by the modulated function is: local variables (such as TEMP) return address function parameters lower address high address 4. The stack is cleaned by the main adjustment function after the call. 5. The return value of the function is generally placed in the register. Here is a need to add some points: First, the parameters are put in stack. For internal types, since the compiler knows the memory size used by the various variables, use the PUSH instruction directly; for custom types (such as Structure), the source address is used to perform byte delivery byte the address to the stack. . Second, the function return value is generally placed in the register, which is mainly to support interrupts; if it is placed in the stack, it is possible to overwrite because it is interrupted. Third, if the return value of the function is large, then from the stack to the address unit of the returned value (by the Motor) to the modulated function to the modulated function before calling) to reach the returned purpose. For the second and third, "Thinking In C " book has a better elaboration in Chapter 10. The fourth is an obvious conclusion, if the address returns a local variable in the modulo is meaningless; because the local variable is stored in the stack, the stack will be cleaned up after the call is completed, and these addresses become invalid. Third, the function parameter transfer mechanism in the C language C has both the value of C has a reference delivery. On the value transfer with C, it will focus on the reference delivery. As mentioned earlier herein, reference delivery is the address of the transfer variable to the stack used by the modulated function. Declaring reference delivery in C to use "&" symbols, but not when calling.

The following code is the SWAP2 function and main function that uses references: INT & SWAP2 (INT & X, INT & Y) {INT TEMP; TEMP = X; X = Y; Y = Temp; Return X;} void main () {Int a = 1, b = 2; SWAP2 (A, B);} At this time, the function SWAP2 will accept the address of the two integer variables while returning one of them. From the main function, call SWAP2 (A, B) for SWAP2, you can't see if the reference passes is used, whether to use reference delivery, is determined by the definition of the SWAP2 function. The following is a compilation code for the main function: 11: void main () 12: {................................................................................................................. EBP-8], 2; Variables B14: SWAP2 (A, B); 00401096 Lea Eax, [EBP-8]; send B offset address to EAX00401099 PUSH EAX; B offset address stack 0040109a Lea ECX, [EBP-4]; send A's offset address into ECX0040109D PUSH ECX; put A's offset address stack 0040109E Call @ ilt 20 (SWAP2) (00401019); call SWAP function 004010A3 Add ESP, 8; cleanup stack The parameter 15:} It can be seen that the main function is stack the offset address of B and A before calling SWAP2, which is the address of the transfer variable. The assembly code of the SWAP2 function is: 2: INT & SWAP2 (INT & X, INT & Y) 3: {00401030 Push EBP00401031 MOV EBP, ESP ............ 4: int Temp; 5: Temp = x; 00401048 MOV Eax, DWORD PTR [EBP 8] 0040104B MOV ECX, DWORD PTR [EAX] 0040104D MOV DWORD PTR [EBP-4], ECX6: X = Y; 00401050 MOV EDX, DWORD PTR [EBP 8] 00401053 MOV EAX, DWORD PTR [EBP 0CH] 00401056 MOV ECX, DWORD PTR [EAX] 00401058 MOV DWORD PTR [EDX], ECX7: Y = Temp; 0040105A MOV EDX, DWORD PTR [EBP 0CH] 0040105D MOV EAX, DWORD PTR [EBP-4] 00401060 MOV DWORD PTR [EDX], EAX8: RETURN X; 00401062 MOV Eax, DWORD PTR [EBP 8]; Returns X, due to X is offset by external variables;, the return is a legal 9:}, Swap2 is the same as the assembly code of the front SWAP function. This is because the front SWAP function accepts the pointer variable, and the value of the pointer variable is the address. Therefore, for the SWAP2 and the front SWAP here, the function parameters in the stack are stored all the address, which is consistent in the way in the function.

转载请注明原文地址:https://www.9cbs.com/read-4985.html

New Post(0)