include
include
using namespace std;
class CPU { private: int A, B; // Registers int PC; // Program Counter vector memory; bool running;
public: CPU() : A(0), B(0), PC(0), running(true) { memory.resize(256, 0); }
void loadProgram(const vector<int>& program) {
for (size_t i = 0; i < program.size(); ++i) {
memory[i] = program[i];
}
}
void run() {
while (running) {
int opcode = memory[PC++];
switch (opcode) {
case 1: // LOAD A
A = memory[PC++];
break;
case 2: // LOAD B
B = memory[PC++];
break;
case 3: // ADD A and B
A = A + B;
break;
case 4: // SUB A and B
A = A - B;
break;
case 5: // PRINT A
cout << "Register A: " << A << endl;
break;
case 0: // HALT
running = false;
break;
default:
cout << "Unknown instruction: " << opcode << endl;
running = false;
break;
}
}
}
};
int main() { CPU cpu; // Program: LOAD A with 10, LOAD B with 5, ADD A and B, PRINT A, HALT vector program = { 1, 10, // LOAD A = 10 2, 5, // LOAD B = 5 3, // ADD A + B 5, // PRINT A 0 // HALT };
cpu.loadProgram(program);
cpu.run();
return 0;
}
Log in or sign up for Devpost to join the conversation.