CS 208 s21 — Learning Block #15
Table of Contents
1 Warmup
2 Stack Frame Exercise
Each node in the above tree represents a function call. How many stack frames will be created? How many will be present on the stack at one time?3
3 Practice
CSPP practice problems 3.34 (p. 252) and 3.35 (p. 255)
rfun: testq %rdi, %rdi jne .L8 movl $0, %eax ret .L8: pushq %rbx movq %rdi, %rbx shrq $2, %rdi call rfun addq %rbx, %rax popq %rbx ret
long rfun(unsigned long x) { if (_______) return _______; unsigned long nx = ______; long rv = rfun(nx); return ______; }
Footnotes:
Those instructions store the address of the popq %rax
instruction in %rax
.
call
pushes the address of the next instruction (the return address) onto the stack and then jumps to the specified target.
popq %rax
is the next instruction, so call next
pushes its address onto the stack, and then jumps to the next
label, which happens to be at popq %rax
.
popq %rax
then pops the value from the top of the stack into %rax
.
- Arguments for a function call (beyond the first six)
- Saved registers (callee-saved)
- Local variables
- Return address
Six stack frames will be created, one for each call. Four will be on the stack at one time, (e.g., slorp, squish, splat, printf), since once squish calls slop, splat and the first printf will have returned and been popped off the stack.