Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 40 ยท The Stack and Functions

CALL and RET

See how call pushes a return address and ret pops it, how nested calls and recursion use the stack, and how a stack overflow or bad ret happens, with a toy CPU.

A function is reusable code: you write it once and jump to it from many places. The difficulty is coming back. When helper finishes, the CPU must resume exactly after the instruction that called it, and since helper may be called from a hundred different places, that address cannot be fixed in advance. The solution is two small instructions, call and ret, and the stack you met in The Stack: push, pop and rsp.

In this lesson you learn what these two instructions do step by step, how nested calls and recursion keep their return addresses straight, and what goes wrong when a function leaves the stack unbalanced. Every program runs on the toy CPU, with the interpreter at the bottom of each TryIt. In the toy, the "address" of an instruction is simply its number in the program (labels excluded), so a return address looks like a small integer.

What call and ret do

call target performs two actions as one instruction:

  1. Push the return address: the address of the instruction that follows the call.
  2. Jump to target.

ret is the reverse: it pops the top of the stack into the instruction pointer (rip), so execution continues at that saved address. Written out in x86-64:

NASM
        call  helper        ; push address of the next instruction, then jump to helper
        mov   [result], eax ; <- the return address points here
        ; ...
helper:
        mov   eax, 42
        ret                 ; pop the saved address into rip

That is all. There is no hidden "call stack" structure inside the CPU: it is the ordinary stack in memory, with return addresses sitting between your data. The real encoding of call helper is the byte E8 followed by a 32-bit relative offset (5 bytes in all), so the pushed address is "the address of the call plus 5". ret (byte C3) has no operands; it trusts that rsp points at a return address.

On ARM64 the idea is the same but the return address does not go onto the stack automatically. bl label ("branch with link") stores the address of the next instruction in the link register x30 (also called lr) and jumps; ret jumps to the address in x30. A function that calls others has to save x30 on the stack itself first, otherwise its own bl instructions would overwrite it. You will meet this in ARM Load/Store, Conditions and Branches.

sequenceDiagram
  participant M as main
  participant O as outer
  participant I as inner
  M->>O: call outer - pushes return address A
  O->>I: call inner - pushes return address B
  I-->>O: ret - pops B
  O-->>M: ret - pops A

Return addresses are pushed in call order and popped in the opposite order, so every function returns to its own caller.

Nested calls, step by step

The next program calls outer, which calls inner. Each function adds a number to rax, so the final value tells you which parts ran. In the trace, look at the rsp column: it drops by 8 at every call and rises by 8 at every ret. The instruction numbers show the jumps: after ret in inner (line 9) the next line executed is 6, right after the call inner in outer.

JS main calls outer, outer calls inner

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The output ends with 1111: 1 from main, plus 10, 1000 and 100 added in the order outer-start, inner, outer-end. The stack reached 16 bytes: one return address for outer and one for inner. When inner returned, its address was popped and outer's was the top again, exactly the last-in, first-out behavior that nested calls need.

Passing values: a tiny convention

In the toy programs below, the first argument goes in rdi and the result comes back in rax. That mirrors the real System V x86-64 convention for the first argument and the return value, which is treated fully in Calling Conventions. For now, the rule is just an agreement between caller and callee.

Recursion at the instruction level

Recursion needs no new instruction. A function that calls itself pushes one more return address per level, so each active invocation has its own place on the stack. The only extra care is that anything the function still needs after the recursive call must be saved on the stack first, because the deeper calls will change registers.

Here is the factorial: fact(n) = n * fact(n - 1), with fact(1) = 1. Before the recursive call the code does push rdi to remember n, because the callee decrements rdi. After the call it does pop rdi, so rdi is n again, and multiplies the returned value in rax.

Fibonacci, fib(n) = fib(n-1) + fib(n-2), calls itself twice, so it must also save the first result (push rax) while the second call runs. The program prints the answer, the number of instructions executed and the deepest stack use in 8-byte slots.

JS Recursive factorial and Fibonacci

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The factorial results are 1, 120 and 3,628,800 with steps growing roughly in line with n (8, 40, 80) and a stack that grows with n (1, 9 and 19 slots). Fibonacci tells a different story: fib(10) is 55 and takes 1,416 steps, fib(15) is 610 and takes 15,784 steps. The depth only grows linearly (19 and 29 slots), but the number of calls roughly multiplies by 1.6 for every step of n, because each call spawns two more. The stack remembers where you are, not how much work you have done.

The pattern "push before the call, pop after" is the heart of every recursive function. Ask yourself, for each register: "do I still need this value after the call returns?" If so, it must be on the stack.

Two classic stack bugs

Because ret blindly trusts the top of the stack, two mistakes are famous. If a function pushes something and forgets to pop it, ret will treat that value as the return address and jump somewhere unintended. And if a function calls itself forever, every level adds a return address until the stack runs out of memory.

JS An unbalanced push and runaway recursion

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first run prints 222, not 111: ret popped the pushed address and landed on mystery. That is the whole idea behind many real attacks, where an attacker overwrites a saved return address so that ret goes where they want (see Security: Buffer Overflows and Defenses). The second run ends with a "stack overflow" message after the stack floor is crossed. On a real system the operating system maps a limited stack region (commonly a few megabytes for the main thread) and stepping beyond it ends the process with a segmentation fault.

Why recursion is not free

Each call costs a push, a jump, a pop and a return, plus whatever the function saves. Compilers can remove the cost in some cases: when the recursive call is the very last thing a function does (a tail call), an optimising compiler may turn it into a plain jmp so the stack does not grow. Not every language or compiler guarantees this. When depth matters, an explicit loop is the safer choice, as you saw in Writing Loops.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What are the two things a call instruction does?

    call = push the return address (the instruction after the call) + jump. ret later pops that address into the instruction pointer.
  2. 2. In the nested call example (main calls outer, outer calls inner), in which order are the return addresses popped?

    The stack is last-in, first-out, so the most recent return address (to outer) is popped by inner's ret, and the earlier one (to main) by outer's ret.
  3. 3. Why does the recursive factorial do push rdi before call fact?

    The recursive call changes rdi. The function still needs its original n to multiply with the result, so it saves n on the stack and restores it with pop.
  4. 4. A function pushes a value and then executes ret without popping it. What happens?

    ret always takes whatever is on top of the stack as the return address. A leftover pushed value sends execution to the wrong place, often crashing the program.
  5. 5. Where does ARM64 keep the return address when bl is executed?

    bl writes the return address into x30 and jumps. Functions that call others must save x30 themselves, usually on the stack.

Summary

  • call pushes the address of the next instruction and jumps; ret pops that address into rip. The return addresses live on the ordinary stack.
  • Nested calls work because the stack is last-in, first-out: the most recent call returns first.
  • On ARM64, bl puts the return address in x30 (lr) and ret jumps to it; non-leaf functions save x30 on the stack.
  • Recursion uses one stack frame per level; values needed after a recursive call must be pushed before it and popped after it.
  • Stack depth grows with recursion depth (linear in n for these examples); the number of calls can still grow much faster, as with Fibonacci.
  • An unbalanced push before ret or infinite recursion are the classic stack bugs; the first can redirect execution, the second ends in a stack overflow.

Next: learn the rules that let separately compiled functions cooperate in Calling Conventions: System V, Windows x64 and AAPCS64.