Introduction to Computer Systems The Stack

The Stack

Purpose of the Stack

Return address example


long leaf(long y)
    return y + 2;

long top(long x)
    long t = leaf(x - x);

    return 2 * t;

int main()
    long val = top(100);
    printf("%l", val);

    return 0;

Stack (grows down):

main() called
address of line 10
top() called
address of line 6
leaf() called

Passing Parameters To Functions

In x86-64, the first six parameters are passed in registers:

After that, they are stored in the stack.

NOTE: x86-32 always puts paramters in the stack

Preserving Register State

By convention, some registers are saved on the stack by the funciton being called (callee-saved), and other registers are saved to the stack by the function doing the calling (caller-saved). Any register not used as a paramter can be used as a caller-saved register