Debugging C program (int declaration) -


i'm still learning assembly , c, now, i'm trying understand how compiler works. have here simple code:

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

now know how cpu works, jumping frames , subroutines etc. don't understand program "store" local variables. in case in main's frame?

here 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;" break point that's why the offset 6 have arrow. so, main's function starts others pushing ebp bla bla bla, , don't understand this:

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

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

just clear ideas here :d

thanks in advance!

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

you find such instruction in every function. purpose create stack frame of appropriate size function can store locals (remember stack grows backward!).
stack frame little big in case. because gcc (starting 2.96) pads stack frames 16 bytes boundaries default account ssex instructions require packed 128-bit vectors aligned 16 bytes. (reference here).

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

this line initializing correct value (0xa = 10d). locals referred offset relative ebp, marks beginning of stack frame (which therefore included between ebp , esp).


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -