├── LICENSE
├── README.md
└── stack_machine.ino
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Lohit Kolluri
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
Stack Machine Simulator
2 |
3 | A simple Arduino sketch that simulates a stack machine allowing you to execute stack-based operations via serial communication.
4 |
5 | 🚀 Demo
6 |
7 | [www.tinkercad.com/things/fhYWyLqPAry](www.tinkercad.com/things/fhYWyLqPAry)
8 |
9 |
10 |
11 | 🧐 Features
12 |
13 | Here're some of the project's best features:
14 |
15 | * Push values onto the stack.
16 | * Pop values from the stack.
17 | * Perform addition multiplication and division operations.
18 | * Error handling for stack underflow and division by zero.
19 | * Interaction via the Arduino Serial Monitor.
20 |
21 | 🛠️ Installation Steps:
22 |
23 | 1. **Upload the provided Arduino sketch:**
24 | - Use the Arduino IDE or compatible online platforms to upload the sketch to your Arduino board.
25 |
26 | 2. **Open the Arduino Serial Monitor:**
27 | - Navigate to Tools -> Serial Monitor in the Arduino IDE or use a serial communication tool if you're using Tinkercad.
28 |
29 | 3. **Enter instructions in the Serial Monitor:**
30 | - Supported commands include:
31 | - `PUSH `: Push a floating-point value onto the stack.
32 | - `POP`: Pop the top value from the stack.
33 | - `ADD`: Perform addition on the top two stack values.
34 | - `MUL`: Perform multiplication on the top two stack values.
35 | - `DIV`: Perform division on the top two stack values (avoiding division by zero).
36 |
37 | 4. **Observe the stack's current state and operation results:**
38 | - View the Serial Monitor for real-time feedback on the stack's state and any operation results.
39 |
40 |
41 | 🛡️ License:
42 |
43 | This project is licensed under the [MIT License](LICENSE)
44 |
--------------------------------------------------------------------------------
/stack_machine.ino:
--------------------------------------------------------------------------------
1 | const int STACK_SIZE = 10;
2 | float stack[STACK_SIZE];
3 | int top = -1;
4 |
5 | void push(float value) {
6 | if (top < STACK_SIZE - 1) {
7 | top++;
8 | stack[top] = value;
9 | }
10 | }
11 |
12 | float pop() {
13 | if (top >= 0) {
14 | float value = stack[top];
15 | top--;
16 | return value;
17 | }
18 | return 0.0;
19 | }
20 |
21 | void setup() {
22 | Serial.begin(9600);
23 | }
24 |
25 | void loop() {
26 | if (Serial.available() > 0) {
27 | String instruction = Serial.readStringUntil('\n');
28 | executeInstruction(instruction);
29 | }
30 | }
31 |
32 | void executeInstruction(String instruction) {
33 | instruction.trim();
34 | Serial.print("Received: ");
35 | Serial.println(instruction);
36 |
37 | if (instruction.startsWith("PUSH")) {
38 | float value = instruction.substring(5).toFloat();
39 | push(value);
40 | } else if (instruction == "POP") {
41 | float value = pop();
42 | Serial.print("Popped: ");
43 | Serial.println(value);
44 | } else if (instruction == "ADD") {
45 | if (top >= 1) {
46 | float b = pop();
47 | float a = pop();
48 | push(a + b);
49 | } else {
50 | Serial.println("Not enough operands for ADD");
51 | }
52 | } else if (instruction == "MUL") {
53 | if (top >= 1) {
54 | float b = pop();
55 | float a = pop();
56 | push(a * b);
57 | } else {
58 | Serial.println("Not enough operands for MUL");
59 | }
60 | } else if (instruction == "DIV") {
61 | if (top >= 1) {
62 | float b = pop();
63 | float a = pop();
64 | if (b != 0) {
65 | push(a / b);
66 | } else {
67 | Serial.println("Division by zero");
68 | }
69 | } else {
70 | Serial.println("Not enough operands for DIV");
71 | }
72 | } else {
73 | Serial.println("Invalid instruction");
74 | }
75 |
76 | Serial.print("Stack: [");
77 | for (int i = 0; i <= top; i++) {
78 | Serial.print(stack[i]);
79 | if (i < top) {
80 | Serial.print(", ");
81 | }
82 | }
83 | Serial.println("]");
84 | }
--------------------------------------------------------------------------------