Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 973
Odin Avatar asked Jan 11 '13 13:01

Odin


1 Answers

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,"

like image 139
thetaprime Avatar answered Sep 22 '22 17:09

thetaprime