Retn 4
Is a function return instruction.
Previously, I had been entangled in the retn register operation sequence, and I was just debugging it. I followed it in detail and suddenly went on to share it.
Assume an environment:
When retn 4 is not executed, esp = 0013feb8; EIP = 5d1d8b97; and [0013feb8] = 7c974a19
After retn 4 is executed:
First
EIP = [0013feb8]
: At this time, the CPU first directs the EIP to get the value 7c974a19, which is 4 bytes from 0013feb8 in the stack, as the retn jump target, and then the CPU jumps over and waits to execute the command under 7c974a19.
In this case, EIP = 7c974a19
Then
Because there is a 4 in retn 4, the CPU only allows the ESP stack + 4 + 4, making the ESP = 0013fec0.
Why are there two + 4? Another reason for + 4 is that if the command is retn instead of retn 4, ESP will automatically generate a + 4 after the current value (0013feb8) is given to the EIP, that is, ESP is 0013 febc.
Summary:
Retn operation: First EIP = ESP, then ESP = ESP + 4
Retn n operation: First EIP = ESP, then ESP = ESP + 4 + n
Detailed analysis of the meaning of retn 4 in the compilation