Unused stack space

michael_quinlivan_at_hotmail.com
Date: 09/30/05

  • Next message: QNils_O=2E_Sel=E5sdal=22?=: "Re: Unused stack space"
    Date: 29 Sep 2005 22:45:53 -0700
    
    

    In a C module that I have compiled, it seems to generate object files
    which waste stack space. The C file is as follows

    print1(char * str)
    {
       printf(str);
    }

    yes I know it is useless, it was simply created so that I could examine
    how GCC generates asm code, which is below

    >objdump -d print1.o
    00000013 <print1>:
      13: 55 push %ebp
      14: 89 e5 mov %esp,%ebp
      16: 83 ec 08 sub $0x8,%esp
      19: 8b 45 08 mov 0x8(%ebp),%eax
      1c: 89 04 24 mov %eax,(%esp)
      1f: e8 fc ff ff ff call 20 <print1+0xd>
      24: c9 leave
      25: c3 ret

    The code is relatively simple, but I don't understand the third
    instruction (sub 0x8, esp). obviously it allocates 8 bytes to the
    stack, but why does it allocate this much? It only uses 4 bytes of it
    (to pass the str parameter to printf), and leaves four bytes unused.
    Why is this? Or am I confusing something??

    My mental image of the stack just before the call to printf is as
    follows. Is this accurate?

    |Param to print1 |
    |--------------------|
    |Addr of caller |
    |--------------------|
    |Caller EBP |
    |--------------------|
    |UNUSED DWORD |
    |--------------------|
    |Param to printf |
    |--------------------|

    cheers
    MQ


  • Next message: QNils_O=2E_Sel=E5sdal=22?=: "Re: Unused stack space"

    Relevant Pages

    • Re: User defined stack for threads in Linux 2.6.11 + glibc 2.3.5
      ... If it crashes, what does its stack ... Again, in particular, neither printf, sleep, nor pthread_exit are on the list. ... The redundancy, like your multiple consecutive sleeps, doesn't hurt anything, but the redundancy does redundantly make reading the code more redundant than it really redundantly needs to be. ...
      (comp.programming.threads)
    • Re: calling convention stdcalll and cdecl call
      ... Nope, behavior is both defined and expected (prints 1, excess ... calling conventions for variadic functions in general. ... the behavior of printf has to do with calling conventions for variadic ... the stack after itself based on what the format string says. ...
      (microsoft.public.vc.language)
    • Re: evaluation of arguments to a function
      ... The printf format has a space between the two %d's, ... and implimentations that push each parameter value onto a stack ... allocate an amount of storage large enough to hold all the parameters ... parameter values into increasing storage with incrementing stack pointer.) ...
      (comp.lang.c)
    • Re: calling printf from gas for floating point display
      ... but I'm not able to figure out how to get printf to printout floats or doubles. ... the stack pointer, and fstpfrom the FPU stack. ... subl $8, %esp fstpq 0pushl $format_string call printf addl $12, %esp ...
      (alt.lang.asm)
    • Re: puzzle in C
      ... printf() the format string "%d, a+b" but you didn't give it a value to ... Since your format string includes %d the printffunction ... Arguments to functions are conventionally passed on the stack. ... differently depending on your compiler, ...
      (comp.lang.c)