Learn how to use the stack in MIPS assembly! 📦 In this tutorial, I break down stack management, function calls, recursion, parameter passing, and multiple return values using the MARS simulator. Understand how $sp (stack pointer), push/pop operations, and accessing local variables work in MIPS assembly programming.
🔹 First Program: Recursive Factorial with Stack
✔ Stack Setup – Pushing $ra (return address) and $a0 (argument) onto the stack
✔ Recursive Call – Saving state and calling factorial(n-1)
✔ Stack Cleanup – Restoring $ra and $a0 before returning
🔹 Second Program: Passing Extra Parameters via Stack
✔ First Four Arguments – Passed in $a0-$a3
✔ Extra Parameters – Stored on the stack and accessed with lw
✔ Stack Cleanup – Saving and restoring $ra properly
🔹 Third Program: Using Stack for Local Variables
✔ Allocating Stack Space – Using addi $sp, $sp, -12
✔ Storing Local Variables – sum (0($sp)), diff (4($sp))
✔ Retrieving and Using Values – Performing arithmetic and restoring values
🔹 Fourth Program: Returning Multiple Values
✔ Returning in Registers – $v0 (sum), $v1 (product)
✔ Passing a Pointer for Extra Return Values – Storing difference (a - b) in stack memory
✔ Retrieving the Third Return Value – Caller loads from stack memory using lw
📌 Perfect for beginners learning MIPS stack operations, recursion, and function conventions!
👍 Like, subscribe, and hit the notification bell for more MIPS tutorials!
#MIPS #assembly #stackpointer #recursion #MARS #programming