Odin
Odin

Reputation: 677

Useless allocated Stackspace?

Why does this function allocate more stack space than it needs to, before calling gets()?

echo:
  pushl  %ebp
  movl   %esp, %ebp
  pushl  %ebx
  leal   -8(%ebp), %ebx
  subl   $20,  %esp       <-- Why so much space?
  movl   %ebx, (%esp)
  call   gets
  ...

The corresponding C code:

void echo()
{
  char buf[4];
  gets(buf);
  puts(buf);
}

Why is there an additional extra space of three words between the buffer and the argument for gets?

stack

Upvotes: 13

Views: 218

Answers (1)

thetaprime
thetaprime

Reputation: 562

There are two sentences in the book Computer Systems. "gcc adhere to an x86 programming guideline that the total stack space used by the function should be multiple of 16 bytes." and "Including the 4 bytes for the saved %ebp and the 4 bytes for the return address,"

Upvotes: 10

Related Questions