I’m working on practice problem 3.34 in Computer Systems a Programmers Perspective and trying to understand exactly what is happening. The question states "Consider a function P
, which generates local values, named a0-a7
. It then calls function Q
using these generated values as arguments. GCC produces the following code for the first part of P
". We are given the following assembly code:
/* long P(long x)
* x in %rdi */
P:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbp
pushq %rbx
subq $24, %rsp
leaq 1(%rdi), %r15
leaq 2(%rdi), %r14
leaq 3(%rdi), %r13
leaq 4(%rdi), %r12
leaq 5(%rdi), %rbp
leaq 6(%rdi), %rax
movq %rax, (%rsp)
leaq 7(%rdi), %rdx
movq %rdx, 8(%rsp)
movl $0, %eax
call Q
So far, this is what I understand:
The instructions pushq %r15
through pushq %rbx
Are being pushed to the stack so as to preserve those values, and eventually replace them in their respective registers when procedure P
returns (Since they are callee saved registers).
I see that the instruction subq $24, %rsp
allocates 24 bytes of space on the stack.
I have two questions though:
- What are the load effective address lines doing?
It seems to me that it is taking the memory location that is is addressed by
long x
and storing that new memory address (after adding 1 or 2 or ... 7) in the various callee saved registers. Is this correct? I'm a bit confused as to the value that they store? Is there any significance to it? Also, what will functionQ
do with these registers? How does it know to use them as arguments, since they don't seem to be the argument registers? Onlylong x
is passed on as an argument (as it is in register%rdi
. - What is the contents of the Stack? I see that 24 bytes were allocated, but I can't seem to account for all of that space :( I understand the stack to look like this:
???????????????????????????????????:16
The result of 7(%rdi) (argument a7):8
The result of 6(%rdi) (argument a6):0 <--- %rsp
I cant seem to account for what is contained in bytes 16-23 :(
Thank you soo much in advance, I'm really struggling with this one.