Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my stack pointer only incrementing in multiples of 16?

Tags:

x86

assembly

Using the following C code

void func() {
  int a=1,b=2,c=3;
}

Compiling using gcc -S -O -o- myfile.c I get the output

    .file   "myfile.c"
    .intel_syntax noprefix
    .text
.globl func
    .type   func, @function
func:
    push    ebp
    mov ebp, esp
    sub esp, 16
    mov DWORD PTR [ebp-4], 1
    mov DWORD PTR [ebp-8], 2
    mov DWORD PTR [ebp-12], 3
    mov DWORD PTR [ebp-16], 4
    mov DWORD PTR [ebp-20], 5
    leave
    ret
    .size   func, .-func
    .ident  "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
    .section    .note.GNU-stack,"",@progbits

Here I would expect the third line after func: to be sub esp,12 instead of sub esp,16. I played with different numbers of automatic variables in the function and found that it grows in increments of 16 bytes. Why does this happen? Does this happen on all platforms, or is it platform specific?

I'm currently running an Intel Mac with OSX, compiling through an Ubuntu (32-bit) VirtualBox guest using GCC.

like image 208
Martin Doms Avatar asked Mar 31 '11 06:03

Martin Doms


People also ask

Why is the stack pointer decremented?

The addresses decrease as they move toward the top of the stack and increase as they move toward the bottom, so when a data element is pushed onto the stack, the stack pointer decrements to the next address below the current one, and when an element is removed, the pointer increments to the address of the next saved ...

Does stack pointer increment or decrement?

When you PUT something ONTO the stack (PUSH onto the stack), the SP is decremented before the item is placed on the stack. When you take something OFF of the stack (PULL from the stack), the SP is incremented after the item is pulled from the stack.

What is the value of stack pointer?

The stack pointer (SP) points to the top element of the stack. The current value of SP is (016E)hex. The CALL instruction is of two words, the first word is the op-code and second word is the starting address of the subroutine (one word = 2 Bytes).

How does the stack pointer move?

The stack pointer points to the last in-use byte of the stack. The standard convention is that when your function starts up, you can claim some of the stack by moving the stack pointer down--this indicates to any functions you might call that you're using those bytes of the stack.


2 Answers

From GCC man page, (bold emphasis mine):

-mpreferred-stack-boundary=num

Attempt to keep the stack boundary aligned to a 2 raised to num byte boundary. If -mpreferred-stack-boundary is not specified, the default is 4 (16 bytes or 128 bits).

like image 198
typo.pl Avatar answered Nov 15 '22 09:11

typo.pl


That is very strange output.

Are you sure the c file wasn't:

void func() {
  int a=1,b=2,c=3,d=4,e=5;
}

?

Otherwise why the lines

mov DWORD PTR [ebp-16], 4
mov DWORD PTR [ebp-20], 5
like image 24
jpowell Avatar answered Nov 15 '22 09:11

jpowell