为什么一开始需要输入两个数字才开始执行
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class QD_ALU {
public:
const string& aluName;
int AC;
QD_ALU(const string& aluName) :aluName(aluName), AC(0) {}
};
class QD_Memory {
public:
vector<int> memory;
QD_Memory(int size) : memory(size, 0) {}
};
class QD_In {
public:
const string& inName;
QD_In(const string& inName) :inName(inName) {}
int read() {
int input;
cin >> input;
return input;
}
};
class QD_Out {
public:
const string& outName;
QD_Out(const string& outName) :outName(outName) {}
void write(int output) {
cout << output << std::endl;
}
};
class QD_CU {
public:
const string& cuName;
int PC;
QD_CU(const string& cuName, QD_Memory* m, QD_In* in, QD_Out* out, QD_ALU* alu) :cuName(cuName), PC(0), m(m), in(in), out(out), alu(alu) {}
void run(vector<int>& instructions) {
while (instructions[PC] != 4300) {
execute(instructions[PC]);
}
}
private:
QD_Memory* m;
QD_In* in;
QD_Out* out;
QD_ALU* alu;
void execute(int instruction) {
switch (instruction / 100) {
case 10:
m->memory[instruction % 100] = in->read();
cout << "read: " << in->read() << " adress: " << instruction % 100 << endl;
PC++;
break;
case 11:
cout << "write: " << m->memory[instruction % 100]<<" adress: " << instruction % 100 << endl;
out->write(m->memory[instruction % 100]);
PC++;
break;
case 20:
alu->AC = m->memory[instruction % 100];
cout << "adress:"<< instruction % 100<<" AC "<<alu->AC<<endl;
PC++;
break;
case 21:// STORE
m->memory[instruction % 100] = alu->AC;
PC++;
cout << " AC " << alu->AC << "adress:" << instruction % 100 << endl;
break;
case 30:// ADD
alu->AC += m->memory[instruction % 100];
PC++;
cout << " AC " << alu->AC << endl;
break;
case 31:
// SUB
alu->AC -= m->memory[instruction % 100];
PC++;
cout << " AC " << alu->AC << endl;
break;
case 40:
// BR
PC = instruction % 100;
cout << PC;
break;
default:
std::cout << "Invalid instruction: " << instruction << std::endl;
break;
}
}
};
class Computer {
public:
QD_CU cu;
QD_ALU alu;
QD_Memory memory;
QD_In in;
QD_Out out;
Computer(const string& cuName, const string& aluName, int memorySize, const string& inName, const string& outName)
: cu(cuName, &memory, &in, &out, &alu), alu(aluName), memory(memorySize), in(inName), out(outName) {}
void run(vector<int>& instructions) {
cu.run(instructions);
}
};
int main() {
Computer mycomputer1("青芯CU1", "青芯ALU1", 1024, "输入设备1", "输出设备1");
vector<int> instructions = { 1007, 1008, 2007, 3008, 2109, 1109, 4010, 0000, 0000, 0000, 4300 };
mycomputer1.run(instructions);
return 0;
}
