How to achieve an abnormality in C ++ compiler 2

zhaozj2021-02-16  63

See how the C compiler achieves exception handling 1 sdssly (translation)

http://www.9cbs.net/develop/Article/15\15051.SHTM

Without the following, then I went to the original text, I also translated part, but I still didn't go to key partial functions and stack.

The stack is a continuous memory that saves the partial object of the function. More specifically, each function has a stack frame (translation: STACK FRAME, when the function is called, the first sentence will be the PUSH EBP after entering the function, then the MOV EBP, ESP, so EBP generally points to the current function The top of the stack, and the content pointing is that the last layer call function enters the top, so out, finally 0 is the system's entry, such a function used to use a frame) to save all functions Temporary objects generated by local objects and function expressions (translation: 1.c objects are wide, not just Class, Structure, simple type is also object 2. Temporary object, you should be very clear, take an example For example, there is a function is int myfun (); you write int RET = myfun () in the function; then the result returned by myfun () is put into the RET, if you write befun (); and ignore its return Value, although you ignore it, but still have a place where it returns to the value, this is a temporary object, in the debug environment of the VC, you can see the temporary variable on the page of the AUTO variable). Note that the above description is just a typical situation. In fact, the compiler may store all or part of the variables to the register to achieve faster execution speed (translation: compiler optimization). The stack is a processor (CPU) level support concept (the reason why this is said, because there is a Push and POP in the assembly code). The processor provides internal registers and special instructions to implement stack processing.

Figure 2 shows a typical stack, which is when the function foo call function bar, then the Bar call the contents of the stack after the function widget. Please note that the stack is growing down (the translation: I usually have a low address on the paper, so the author's picture looks a bit blame, but it should be no problem), which means the next one will The address of the element in the stack will be smaller than the address of the previous element (low). The compiler uses the ESP register to identify the current stack frame. In the case shown above, the function being executed when the Widget is executing, the EBP register points to the Widget's stack frame (that is, when the function is entered, the PUSH is the top of the stack after EBP) . Function Access Local variables are offset with respect to the frame top with the location of the local variable. When compiling, the compiler will turn all local variables from the name into a fixed relative to the offset, for example, a partial variable of the Widget function accesses its location with EBP-24.

The above figure also shows the ESP register. In the case of the illustration, it points to the last item of the stack, that is, the end of the Widget frame, the next frame will start from this location.

The processor supports two stack operations: Push and POP:

POP EAX

It means that 4 bytes from ESP are read to EAX, and then the ESP increases 4 (32-bit processors). same,

Push EBP

Means ESP minus the ESP, then write the contents of the EBP to the ESP pointing.

When the compiler recompores a function, it adds some code for creation and initialization function stack frames to the function header. Similarly, add the code that pops up the stack frame from the stack at the end of the function.

Typically, the compiler is generated in the header of the function

Push EBP; Save Current Frame Pointer On Stack

MOV EBP, ESP; Activate The New Framesub ESP, 10; Subtract. Set ESP At the End of the Frame

The first sentence saves the current frame pointer EBP to the stack, the second sentence is activated by setting EBP to the current ESP to activate the current function frame, the third sentence sets the end of the ESP register to the current frame, that is, minus the ESP to subtract this function The total length of the local object. The compiler knows how many local objects and the length of each object, so it can clearly know the exact length of the frame of a function.

Popping the current frame from the stack at the end of the function

MOV ESP, EBP

POP EBP; ACTIVATE CALLER's FRAME

Ret; return to the caller

Restore ESP and EBP, then execute RET

When the processor performs the RET instruction, a POP EIP is actually similar, saved the EIP popped up, and then jumps to the EIP start execution. Instead, when the Call instruction is executed, put the current EIP into the stack, then JMP to the corresponding address,

Figure 3 shows more details of the runtime stack. As shown, the function parameters are also part of the function frame, and the calling function puts the parameters into the stack, then the function returns not to execute

Add ESP, Args_Size

Or, use another RET instruction, as follows

Ret 24

After returning, add ESP, 24

Note that every thread in the process has its own stack.

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

New Post(0)