ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

ASM x86 Stack Frame Setup and Teardown

ASM x86

Goal -- WPM

Ready
Exercise Algorithm Area
1; Recursive factorial calculation with explicit stack frame management
2
3section .text
4global _start
5
6_start:
7; Example usage: calculate factorial of 5
8mov rdi, 5
9call factorial
10
11; Exit program
12mov rax, 60
13mov rdi, 0
14syscall
15
16; Function: factorial
17; Computes the factorial of a non-negative integer.
18; Input: RDI = integer n
19; Output: RAX = n!
20factorial:
21; --- Prologue: Set up stack frame ---
22push rbp ; Save old base pointer
23mov rbp, rsp ; Set new base pointer
24push rbx ; Save callee-saved register
25push r12 ; Save callee-saved register
26push r13 ; Save callee-saved register
27push r14 ; Save callee-saved register
28push r15 ; Save callee-saved register
29
30; Allocate space for local variables (if any)
31; sub rsp, 16 ; Example: 16 bytes for local vars
32
33; --- Function Body ---
34; Base case: if n <= 1, return 1
35cmp rdi, 1
36jle .base_case
37
38; Recursive step: n * factorial(n-1)
39; Save n for later multiplication
40mov rbx, rdi ; Use RBX to store n
41
42; Prepare argument for recursive call: n-1
43dec rdi
44call factorial
45
46; Result of factorial(n-1) is in RAX
47; Multiply by n (stored in RBX)
48imul rax, rbx
49
50; --- Epilogue: Restore stack frame ---
51.return_from_recursion:
52; Deallocate local variables (if any)
53; add rsp, 16 ; Example: deallocate 16 bytes
54
55pop r15 ; Restore callee-saved register
56pop r14 ; Restore callee-saved register
57pop r13 ; Restore callee-saved register
58pop r12 ; Restore callee-saved register
59pop rbx ; Restore callee-saved register
60pop rbp ; Restore old base pointer
61ret ; Return to caller
62
63.base_case:
64mov rax, 1 ; Factorial of 0 or 1 is 1
65jmp .return_from_recursion ; Jump to epilogue
Algorithm description viewbox

ASM x86 Stack Frame Setup and Teardown

Algorithm description:

This ASM x86 code implements a recursive factorial function with explicit stack frame management. It demonstrates the standard prologue and epilogue for setting up and tearing down stack frames, saving and restoring callee-saved registers, and handling the base case and recursive step. This is crucial for understanding how function calls work at a low level and managing program state.

Algorithm explanation:

The `factorial` function calculates n! recursively. The prologue (`push rbp`, `mov rbp, rsp`, `push callee-saved registers`) establishes a new stack frame, saving the caller's context and allocating space for local variables. The base case (`cmp rdi, 1`, `jle .base_case`) handles n=0 or n=1 by returning 1. The recursive step (`dec rdi`, `call factorial`) computes (n-1)!, and then multiplies the result by n. The epilogue (`pop callee-saved registers`, `pop rbp`, `ret`) restores the caller's stack frame and registers before returning. This ensures that each recursive call has its own isolated state and that the caller's context is preserved. The space complexity is O(n) due to the recursion depth, and the time complexity is also O(n) because each call performs constant work plus a recursive call.

Pseudocode:

FUNCTION factorial(n):
  SAVE rbp
  SET rbp = rsp
  SAVE callee-saved registers

  IF n <= 1 THEN
    SET rax = 1
    GOTO epilogue
  ELSE
    SAVE n in rbx
    DECREMENT n
    CALL factorial(n)
    MULTIPLY rax BY rbx
  END IF

epilogue:
  RESTORE callee-saved registers
  RESTORE rbp
  RETURN