In-depth understanding of C language (definition of local variable)

zhaozj2021-02-08  213

In-depth understanding of C language

This section we primarily study how the C language uses local variables in the function. The space address allocated for global variables and local variables is different. The global variable is placed in the _data section, which is removed. The other block of the _text code segment. The local variable is mainly used to use the stack of memory space. Ok, let's take a look at the case study below.

Research case three

Tool: TurboC C V2.0, DEBUG, MASM V5.0, NASM Instance C Program: / * EXAMPLE3.C * / CHAR CH; INT E_MAIN () {INT I1; INT I2; Int I3; I1 = 1; I2 = 2; i3 = 3;

}

; Entourt Start.asm [BITS 16] [Global Start] [EXTERN _E_MAIN] Start: Call_e_main

Target content: method of using local variables in C language

Similarly, here I need Start.asm as the entrance to our C language. We use E_MAIN to avoid regular main function portals so we can understand the code instructions generated inside the function.

Like the previous section, we compile the C program and entrance assembler Start.ASMNASMW -F Obj -o Start.obj Start.ASMTCC -MT -OEXAMPLE3.OBJ-C Example3.Clink Start.obj Example3.obj, Example3.exe, , exe2bin example3.exe

Similarly, we use old DOS's Debug tool to disassemble the code generated by EXAMPLE3.BIN.

Debug-n example3.bin-l 0-u 0xxxx: 0000 call 0003xxxx: 0003 push bpxxxx: 0004 MOV BP, SPXXX: 0006 SUB SP, 06XXXX: 0009 MOV WORD PTR [BP-06], 0001XXXX: 000E MOV WORD PTR [BP-04], 0002XXXX: 0014 MOV Word PTR [BP-02], 0003XXXX: 0019 MOV SP, BPXXXX: 001B POP BPXXXX: 001C RET

Ok, here is the code generated by C has been revealed. Except for the first sentence, the call 0003 is our code in Start.asm, the other is the code generated by my C program. First enter the E_MAIN function. Execution

PUSH BPMOV BP, SP This is the same as the code in the previous second case. Save the BP first, then pass the stack pointer to the BP so that you will be accessed by BP.

Sub SP, 06 continues to move the stack pointer to move 6 bytes. Because we define three integer variables I1, I2, I3, a total of 6 bytes of space in E_MAIN. Here is localized by moving stack pointers Memory space allocation of variables.

MOV WORD PTR [BP-06], 0001MOV WORD PTR [BP-04], 0002MOV WORD PTR [BP-02], 0003 respectively correspond to three assignment statements I1 = 1 = 1 in E_MAIN; I2 = 2; I3 = 3; Here we can see that the address of I1 is actually BP-06. I2 is BP-04. I3 is BP-02. The front SUB SP, 06 is for these three variables to leave 6 bytes of space (BP-08 to BP-02) At the same time, we also see that 16-bit assignment statements in the C language are completed by simple MOV instructions.

MOV SP, BP When the E_MAIN function ends, the stack pointer is reduced to BP (BP value has never been changed). In this way, the space of our local variables I1, I2, I3 disappeared. So when the function in the C language ends The local variable in the function will automatically disappear.

POP BP restores the value of the BP. This is called with the previous PUSH BP

Ok, this case study is completed. The following is a summary time. The space in the local variable in the C language function is generally in the stack. Before entering the function, through "SUB SP, XX" to these local variables Allocate the stack space. Then use the BP to access these local variables. At the end, the "MOV SP, BP" restores the stack pointer, and the local variable disappears. Finally, the BP is restored with "POP BP", and the function is ended. It is worth noting that the C language will automatically use the int type variable to be RESIGTER INT. This part variable is not used by the stack space, but is directly using the Si register. For example, a typical example void loop {INT i; while (I <10000) {i ;}} For such functions, the C language usually optimizes the RESIGTER INT I. This i does not use any memory space to save the value, its value is directly saved Si register. So, it is natural to get a general variable for its access speed.

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

New Post(0)