-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssembler.cpp
More file actions
276 lines (242 loc) · 9.53 KB
/
Assembler.cpp
File metadata and controls
276 lines (242 loc) · 9.53 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Team:
Nicholas Dibello-Hitta, cssc4049, RedID: 828490930
Dylan Agoncillo, cssc4051, RedID: 824340550
*/
#include "Assembler.h"
#include "AppendixA.h"
#include <fstream>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
//global variables
std::ifstream inputFile;
std::ofstream outputFile;
bool extFormat = false;
int locctr = 0,
programCounter = 0,
baseRegister = 0,
opni = 0,
xbpe = 0,
displacement = 0,
objectCode = 0;
std::unordered_map<std::string, int> SYMTAB;
//column sizes
const int address_Column = 4,
symbol_Column = 9,
instruction_Column = 8,
operand_Column = 20,
opcode_Column = 4,
comment_Column = 20;
int assemblerPass = 0;
bool validate_input(const std::string passedFile) {
//Check to make sure that the user inputted the correct type of file (.sic)
std::cout << "file extension: " + passedFile.substr(passedFile.length() - 4) << std::endl; //TESTING
if (".sic" != passedFile.substr(passedFile.length() - 4)) {
std::cout << "incompatible file extension, exiting program" << std::endl;
return 1;
}
return 0;
}
void remove_whiteSpaces(std::vector<std::string> ¤tLine) {
for (int i = 0; i < currentLine.size(); i++) {
int start = currentLine[i].find_first_not_of(" \t\n\r\f\v");
size_t end = currentLine[i].find_last_not_of(" \t\n\r\f\v");
if (start != currentLine[i].npos) { //only strip whitespaces if the line isn't blank
currentLine[i] = currentLine[i].substr(start, end - start + 1);
}
}
}
//I'm putting these functions here because it's only used in pass 1
void processLine(std::vector<std::string> ¤tLine) {
currentLine.clear();
std::string temp;
std::cout << "Line 54" << std::endl;
//get the first line
std::getline(inputFile, temp);
std::cout << "temp variable: " << temp << std::endl;
std::cout << "assemblerPass: " << std::to_string(assemblerPass) << std::endl;
//if it's a comment, just pass it as the current line
if (temp[0] == '.') currentLine.push_back(temp);
//if it's a regular line, separate it into symbol, instruction, operand, and
else if (assemblerPass == 1) {
currentLine.push_back(temp.substr(0, symbol_Column));
currentLine.push_back(temp.substr(symbol_Column, instruction_Column));
currentLine.push_back(temp.substr(symbol_Column + instruction_Column, operand_Column));
//if format 4
if (temp.size() > (symbol_Column + instruction_Column + operand_Column)) {
currentLine.push_back(temp.substr(symbol_Column + instruction_Column + operand_Column));
}
} else if (assemblerPass == 2) {
currentLine.push_back(temp.substr(0, address_Column));
currentLine.push_back(temp.substr(address_Column + 1, symbol_Column)); //have to include the buffer between address and symbol
currentLine.push_back(temp.substr(address_Column + 1 + symbol_Column, instruction_Column));
currentLine.push_back(temp.substr(address_Column + 1 + symbol_Column + instruction_Column, operand_Column));
//if format 4
if (temp.size() > (address_Column + 1 + symbol_Column + instruction_Column + operand_Column)) {
currentLine.push_back(temp.substr(address_Column + 1 + symbol_Column + instruction_Column + operand_Column));
}
}
//remove whitespaces
remove_whiteSpaces(currentLine);
for (int i = 0; i < currentLine.size(); i++) {
std::cout << "currentLine[" << std::to_string(i) << "]:" << currentLine[i] << ":\n";
}
}
//I want this to mirror processLine
void outputToFile(std::vector<std::string> ¤tLine) {
//write comments straight to file
if (currentLine[0][0] == '.') {
outputFile << currentLine[0] << std::endl;
return;
}
if (assemblerPass == 1) {
//calculate address field
outputFile << std::setw(address_Column) << std::left << std::hex << locctr;
//buffer
outputFile << ' ';
//Symbols
outputFile << std::setw(symbol_Column) << std::left << currentLine[0];
//Extended format?
if (extFormat) {
outputFile << '+';
} else {
outputFile << ' ';
}
//Instructions
outputFile << std::setw(instruction_Column) << std::left << currentLine[1];
//Operand
outputFile << currentLine[2];
}
else if (assemblerPass == 2) {
//calculate address field
outputFile << std::setw(address_Column) << std::left << std::hex << currentLine[0];
//buffer
outputFile << ' ';
//Symbols
outputFile << std::setw(symbol_Column) << std::left << currentLine[1];
//Extended format?
if (extFormat) {
outputFile << '+';
} else {
outputFile << ' ';
}
//Instructions
outputFile << std::setw(instruction_Column) << std::left << currentLine[2];
//Operand
outputFile << std::setw(operand_Column) << std::left << currentLine[3];
// std::cout << "printing out the value of currentLine again in outputToFile" << std::endl;
// for (int i = 0; i < currentLine.size(); i++) {
// std::cout << "currentLine[" << std::to_string(i) << "]:" << currentLine[i] << ":\n";
// }
std::cout << "testing" << std::endl;
//Assembled object code
if (objectCode) outputFile << std::setw(opcode_Column) << std::left << std::hex << std::stoi(currentLine[4]);
std::cout << "line 148" << std::endl;
}
//End the line
outputFile << std::endl;
}
//This figure out the format of a given instruction
void instruction_formats(std::vector<std::string> currentLine) {
if (currentLine[1][0] == '+'){
extFormat = true;
}
else if (currentLine[1][0] == ' '){
//nothing to do
}
else{
//error throw?
}
if (AppendixA::OPTAB.find(currentLine[1]) != AppendixA::OPTAB.end()){
if (extFormat = true){
locctr += 4;
}
else{
locctr += 3;
}
}
else if (currentLine[1].compare("RESW") == 0){
locctr += 3 * std::stoi(currentLine[2]);
}
else if (currentLine[1].compare("RESB") == 0){
locctr += std::stoi(currentLine[2]);
}
else if (currentLine[1].compare("WORD") == 0){
locctr += 3;
}
else if (currentLine[1].compare("BYTE") == 0){
//if C, X, or D
}
else{
std::cout << "the current value of currentLine[1]:" << currentLine[1] << ": end " <<std::endl;
}
}
//Assemble object code from a given line
void assemble_object(std::vector<std::string> ¤tLine) {
if (currentLine[0][0] == '.') { //don't assemble comments :)
return;
}
objectCode = 0;
extFormat = false;
std::cout << "is " << currentLine[2] << " in our OPTAB? : ";
//is this command in our appendix A?
if (AppendixA::OPTAB.find(currentLine[2]) != AppendixA::OPTAB.end() || AppendixA::OPTAB.find(currentLine[2].substr(1)) != AppendixA::OPTAB.end()) {
std::cout << "yes!" << std::endl;
//set program counter & first 1 1/2 bytes
programCounter = std::stoi(currentLine[0], nullptr, 16) + 3;
xbpe = 0;
if (currentLine[2][0] == '+'){ //if it's extended format remove '+' before search
opni = AppendixA::OPTAB.at(currentLine[2].substr(1));
programCounter += 1; //one more byte for format 4
xbpe += 1;
//extFormat = true;
} else opni = AppendixA::OPTAB.at(currentLine[2]);
//find symbols in SYMTAB
if (SYMTAB.find(currentLine[3].substr(0, currentLine[3].find_first_of(", "))) != SYMTAB.end()) {
//set displacement equal to the location of the symbol in memory
displacement = SYMTAB.at(currentLine[3].substr(0, currentLine[3].find_first_of(", ")));
}
//if PC relative
if (-2048 <= (displacement - programCounter) <= 2047) {
std::cout << "PC Relative Addressing" << std::endl;
displacement -= programCounter;
xbpe += 2;
}
//Other addressing modes
if (currentLine[3][0] == '#') {
std::cout << "Immediate Addressing" << std::endl;
opni += 1;
} else if (currentLine[3][0] == '@') {
std::cout << "Indirect Addressing" << std::endl;
opni += 2;
} else if (currentLine[3].substr(currentLine[3].size()-2).compare(",X") == 0) {
std::cout << "Indexed Addressing" << std::endl;
opni += 8;
}
else {
std::cout << "Direct Addressing" << std::endl;
opni += 3;
}
//assign values to objectCode
std::cout << "opni: " << std::to_string(opni) << std::endl;
std::cout << "xbpe: " << std::to_string(xbpe) << std::endl;
std::cout << "displacement: " << std::to_string(displacement) << std::endl;
if (extFormat) {
objectCode += opni << 24;
objectCode += xbpe << 20;
} else {
objectCode += opni << 16;
objectCode += xbpe << 12;
}
objectCode += static_cast<unsigned int>(displacement & 0xFFF);
std::cout << "Line 254" << std::endl;
}
else { //it's an assembler directive
std::cout << " NO :(" << std::endl;
}
//append assembled code to currentLine
std::cout << "objectCode for line " << currentLine[0] << " is: " << std::to_string(objectCode) << std::endl;
currentLine.push_back(std::to_string(objectCode));
}