简体   繁体   中英

Decreasing of the stack pointer by creating local variables

To get a better understanding of binary files I've prepared a small c++ example and used gdb to disassemble and look for the machine code.

The main() function calls the function func() :

int func(void)
{
    int a;
    int b;
    int c;
    int d;
    d = 4;
    c = 3;
    b = 2;
    a = 1;
    return 0;
}

The project is compiled with g++ keeping the debugging information. Next gdb is used to disassemble the source code. What I got for func() looks like:

0x00000000004004cc <+0>:    push   %rbp
0x00000000004004cd <+1>:    mov    %rsp,%rbp
0x00000000004004d0 <+4>:    movl   $0x4,-0x10(%rbp)
0x00000000004004d7 <+11>:   movl   $0x3,-0xc(%rbp)
0x00000000004004de <+18>:   movl   $0x2,-0x8(%rbp)
0x00000000004004e5 <+25>:   movl   $0x1,-0x4(%rbp)
0x00000000004004ec <+32>:   mov    $0x0,%eax
0x00000000004004f1 <+37>:   pop    %rbp
0x00000000004004f2 <+38>:   retq

Now my problem is that I expect that the stack pointer should be moved by 16 bytes to lower addresses relative to the base pointer, since each integer value needs 4 bytes. But it looks like that the values are putted on the stack without moving the stack pointer.

What did I not understand correctly? Is this a problem with the compiler or did the assembler omit some lines?

Best regards,
NouGHt

There's absolutely no problem with your compiler. The compiler is free to choose how to compile your code, and it chose not to modify the stack pointer. There's no need for it to do so since your function doesn't call any other functions. If it did call another function then it would need to create another stack frame so that the callee did not stomp on the caller's stack frame.

As a general rule, you should avoid trying to make any assumptions on how the compiler will compile your code. For example, your compiler would be perfectly at liberty to opimize away the body of your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM