How to create local variables inside the main function?

I know how to pass parameters to a user-defined function and how to create local variables inside such a function. But I want to create local variables for the main function.

So, the main function is the first thing that is executed when the program starts, but what is the initial value of esp when the main launch starts? i) what is on top of the stack when starting the main launch, are they command line arguments?

If I want to create local variables inside main, should I save the esp value in ebp and then increase esp by how much data I need, how do I do it inside a user-defined function?

0
source share
1 answer

So, the main function is the first thing that is executed when the program starts, but what is the initial value of esp when the main launch starts? i) what is on top of the stack when starting the main launch, are they command line arguments?

mainIt is called as a normal function, therefore (with a cdeclconvention call) the top elements are an up-down (optional) environment pointer, then a pointer to a pointer to a string argument argument is an array, then argc, then a return address main.

If I want to create local variables inside main, should I save the esp value in ebp and then increase esp by how much data I need, how do I do it inside a user-defined function?

main . crt0.o ( ) :

void
_start(void)
{
    /* initialisation skipped */
    int rv = main(newargc, newargv, environ);
    do_global_dtors();
    exit(rv);
    /* NOTREACHED */
}

, tl; dr: .

( , _start , , syscall exit.)

+2

Source: https://habr.com/ru/post/1570825/


All Articles