When you declare a variable i.e.
how exactly does it create it in the stack...Code:int k;
or better yet, explain this...
becomes:Code:int main() { int k=5; return 0; }
my explanation (please correct me)Code:.text .align 4 .globl main .type main,@function main: pushl %ebp movl %esp, %ebp subl $4, %esp movl $5, -4(%ebp) movl $0, %eax leave ret
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...Code: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)
using pointers...
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
-LC



LinkBack URL
About LinkBacks




