Accounting for recursive call frames

An x86-64 Linux assembly implementation uses the System V AMD64 convention to reproduce this C function for nonnegative inputs:


C
long total(long n) {
    if (n == 0)
        return 0;
    return n + total(n - 1);
}


Every invocation, including the base case, pushes one 8-byte copy of its incoming RBX, then stores its own n in RBX. A non-base invocation calls total(n - 1), adds its saved n to the returned RAX, restores RBX, and returns. The base case returns zero after restoring RBX.


There are no other pushes, stack allocations, or calls. Every call adds an 8-byte return address; each frame's single register push provides the required alignment for its recursive call. The outer call is total(6), and the implementation performs all recursive calls without optimization.


Find the peak additional stack use measured from immediately before the outer call, including that call's return address, and the final returned value. Give stack use in bytes and both answers as integers, to zero decimal places.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Assembler with C Questions: