Tuesday, 2 October 2018

Stack function argumen & local variable declaration order in stack

Program:


/*  Stack function argumen & local variable declaration order in stackprogram by Velraj.K
    Check : http://velrajcoding.blogspot.in
  */
#include <stdio.h>
#include <stdlib.h>

int global = 0;

int stack_no_argument()
{
        return 6;
}

void stack_no_arg_declar_call(int arg)
{
        int a;
        int b;

        printf("\n\t\t%s &argc = %p &a = %p &b = %p \n\t\t", __func__, &arg, &a, &b);
}

oid stack_no_arg_declar(int arg)
{
        int a[100];
        int b[200];
        int c[300];
        char chr[100];   /* Seems stack also it is taking extra bye, use only int is continue.
                          * but mixing char inbetween get extra bye on first a int and then this variable */
        int d[100];
        void *ptr = NULL;
        int *ptr_int;

        ptr = malloc(100);
        ptr_int = b;

        *(ptr_int + 200) = 1;
        printf("\n\t\t &a[0] = %p &a[99] = %p \n\t\t &b[0] = %p &b[199] = %p\n\t\t &c[0] = %p &c[299] = %p\n\t\t",
                        &a[0], &a[99], &b[0], &b[199], &c[0], &c[299]);

        printf("\n\t\t chr[0] = %p, &chr[99] = %p\n\t\t &d[0] = %p &d[99] = %p\n\t\t global = %p\n\t\t malloc= %p\n\t\t",
                        &chr[0], &chr[99], &d[0], &d[99],
                        &global, ptr);
        printf("\n\t\t &ptr = %p \n\t\t &ptr_int = %p \n\t\t",
                        &ptr, &ptr_int);
        printf("\n\t\t argc = %p \n\t\t a[0] = %d c[0] = %d \n\n",
                        &arg, a[0], c[0]);
       stack_no_arg_declar_call(100);
}

int stack_argument_seque(int a, int b, char ch, float *flo)
{
        b = a + ch;
        printf("Inside the function \n");
        return 10;
}



int main()
{
        float value = 0.0;
        int ret = 0;
/*
        ---- snip -----
        fstps   -20(%ebp)
        movl    $0, -16(%ebp)
        leal    -20(%ebp), %eax
        pushl   %eax
        pushl   $99   
        pushl   $2
        pushl   $1
        call    stack_argument_seque
        ---- snip -----
        Argument pushed into stack from right to left.
        Note: ASCII value of 'c' is 99.
*/

int stack_argument_seque(int a, int b, char ch, float *flo)
{
        b = a + ch;
        printf("Inside the function \n");
        return 10;
}



int main()
{
        float value = 0.0;
        int ret = 0;
/*
        ---- snip -----
        fstps   -20(%ebp)
        movl    $0, -16(%ebp)
        leal    -20(%ebp), %eax
        pushl   %eax
        pushl   $99   
        pushl   $2
        pushl   $1
        call    stack_argument_seque
        ---- snip -----
        Argument pushed into stack from right to left.
        Note: ASCII value of 'c' is 99.
*/

int stack_argument_seque(int a, int b, char ch, float *flo)
{
        b = a + ch;
        printf("Inside the function \n");
        return 10;
}



int main()
{
        float value = 0.0;
        int ret = 0;
/*
        ---- snip -----
        fstps   -20(%ebp)
        movl    $0, -16(%ebp)
        leal    -20(%ebp), %eax
        pushl   %eax
        pushl   $99   
        pushl   $2
        pushl   $1
        call    stack_argument_seque
        ---- snip -----
        Argument pushed into stack from right to left.
        Note: ASCII value of 'c' is 99.
*/


        ret = stack_argument_seque(1, 2, 'c', &value);
        stack_no_argument();
        stack_no_arg_declar(10);
}


Output:
   ------
   velraj@velraj-HEC41:~/CProgram$ ./a.out
Inside the function

                 &a[0] = 0xbfdd319c &a[99] = 0xbfdd3328
                 &b[0] = 0xbfdd332c &b[199] = 0xbfdd3648
                 &c[0] = 0xbfdd364c
                global = 0x804a02c
                  malloc= 0x942c410
                a[0] = 0 c[0] = 0


After add the char inbetween:
--------------------------
velraj@velraj-HEC41:~/CProgram$ ./a.out
Inside the function
Output:
   ------
   velraj@velraj-HEC41:~/CProgram$ ./a.out
Inside the function

                 &a[0] = 0xbfdd319c &a[99] = 0xbfdd3328
                 &b[0] = 0xbfdd332c &b[199] = 0xbfdd3648
                 &c[0] = 0xbfdd364c
                global = 0x804a02c
                  malloc= 0x942c410
                a[0] = 0 c[0] = 0


After add the char inbetween:
--------------------------
velraj@velraj-HEC41:~/CProgram$ ./a.out
Inside the function


                 &a[0] = 0xbfed0ac8 &a[99] = 0xbfed0c54
                 &b[0] = 0xbfed0de8 &b[199] = 0xbfed1104
                 &c[0] = 0xbfed1108 &c[299] = 0xbfed15b4
                 chr[0] = 0xbfed15b8, &chr[99] = 0xbfed161b
                 &d[0] = 0xbfed0c58 &d[99] = 0xbfed0de4
                 global = 0x804a02c
                 malloc= 0x855b410
                 a[0] = 0 c[0] = 1

        Here we are wring b+200 but it write on c[0], hece a first b second and 3rd is c, since stack botton start on top, hence address wise c a is long from stack.

        Output current:
        --------------
velraj@velraj-HEC41:~/CProgram$ ./a.out
Inside the function


                 &a[0] = 0xbf85fa58 &a[99] = 0xbf85fbe4
                 &b[0] = 0xbf85fd78 &b[199] = 0xbf860094
                 &c[0] = 0xbf860098 &c[299] = 0xbf860544
               
                 chr[0] = 0xbf860548, &chr[99] = 0xbf8605ab
                 &d[0] = 0xbf85fbe8 &d[99] = 0xbf85fd74
                 global = 0x804a02c
                 malloc= 0x80e9410
               
                 &ptr = 0xbf85fa50
                 &ptr_int = 0xbf85fa54
               
                 argc = 0xbf8605c0
                 a[0] = 0 c[0] = 1


                stack_no_arg_declar_call &argc = 0xbf85fa40 &a = 0xbf85fa24 &b = 0xbf85fa28


Diagram to understand:



No comments:

Post a Comment