A custom stack machine implemented in MIPS Assembly, featuring arithmetic operations, stack manipulation (push/pop), printing, and persistence via file save/load.
MIPS Assembly Stack Machine Emulator RISC MARS SPIM Computer-Architecture System-Programming File-IO Menu-Driven
A menu-driven, custom stack machine emulator written entirely in MIPS Assembly Language. This project demonstrates fundamental concepts of computer architecture, system programming, and low-level stack management, including persistence through file I/O operations.
Since this project is written in MIPS Assembly, it must be executed in a compatible MIPS simulator, such as MARS or SPIM.
This project implements a custom stack machine, often used to process instructions in Reverse Polish Notation (RPN). It is designed to run on a MIPS simulator (like MARS) and offers a command-line menu interface for user interaction.
The program dynamically allocates the stack memory (heap) at startup, based on user input, and manages the stack pointer ($t8) to handle push and pop operations. Critical error handling is implemented for stack overflow, stack underflow, invalid user input, and integer arithmetic overflow.
The stack machine supports the following functionalities via a command menu (0-7):
- Exit (0): Terminates the program cleanly.
- Push (1): Prompts the user for an integer and pushes it onto the stack. Checks for stack overflow.
- Pop (2): Removes the top element from the stack, prints its value to the console, and updates the stack pointer. Checks for stack underflow (empty stack).
- Print (3): Iterates through the stack and prints all stored elements, each on a new line.
- Add (4): Pops the top two elements, performs an integer addition, and pushes the result back onto the stack. Includes MIPS-specific overflow detection.
- Multiply (5): Pops the top two elements, performs an integer multiplication, and pushes the result back onto the stack. Includes MIPS-specific overflow detection using
mfhi(checking high-order word). - Dump (6) / Save: Saves the current contents of the stack to a user-specified file. The file content includes the stack elements as strings, followed by a
*character and the total allocated stack size, enabling accurate restoration. - Load (7): Loads a stack from a user-specified file. It validates that the loaded stack size matches the program's initial heap size before populating the current stack.
To run this project, you need a MIPS Assembly emulator. The most common choice in educational settings is MARS (MIPS Assembler and Runtime Simulator), which is recommended.
- Download the Emulator: Obtain the latest version of MARS or SPIM.
- Open the Source File: Launch the emulator and open the
q8.asmfile. - Assemble: In the emulator, use the "Assemble" or equivalent command (often the F3 key in MARS).
- Run: Execute the assembled program (often the F5 key in MARS).
Upon running, the program will prompt you to define the stack size:
Enter heap size:
Note: The heap size you enter must be a positive integer divisible by 4 (since MIPS words are 4 bytes).
After initialization, the main menu will be displayed:
0: Exit
1: Push
2: Pop
3: Print
4: Add
5: Multiply
6: Dump
7: Load
Please enter your choice:
Enter the number corresponding to the desired operation.
- Enter
1(Push). - Please enter number:
10Done! - Enter
1(Push). - Please enter number:
5Done! - Enter
4(Add). (The stack pops 5 and 10, adds them to get 15, and pushes 15 back) Done! - Enter
3(Print). 15
- Dump (6): You will be prompted for a filename (e.g.,
mystack). The program automatically appends.txt(e.g.,mystack.txt) and saves the stack contents. - Load (7): You will be prompted for the saved filename. The program will only load the stack if the stack size encoded in the file matches the current allocated heap size.
Contributions are welcome! If you find a bug or have an idea for an improvement, please follow these steps:
- Report Issues: Open an issue to discuss the bug or proposed feature.
- Fork the Repository: Create a fork of the project.
- Create a Branch: Create a dedicated branch for your feature or fix (e.g.,
feature/new-operationorfix/overflow-bug). - Submit a Pull Request (PR): Submit a pull request with a clear description of your changes.
This project is released under the MIT License.
For questions or support, please contact the repository owner or open an issue on GitHub.
You can start learning the fundamentals of the MIPS instruction set and assembly language by watching You Can Learn MIPS Assembly in 15 Minutes.