我正在尝试浏览一个带有汇编指令的简单文本文件,它看起来像这样
TOP NOP
VAL INT 0
TAN LA 2,1
这只是一个小例子,所以我可以向您展示它是如何工作的。基本上,我将第一个标签放入标签中,然后将第二个标签(NOP、INT 和 LA)放入操作码中。
之后,我将采用第一个参数(0 和 2)并将它们放在 arg1 中。然而,这就是我的问题所在。使用我拥有的当前代码,当我将参数放入字符串时得到的输出就是这样
TOP
0
2
显然,我只想让最后两个成为唯一的,但是我如何做到这一点,这样我的第一个论点就不会被抛出 TOP 呢?
#include <string>
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
// If no extra file is provided then exit the program with error message
if (argc <= 1)
{
cout << "Correct Usage: " << argv[0] << " <Filename>" << endl;
exit (1);
}
// Array to hold the registers and initialize them all to zero
int registers [] = {0,0,0,0,0,0,0,0};
string memory [16000];
string Symtablelab[1000];
int Symtablepos[1000];
string line;
string label;
string opcode;
string arg1;
string arg2;
// Open the file that was input on the command line
ifstream myFile;
myFile.open(argv[1]);
if (!myFile.is_open())
{
cerr << "Cannot open the file." << endl;
}
int counter = 0;
int i = 0;
int j = 0;
while (getline(myFile, line, '\n'))
{
if (line[0] == '#')
{
continue;
}
if (line.length() == 0)
{
continue;
}
if (line[0] != '\t' && line[0] != ' ')
{
string delimeters = "\t ";
int current;
int next = -1;
current = next + 1;
next = line.find_first_of( delimeters, current);
label = line.substr( current, next - current );
Symtablelab[i] = label;
current = next + 1;
next = line.find_first_of(delimeters, current);
opcode = line.substr(current, next - current);
if (opcode != "WORDS" && opcode != "INT")
{
counter += 3;
}
if (opcode == "INT")
{
counter++;
}
delimeters = ", \n\t";
current = next + 1;
next = line.find_first_of(delimeters, current);
arg1 = line.substr(current, next-current);
cout << arg1<<endl;
i++;
}
}