Detailed analysis of the meaning of retn 4 in the compilation

Source: Internet
Author: User

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

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.