1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
void pass1(string file)
{
//Variables and struct
int LOCCTR = 0;
ifstream ofile;
string codeLine, cLabel, labNum, cOp, opNum, cIn, cComm, rest;
struct OPTAB {
string opcode;
string effect;
};
OPTAB ADD, AND, COMP, DIV, J, JEQ, JGT, JLT, JSUB, LDA, LDCH, LDL, LDX, MUL, OR, RD,
RSUB, STA, STCH, STL, STX, SUB, TD, TIX, WD, START, END, BYTE, WORD, RESB, RESW;
ADD.opcode = "18";
ADD.effect = "A <- (A) + (m..m+2)";
AND.opcode = "58";
AND.effect = "A <- (A) & (m..m+2) [bitwise]";
COMP.opcode = "28";
COMP.effect = "cond code <- (A) : (m..m+2)";
DIV.opcode = "24";
DIV.effect = "A <- (A) / (m..m+2)";
J.opcode = "3C";
J.effect = "PC <- m";
JEQ.opcode = "30";
JEQ.effect = "PC <- m if cond code set to =";
JGT.opcode = "34";
JGT.effect = "PC <- m if cond code set to >";
JLT.opcode = "38";
JLT.effect = "PC <- m if cond code set to <";
JSUB.opcode = "48";
JSUB.effect = "L <- (PC); PC <- m";
LDA.opcode = "00";
LDA.effect = "A <- (m..m+2)";
LDCH.opcode = "50";
LDCH.effect = "A[rightmost byte] <- (m)";
LDL.opcode = "08";
LDL.effect = "L <- (m..m+2)";
LDX.opcode = "04";
LDX.effect = "X <- (m..m+2)";
MUL.opcode = "20";
MUL.effect = "A <- (A) * (m..m+2)";
OR.opcode = "44";
OR.effect = "A <- (A) | (m..m+2) [bitwise]";
RD.opcode = "D8";
RD.effect = "A[rightmost byte] <- data from device specified by (m)";
RSUB.opcode = "4C";
RSUB.effect = "PC <- (L)";
STA.opcode = "0C";
STA.effect = "m..m+2 <- (A)";
STCH.opcode = "54";
STCH.effect = "m <- (A)[rightmost byte]";
STL.opcode = "14";
STL.effect = "m..m+2 <- (L)";
STX.opcode = "10";
STX.effect = "m..m+2 <- (X)";
SUB.opcode = "1C";
SUB.effect = "A <- (A) - (m..m+2)";
TD.opcode = "E0";
TD.effect = "Test device specified by (m)";
TIX.opcode = "2C";
TIX.effect = "X <- (X) + 1; compare X and (m..m+2)";
WD.opcode = "DC";
WD.effect = "Device specified by (m) <- (A)[rightmost byte]";
START.opcode = "E5";
START.effect = "Program is to be loaded at location n (given in hexadecimal)";
END.opcode = "E6";
END.effect = "Physical end of program; label is first executable program statement";
BYTE.opcode = "E7";
BYTE.effect = "Stores either character strings (C'...') or hexadecimal values (X'...')";
WORD.opcode = "E8";
WORD.effect = "Stores the value v in a WORD";
RESB.opcode = "E9";
RESB.effect = "Reserves space for n bytes";
RESW.opcode = "EA";
RESW.effect = "Reserves space for n words (3n bytes)";
//Clear strings for use
codeLine.clear();
cLabel.clear();
cOp.clear();
cIn.clear();
cComm.clear();
//Open file given
ofile.open(file, ios::in);
if (!ofile)
{
cout << file << " could not be found!" <<endl;
}
//Read file
while (!ofile.eof() | cIn != "END")
{
//Read line
getline(ofile,codeLine);
cout << codeLine<<endl;
ofstream intermediate;
intermediate.open("Intermediate.txt", ios::app | ios::out);
if (codeLine[0] == '.')
{
intermediate << codeLine << endl;
codeLine.clear();
}else if (codeLine[0] == ' '| codeLine[0] == '\t' | isalpha(codeLine[0]))
{
//Split line into label, instruction, opcode, comment
if (isalnum(codeLine[0]))
{
istringstream ss(codeLine);
ss >> cLabel;
ss >> cIn;
ss >> cOp;
if (cIn == "START")
{
LOCCTR = atoi(cOp.c_str());
}
//Place into variables
//Check for errors
getline (ss, cComm);
intermediate<<endl<<"---------------------------------------------------------------------------"<< endl;
intermediate << cLabel << '\t' << cIn << '\t' << cOp << '\t' << cComm << endl;
/*while (opNum != cOp)
{
opNum = find(ADD.opcode, RESW.opcode, cOp);
}*/
intermediate << cOp << endl;
intermediate << LOCCTR << endl;
intermediate << cOp << endl;
//intermediate << Write errors << endl;
}
if (codeLine[0] == ' '| codeLine[0] == '\t')
{
istringstream ss(codeLine);
ss >> cIn;
//Search for instruction
ss >> cOp;
//Place into variables
//Check for errors
getline (ss, cComm);
intermediate <<endl<<"---------------------------------------------------------------------------"<< endl;
intermediate << '\t' << cIn << '\t' << cOp << '\t' << cComm << endl;
//intermediate << Write OPCODE value << endl;
//intermediate << Write address << endl;
intermediate << cOp << endl;
//intermediate << Write errors << endl;
}
}
if (ofile.eof() | cIn == "END")
{
intermediate.close();
cout << endl << "(End of File)" << endl;
}
codeLine.clear();
}
ofile.close();
}
|