c / asm / stack and pointers
When you declare a variable i.e.
how exactly does it create it in the stack...
or better yet, explain this...
my explanation (please correct me)
movl %esp, %ebp
subl $4, %esp
movl $5, -4(%ebp)
movl $0, %eax
correct me if I'm wrong but if i made another function called foo() could I use foo to get back at the -4(%ebp) and how? without parameters...
pushl %ebp - pushes the frame pointer onto the stack to be popped later
movl %esp, %ebp - moves the current stack pointer to become the new frame pointer
subl $4, %esp - subtract 4 bytes (one word) from the stack\
pointer, i.e. move the pointer up four to make room for the var.
movl $5, -4(%ebp) - assign the value 5 to the address of ebp offsetted -4 (4 up)
movl $0, %eax - the return value
leave - you know
ret - return %eax;
(btw: I think this is ILLEGAL C because it is compiler specific on how the stack is arranged but just for the point of learning, assume its on gcc and a linux system)
and another question
is there a var in C that stores the value of EBP and ESP etc. without using inline asm? To my knowledge there is not but if there is, i would like to learn of it...
Thanks for any input