Debugging C program (int declaration)

余生长醉 提交于 2019-12-11 19:29:16

问题


I'm still learning assembly and C, but now, I'm trying to understand how the compiler works. I have here a simple code:

int sub()
{
  return 0xBEEF;
}
main()
{
  int a=10;
  sub();
}

Now I know already how the CPU works, jumping into the frames and subroutines etc. What i don't understand is where the program "store" their local variables. In this case in the main's frame?

Here is the main frame on debugger:

   0x080483f6 <+0>:     push   %ebp
   0x080483f7 <+1>:     mov    %esp,%ebp
   0x080483f9 <+3>:     sub    $0x10,%esp
=> 0x080483fc <+6>:     movl   $0xa,-0x4(%ebp)
   0x08048403 <+13>:    call   0x80483ec <sub>
   0x08048408 <+18>:    leave  
   0x08048409 <+19>:    ret 

I have in "int a=10;" a break point that's why the the offset 6 have that arrow. So, the main's function starts like the others pushing the ebp bla bla bla, and then i don't understand this:

   0x080483f9 <+3>:     sub    $0x10,%esp
=> 0x080483fc <+6>:     movl   $0xa,-0x4(%ebp)

why is doing sub in esp? is the variable 'a' on the stack with the offset -0x4 of the stack pointer?

just to clear the ideas here :D

Thanks in advance!


回答1:


   0x080483f9 <+3>:     sub    $0x10,%esp

You will find such an instruction in every function. Its purpose is to create a stack frame of the appropriate size so that the function can store its locals (remember that the stack grows backward!).
The stack frame is a little too big in this case. This is because gcc (starting from 2.96) pads stack frames to 16 bytes boundaries by default to account for SSEx instructions which require packed 128-bit vectors to be aligned to 16 bytes. (reference here).

=> 0x080483fc <+6>:     movl   $0xa,-0x4(%ebp)

This line is initializing a to the correct value (0xa = 10d). Locals are always referred with an offset relative to ebp, which marks the beginning of the stack frame (which is therefore included between ebp and esp).



来源:https://stackoverflow.com/questions/19210032/debugging-c-program-int-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!