Linux asm code



I have compiled a useless C module with Gcc under Linux just to see
what compiled C code looks like. The C code is this:

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

The output asm code is as follows (generated by objdump)

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 the "top" four
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

.



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: Task switch under from int08 for RTOS under PM with DOS4GW
    ... > I don't really want to use TSS. ... system handlers, won't work if you push that on local stack, because those handlers use ... DPL0,1,2 stack segments and NOT DPL3 stack like printf. ... writing system code that checks TS bit is mostly complicated and not the fastest way to get ...
    (comp.lang.asm.x86)