Compiling a simple C code into assembly using GCC will have the following output:
... 13 xorl %eax, %eax 14 movl $0, -4(%rbp) 15 movl $5, -8(%rbp) 16 movl $6, -12(%rbp) 17 movl -8(%rbp), %ecx 18 addl -12(%rbp), %ecx 19 movl %ecx, -16(%rbp) 20 popq %rbp 21 retq My question is, why an offset to the frame base pointer (rbp) is being used instead of manipulating the stack pointer (rsp). Isn't that the whole point of having a stack pointer?
What if the stack of this process gets overwritten by some other process (Garbage collection as an example) which doesn't even know the stack is being used, since rsp isn't decremented when writing values.
gcc -O2implies-fomit-frame-pointerwhich avoids wasting instructions making a stack frame. Usegcc -O3(with maybe-fno-tree-vectorize) if you want efficient code to read. Wondering why un-optimized / debug-mode code is inefficient doesn't make much sense.[callstack]. The[stack]tag usage guidelines say this; it's for generic stack data structures.