This next problem will test your understanding of stack frames. It is based on the following recursive C
function:
long silly(long n, long *p)
{
long val, val2;
if (n > 0)
val2 = silly(n - 1, &val);
else
val = val2 = 0;
*p = val + val2 * n;
return val - val2;
}
•
This yields the following machine code:
silly:
pushq %rbp
pushq %rbx
xorl %eax, %eax
xorl %ecx, %ecx
movq %rsi, %rbp
subq $24, %rsp
testq %rdi, %rdi
jg .L7
movq %rcx, 0(%rbp)
addq $24, %rsp
popq %rbx
popq %rbp
ret
.L7:
movq %rdi, %rbx
leaq 8(%rsp), %rsi
leaq -1(%rdi), %rdi
call silly
imulq %rax, %rbx
movq 8(%rsp), %rdx
leaq (%rbx,%rdx), %rcx
subq %rax, %rdx
movq %rdx, %rax
movq %rcx, 0(%rbp)
addq $24, %rsp
popq %rbx
popq %rbp
ret
A. i Is the variable val stored on the stack?
ii If so, at what byte offset (relative to %rsp) is it stored?
iii If so, why is it necessary to store it on the stack?
B. i Is the variable val2 stored on the stack?
ii If so, at what byte offset (relative to %rsp) is it stored?
iii If so, why is it necessary to store it on the stack?
C. What (if anything) is stored at 32(%rsp) at the point of the recursive call (i.e., the program has run to the point of the call)?
D. What (if anything) is stored at 24(%rsp)?