I'm trying to improve my understanding of assembly, just so I can at least have a rough idea of what a given compiled code is doing. And for fun.
I've choosen an example which is relatively simple, even if not the simplest:
int boo(int);
int bar(int, int, int);
int foo(int i) {
int a = bar(boo(i), boo(i), boo(i));
int b = bar(boo(i), boo(i), boo(i));
return a + b;
}
The assembly corresponding to the C++ translation unit, is the following (complete example here):
foo(int):
pushq %r13
pushq %r12
pushq %rbp
pushq %rbx
movl %edi, %ebx
subq $8, %rsp
call boo(int)
movl %ebx, %edi
movl %eax, %r12d
call boo(int)
movl %ebx, %edi
movl %eax, %ebp
call boo(int)
movl %r12d, %edx
movl %ebp, %esi
movl %eax, %edi
call bar(int, int, int)
movl %ebx, %edi
movl %eax, %ebp
call boo(int)
movl %ebx, %edi
movl %eax, %r13d
call boo(int)
movl %ebx, %edi
movl %eax, %r12d
call boo(int)
movl %r13d, %edx
movl %r12d, %esi
movl %eax, %edi
call bar(int, int, int)
addq $8, %rsp
popq %rbx
addl %ebp, %eax
popq %rbp
popq %r12
popq %r13
ret
Why is the stack pointer, %rsp
altered at all?
It looks to me that all computation is accomplished by using registers, and no local variable, so why there's any need to make space in the stack via subq $8, %rsp
?
This question was originally less focused (see the edit history), but I've entirely reworded it as I got a better understanding of the matter thanks to the links that were provided in the comments: