What changes have occurred to the register when calling the function?

Source: Internet
Author: User

There has always been a vague concept, so we use an example to strengthen memory.

Linux x86 gcc3.2.3 at&t format Compilation

The Code is as follows:

Void

Fun ()

{

Int A = 'a ';

}

Void

Main ()

{

Int B;

Fun ();

Return;

}

Start debugging

[Sanool @ sanool ex2] $ gdb a. Out

Gnu gdb Red Hat Linux (6.0post-0.20031117.6rh)

Copyright 2003 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

Welcome to change it and/or distribute copies of it under certain conditions.

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB. Type "show warranty" for details.

This GDB was configured as "i386-redhat-linux-gnu"... (no debugging symbols found)... using host libthread_db library "/lib/tls/libthread_db.so.1 ".

(GDB) disas main

Dump of worker er code for function main:

0x08048323: Push % EBP

0x08048324: mov % ESP, % EBP

0x08048326: Sub $0x8, % ESP

0x08048329: and $0xfffffff0, % ESP

0x0804832c: mov $0x0, % eax

0x08048331: Sub % eax, % ESP

0x08048333: Call 0x8048314

Zero X 08048338: Leave

0x08048339: Ret

0x0804833a: NOP

0x0804833b: NOP

End of worker er dump.

(GDB) disas fun

Dump of javaser code for function fun:

0x08048314: Push % EBP

0x08048315: mov % ESP, % EBP

0x08048317: Sub $0x4, % ESP

0x0804831a: movl $0x41, 0 xfffffffc (% EBP)

Zero X 08048321: Leave

0x08048322: Ret

End of worker er dump.

Explanation:

** When the program executes 0x08048333: Call 0x8048314 next

ESP = 0xbfffe660 (runtime)

EBP = 0xbfffe668 (runtime)

EIP = 0x08048333

** Then execute call 0x8048314, that is

Push % EIP (equivalent to sub $ 4% ESP and mov % EIP % ESP)

Movl $0x8048314, % EIP

The value of 0xbfffe65c is EIP = 0x08048338.

And ESP = 0xbfffe65c

EIP = 0x8048314

EBP = 0xbfffe668

** Execute 0x08048314: Push % EBP

ESP = 0xbfffe658

EBP = 0xbfffe668

The value at 0xbfffe658 is EBP = 0xbfffe668.

** Continue with 0x08048315: mov % ESP, % EBP

Assign the value of ESP to EBP.

That is, EBP = ESP = 0xbfffe658

** Start to execute 0x08048321: Before leave

EIP = 0x08048321

EBP = 0xbfffe658

ESP = 0xbfffe654

** When 0x08048321: leave is started

That is

Movl % EBP, % ESP (I .e. ESP = EBP = 0xbfffe658)

Pop % EBP (that is, mov % ESP, % EBP and add $4, % ESP)

At this time, EBP = 0xbfffe668 returns to the EBP value of the original function,

** Execute 0x08048322: Ret again.

Pop % EIP

(That is, mov % ESP, % eip and then add $4, % ESP)

In this case, EIP = 0x08048338

Program continues to execute leave in main

Call the fun function.

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.