Thread: smashing the stack

  1. #1
    Registered User rohit's Avatar
    Join Date
    Feb 2002
    Posts
    69

    smashing the stack

    Well first of all this is my very first hands on exp. with Assembly...
    I was going through the smashing the stack for fun and profit article
    and stumblled across with this doubt.

    Using the very first example as

    test.c

    void print(int a,int b,int c)
    {
    char buffer1[5];
    }

    void main()
    {
    print(1,2,3);
    }

    the assembler output is
    print:
    pushl %ebp
    movl %esp, %ebp
    subl $24, %esp
    leave
    ret

    If I make the buffer1 to size 4 the subl is nothing but deducting 4 from
    sp

    subl
    $4, %esp

    now how is it possible that one byte extra could allocate so much
    space when the word size is of 8 bytes. What are these extra bytes for.

    cheers
    Rohit

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I guess its down to your compiler, if its in debug mode, or if its trying to optimise for something??

    VC++ in debug allocates a stack frame of 40h (64) bytes even when its an empty function and I dont pass anything........this goes up to 44h when char buffer1[4]; and 48h when char buffer1[5]; (4 byte multiples).....

    All compilers optimise differently and the code created wont always be the same as other compilers.....

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    On 32bit intel and amd is 4byte the magic word.

    nearly everything is aligned to 4byte (you get a significant speed plus!)

    so it is simple for optimizing.
    Hope you don't mind my bad english, I'm Austrian!

  4. #4
    Registered User rohit's Avatar
    Join Date
    Feb 2002
    Posts
    69
    sorry for cross posting well am not in the debugging mode

    i took the program file and did


    cc -S -o test.asc progname.c

    am not in the debug mode or running the program whatsoever just the assembly dump of the program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM