#include<iostream>
#include<iomanip>
#include<string>
#include <fstream>
#include<vector>
#include <algorithm>
#include<math.h>
using namespace std;
int main()
{
int i, j, k,l,m,n,o;
int hex = 0,count,len,start,end;
char t;
fstream file;
string filename;//INTFILE.txt
fstream outputname;
fstream SYMTAB;
char int_len[7] ;
cin >> filename;
file.open(filename, ios::in); //將檔案開啟為輸入狀態
outputname.open("answer.txt", ios::out);
SYMTAB.open("SYMTAB.txt", ios::out);
if (!file) //檢查檔案是否成功開啟
{
cerr << "Can't open file!\n";
exit(1); //在不正常情形下,中斷程式的執行
}
else
{
string inputline;
string tmp = "";
string op[] = { "ADD", "AND", "COMP", "DIV", "J", "JEQ", "JGT", "JLT", "JSUB", "LDA", "LDCH", "LDL", "LDX", "MUL", "OR", "RD", "RSUB", "STA", "STCH", "STL", "STSW", "STX", "SUB", "TD", "TIX", "WD" }; //SIC
string op_code_table[] = { "18", "40", "28", "24", "3C", "30", "34", "38", "48", "00", "50", "08", "04", "20", "44", "D8", "4C", "0C", "54", "14", "E8", "10", "1C", "E0", "2C", "DC" };
vector<string>label;
vector<string>opcode;
vector<string>operand;
vector<int>LOC;
LOC.push_back(0);
LOC.push_back(0);
while (getline(file, inputline))
{
if (inputline[0] == '.')
continue;
////////////// L a b e l///////////////
tmp = "";
for (i = 0; i < 8; i++)
if (inputline[i] == ' ')
continue;
else
tmp = tmp + (inputline[i]);
label.push_back(tmp);
////////////// O p c o d e/////////////
tmp = "";
for (j = 9; j<15; j++)
{
if (inputline[j] == '\0')
break;
else if (inputline[j] == ' ')
continue;
else
tmp = tmp + (inputline[j]);
}
opcode.push_back(tmp);
////////////// O p e r a n d/////////////
tmp = "";
for (k = 17; k<35; k++)
{
if ((inputline.length()<17) || inputline[k] == '\0' || inputline[k] == '\n' || inputline[k] == 'NULL')
break;
else if (inputline[k] == ' ')
continue;
else
tmp = tmp + (inputline[k]);
}
operand.push_back(tmp);
}
for (i = 0; i < label.size();i++)
transform(label[i].begin(), label[i].end(), label[i].begin(), toupper);
for (j = 0; j < opcode.size(); j++)
transform(opcode[j].begin(), opcode[j].end(), opcode[j].begin(), toupper);
for (k = 0; k < operand.size(); k++)
transform(operand[k].begin(), operand[k].end(), operand[k].begin(), toupper);
for (j = 0; j < opcode.size(); j++)
////// C 'eof' ////////////
for (k = 0; k < operand.size(); k++)
{
if (!(operand[k].find("C'")))
{
tmp = "C'";
for (i = 2; i < operand[k].length(); i++)
{
if (operand[k][i] == '\'')
{
tmp = tmp + '\'';
break;
}
t = operand[k][i] + 32;
tmp = tmp + t;
}
operand[k] = tmp;
}
}
for (m = 1; m < opcode.size()-1; m++) //special opcode
{
if (opcode[m] == "BYTE")
{
count = 0;
if (!operand[m].find("C"))
{
for (i = 2; operand[m][i] != '\''; i++)
count++;
LOC.push_back(LOC[m] + count);
}
else
LOC.push_back(LOC[m] + 1);
}
else if (opcode[m] == "RESW")
LOC.push_back(LOC[m] + 3 * atoi(operand[m].c_str()));
else if (opcode[m] == "RESB")
LOC.push_back(LOC[m] + atoi(operand[m].c_str()));
else
LOC.push_back(LOC[m] + 3);
}
for (n = 0; n < operand[0].size(); n++) //START to 10's
{
if (operand[0][n] == 'A')
hex = hex + 10*pow(16.0, operand[0].size() - 1-n);
else if (operand[0][n] == 'B')
hex = hex + 11 * pow(16.0, operand[0].size() - 1 - n);
else if (operand[0][n] == 'C')
hex = hex + 12 * pow(16.0, operand[0].size() - 1 - n);
else if (operand[0][n] == 'D')
hex = hex + 13 * pow(16.0, operand[0].size() - 1 - n);
else if (operand[0][n] == 'E')
hex = hex + 14 * pow(16.0, operand[0].size() - 1 - n);
else if (operand[0][n] == 'F')
hex = hex + 15 * pow(16.0, operand[0].size() - 1 - n);
else
hex = hex + (operand[0][n]-'0')*pow(16.0, (operand[0].size() - n - 1));
}
len = 6-operand[0].length();
//// P r i n t //////////////////
for (l = 0; l < label.size(); l++)
{
for (i = 0; i < len; i++)
outputname << "0";
outputname << std::uppercase << std::hex << LOC[l] + hex << " ";
outputname << left << setw(9) << label[l];
if (operand[l].length() == 0)
outputname << opcode[l] << endl;
else
outputname << setw(6) << opcode[l] << " " << operand[l] << endl;
}
//////SYMTAB print//////////////////
for (i = 0; i < opcode.size(); i++)
{
if (opcode[i] == "START")
start = LOC[i];
if (opcode[i] == "END")
end = LOC[i];
}
for (j = 0; j < label.size(); j++)
{
if (j == 0)
{
SYMTAB << std::uppercase << left << setw(7) << label[j];
SYMTAB << right<<setw(6)<<setfill('0') << std::hex << (start + hex)<<" ";
SYMTAB <<setw(6)<< right << (end - start) << endl;
}
else if (label[j] == "")
;
else
{
SYMTAB << std::uppercase << left << setw(6)<<setfill(' ') << label[j]<<" ";
SYMTAB << std::uppercase << right << setw(6) << setfill('0')<< std::hex << LOC[j] + hex << endl;
}
}
}
return 0;
}
沒有留言:
張貼留言