2

If I insert a new functions and instructions that within the stack frame to assembly x86 code, is it necessary to increase the stack size? if yes by how much?

sub 0x4, %esp
push %eax                 ;;new instruction
...
call  fun                 ;; new inserted function
pop  %eax                 ;;new instruction 
.....
add 0x4, %esp
hamb
  • 159
  • 1
  • 2
  • 11

1 Answers1

4

Calling Conventions are the key to managing how functions are called, values are passed and returned, and how stack space and available registers are managed. There's lots of conventions. Know which one your function is using, and which one the function you are calling uses; they might be different!

A very common convention is "manage your own stack". In this case, each function allocates the amount of stack space it needs on the stack. It does not need to worry about stack space demands of functions it calls; by definition, they will allocate the (additional) amount of stack space they need.

A "leaf" function is one that calls no other functions; each leaf function requires some amount of stack space. Some calling conventions require that the caller of a leaf function, allocate not only space for its own needs, but that of all the leaf functions its calls. This keeps the overhead of calling leaf functions (which are called the most!) to a minimum. Usually this kind of arrangement is only done by a compiler, because it is difficult for people to keep track of all the functions they call and the stack demand of those functions, reliably. Occasionally you may encounter a hand-written assembly function that states as part of its API that the caller must allocate space for it.

The System V ABI offers a "red zone" of some 128 bytes of stack below the SP that are always available for a leaf function to use. This means the caller need not allocate that space, and leaf functions with less than the red zone size demand simply have it available without allocating it. This is a very nice convention because no function has to worry about whether it is calling a leaf function or not, and this enables linking arbitrary sets of routines together.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • How I can Know which convention my function is using? moreover, I use Linux fedora 9. Actually, I'm working on assembly code of one of SPEC benchmark program, and I need to modify that code by inserting new functions and new instructions. – hamb May 28 '12 at 23:34
  • 1
    You get to read your compiler documentation carefully. I think the Wikipedia link is likely to to tell you, since you are running Linux. – Ira Baxter May 28 '12 at 23:56
  • @hamb, the [manual by Agner Fog](http://www.agner.org/optimize/calling_conventions.pdf) could also be helpful. Various calling conventions as well as other register- and stack-related conventions are described there in detail. – Eugene May 29 '12 at 05:37