C++读取TXT文件指令实现简易计算器问题求助:jmp指令无法正常工作
Hey there! Let's get that jmp instruction working properly. The core issue with your current code is that you're reading the file line-by-line in a loop—you can't easily jump around because the file stream only moves forward sequentially. Instead, we need to first store all your instructions in a list, then use an index to navigate through them freely.
Here's the step-by-step fix:
Store all instructions first
We'll load every line of your input file into a vector of pairs (each pair holds the command and its number). This lets us access any instruction directly using an index, instead of being tied to the file's read order.Use a program counter (PC) to track execution position
Replace youricounter with apcvariable that represents the index of the current instruction we're executing. When we hit a jmp, we just adjust this index to jump to the target line.Add loop protection
Without tracking which instructions we've already run, we could end up in an infinite loop (e.g., a jmp that sends us back to a previous instruction). A simple boolean vector will let us mark instructions as visited to avoid this.
Modified Code
#include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> using namespace std; int main() { ifstream file("my.txt"); vector<pair<string, int>> instructions; string cmd; int num; // Load all instructions into memory first if (file.is_open()) { while (file >> cmd >> num) { instructions.emplace_back(cmd, num); } file.close(); } else { cout << "Error opening file!" << endl; cin.get(); return 1; } int var = 0; int pc = 0; // Tracks the current instruction index vector<bool> visited(instructions.size(), false); // Prevent infinite loops while (pc < instructions.size() && !visited[pc]) { visited[pc] = true; auto& current_instr = instructions[pc]; cmd = current_instr.first; num = current_instr.second; cout << "Command: " << cmd << " Number: " << num << " ---- PC: " << pc << " // Var: " << var << endl; if (cmd == "acc") { var += num; pc++; // Move to next instruction after acc } else if (cmd == "jmp") { pc += num; // Jump directly by adjusting the program counter } else if (cmd == "nop") { pc++; // Do nothing, just move to next instruction } } cout << "\nFinal accumulator value: " << var << endl; cin.get(); return 0; }
Key Changes Explained
- Instruction Storage: By loading all commands into a
vector, we break free from the file stream's sequential read limitation—now we can jump to any line instantly. - Program Counter: The
pcvariable is our navigation tool. Forjmp +262, we just add 262 topcto jump forward 262 instructions. Foraccandnop, we incrementpcby 1 to move to the next line. - Loop Prevention: The
visitedarray ensures we don't re-run the same instruction twice, which stops the program from getting stuck in an infinite loop (a common edge case with jmp instructions).
Give this code a try—your jmp instructions should now work as expected, and you'll also avoid accidental infinite loops!
内容的提问来源于stack exchange,提问作者Moblie Games

