CS 208 s21 — Learning Block #15

Table of Contents

1 Warmup

        call next
next:
        popq %rax
  1. What gets stored in %rax by these instructions?1
  2. Write down the different uses for the stack (i.e., what kinds of data gets stored there?).2

2 Stack Frame Exercise

call-graph.png

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:

1

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.

2
  1. Arguments for a function call (beyond the first six)
  2. Saved registers (callee-saved)
  3. Local variables
  4. Return address
3

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.