Skip to content

Commit

Permalink
Added a Few more tests and resolved a few bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh0g0-1758 committed Apr 18, 2024
1 parent 41aaf82 commit 5bdb009
Show file tree
Hide file tree
Showing 21 changed files with 196 additions and 50 deletions.
File renamed without changes.
11 changes: 11 additions & 0 deletions Input/error_program2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SUM START 0
ZERO LDA ZERO
LDX ONE
ADDR X,A
USE CDATA
ONE WORD 1
USE
RSUB
USE CDATA
ZERO WORD 0
END FIRST
11 changes: 11 additions & 0 deletions Input/error_program3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SUM START 0
FIRST LDA ZRO
LDX ONE
ADDR X,A
USE CDATA
ONE WORD 1
USE
RSUB
USE CDATA
ZERO WORD 0
END FIRST
11 changes: 11 additions & 0 deletions Input/error_program4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SUM START 0
FIRST LDA ZERO
LDX ONE
ADDR X,A
USE CDATA
ONE WORD 1
USE
RSB
USE CDATA
ZERO WORD 0
END FIRST
10 changes: 10 additions & 0 deletions Input/error_program5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SUM START 0
FIRST LDA ZERO
LDX ONE
ADDR X,A
USE CDATA
ONE WORD 1
USE
RSUB
USE CDATA
ZERO WORD 0
8 changes: 8 additions & 0 deletions Input/test_program5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SUM START 0
FIRST LDA ZERO
LDX =X'01'
ADDR X,A
RSUB
LTORG
ZERO WORD 0
END FIRST
11 changes: 11 additions & 0 deletions Input/test_program6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SUM START 0
FIRST LDA ZERO
LDX ONE
ADDR X,A
USE CDATA
ONE WORD 1
USE
RSUB
USE CDATA
ZERO WORD 0
END FIRST
File renamed without changes.
1 change: 1 addition & 0 deletions Output/error_program2_expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Duplicate symbol: ZERO
1 change: 1 addition & 0 deletions Output/error_program3_expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INVALID OPERAND ZRO at LOCCTR 3
1 change: 1 addition & 0 deletions Output/error_program4_expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Invalid line: RSB
1 change: 1 addition & 0 deletions Output/error_program5_expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
END statement not found.
3 changes: 3 additions & 0 deletions Output/test_program5_expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HSUM 00000000000f
T0000000f03200907200590104f000001000000
E000000
6 changes: 6 additions & 0 deletions Output/test_program6_expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HSUM 000000000011
T0000000803200b0720059010
T00000b03000001
T000008034f0000
T00000e03000000
E000000
10 changes: 9 additions & 1 deletion RunTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ echo "./../Input/test_program1.txt" | ./assembler
echo "./../Input/test_program2.txt" | ./assembler
echo "./../Input/test_program3.txt" | ./assembler
echo "./../Input/test_program4.txt" | ./assembler
echo "./../Input/error_program.txt" | ./assembler
echo "./../Input/test_program5.txt" | ./assembler
echo "./../Input/test_program6.txt" | ./assembler
echo "./../Input/error_program1.txt" | ./assembler
echo "./../Input/error_program2.txt" | ./assembler
echo "./../Input/error_program3.txt" | ./assembler
echo "./../Input/error_program4.txt" | ./assembler
echo "./../Input/error_program5.txt" | ./assembler
cd tests
./assembler_test

./../Input/error_program5.txt
10 changes: 5 additions & 5 deletions include/initialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace std;
#include "utils.h"

void program_initialisation(string line, string &NAME, int &START_ADDRESS,
int &LOCCTR) {
int &LOCCTR, string File_Name) {
vector<string> tokens;
istringstream iss(line);
string token;
Expand All @@ -17,15 +17,15 @@ void program_initialisation(string line, string &NAME, int &START_ADDRESS,
}
if (tokens[1] != "START") {
string err_msg = "Invalid program start: " + tokens[1];
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
NAME = tokens[0];
START_ADDRESS = hexToInt(tokens[2]);
LOCCTR = START_ADDRESS;
}

void pre_process(string line, map<string, Opcode> &OPTAB) {
void pre_process(string line, map<string, Opcode> &OPTAB, string File_Name) {
Opcode opcode;
vector<string> tokens;
istringstream iss(line);
Expand All @@ -44,14 +44,14 @@ void pre_process(string line, map<string, Opcode> &OPTAB) {
opcode.format = Format::THREE;
} else {
string err_msg = "Invalid format value: " + tokens[1];
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
OPTAB[tokens[0]] = opcode;
} else {
string err_msg =
"Insufficient data values for opcode: " + tokens.size();
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
}
Expand Down
43 changes: 25 additions & 18 deletions include/pass1.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ void pass1(string line, bool &NOBASE,
int &prevLOCCTR, map<string, pair<int, int>> &BLOCK_TABLE,
int &BLOCK_NUMBER, string &CURR_BLOCK_NAME, int &TOTAL_BLOCKS,
map<int, string> &BLOCK_NAMES, map<string, int> &SYMBOL_BLOCK,
map<string, int> &LIT_BLOCK) {
map<string, int> &LIT_BLOCK, string File_Name, bool &has_end,
bool &has_rsub) {
Instruction instruction;
vector<string> tokens;
istringstream iss(line);
Expand Down Expand Up @@ -81,31 +82,37 @@ void pass1(string line, bool &NOBASE,
tokens.insert(tokens.begin(), "");
tokens.push_back("");
}
if (tokens[1] == "RSUB") {
if (tokens.size() > 1 and tokens[1] == "RSUB") {
tokens.push_back("");
}
if (tokens.size() == 2) {
tokens.insert(tokens.begin(), "");
}
if (tokens.size() == 1) {
string err_msg = "Invalid line: " + line;
save_error_msg(err_msg, File_Name);
exit(0);
}
if (tokens[1] == "END") {
if (SYMBOL_TABLE.find(tokens[2]) == SYMBOL_TABLE.end() or
SYMBOL_TABLE[tokens[2]] != START_ADDRESS) {
string err_msg = "Invalid program end: " + tokens[2];
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
has_end = true;
return;
}
if (!tokens[0].empty()) {
if (SYMBOL_TABLE.find(tokens[0]) != SYMBOL_TABLE.end()) {
string err_msg = "Duplicate symbol: " + tokens[0];
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
if (tokens[1] == "EQU") {
vector<string> equ_toks;
if (tokens[2] != "*") {
equ_toks = getExpressionTokens(tokens[2]);
equ_toks = getExpressionTokens(tokens[2], File_Name);
}
if (tokens[2] == "*") {
SYMBOL_TABLE[tokens[0]] = LOCCTR;
Expand All @@ -114,12 +121,12 @@ void pass1(string line, bool &NOBASE,
// Give Expr value here
if (equ_toks[0] == "+" or equ_toks[0] == "-") {
string err_msg = "INVALID STARTING EQU ARGUMENT.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
} else if (SYMBOL_TABLE.find(equ_toks[0]) ==
SYMBOL_TABLE.end()) {
string err_msg = "INVALID EQU ARGUMENT.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
int value = SYMBOL_TABLE[equ_toks[0]];
Expand All @@ -131,7 +138,7 @@ void pass1(string line, bool &NOBASE,
SYMBOL_TABLE.end() or
!is_digits(equ_toks[i + 1])) {
string err_msg = "INVALID EQU ARGUMENT.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
} else {
value = value + stoi(equ_toks[i + 1]);
Expand All @@ -147,7 +154,7 @@ void pass1(string line, bool &NOBASE,
absolute = true;
} else {
string err_msg = "INVALID EQU ARGUMENT.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
}
Expand All @@ -156,13 +163,13 @@ void pass1(string line, bool &NOBASE,
if (SYMBOL_TABLE.find(equ_toks[i + 1]) !=
SYMBOL_TABLE.end()) {
string err_msg = "INVALID EQU ARGUMENT.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
} else if (is_digits(equ_toks[i + 1])) {
value = value - stoi(equ_toks[i + 1]);
} else {
string err_msg = "INVALID EQU ARGUMENT.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
} else {
Expand All @@ -175,7 +182,7 @@ void pass1(string line, bool &NOBASE,
absolute = false;
} else {
string err_msg = "INVALID EQU ARGUMENT.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
}
Expand Down Expand Up @@ -224,7 +231,7 @@ void pass1(string line, bool &NOBASE,
SYMBOL_FLAG[tokens[0]] = false;
} else {
string err_msg = "INVALID EQU ARGUMENT.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
return;
Expand All @@ -241,7 +248,7 @@ void pass1(string line, bool &NOBASE,
LOCCTR = SYMBOL_TABLE[tokens[2]];
} else {
string err_msg = "Invalid ORG argument: " + tokens[2];
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
LOCCTR = SYMBOL_TABLE[tokens[2]];
Expand Down Expand Up @@ -290,7 +297,7 @@ void pass1(string line, bool &NOBASE,
LOCCTR += tokens[2].length() - 3;
} else {
string err_msg = "Invalid byte declaration: " + tokens[2];
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
instruction.new_block = false;
Expand All @@ -299,12 +306,12 @@ void pass1(string line, bool &NOBASE,
} else if (tokens[1][0] == '+') {
if (OPTAB.find(tokens[1].substr(1)) == OPTAB.end()) {
string err_msg = "Invalid opcode: " + tokens[1].substr(1);
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
} else if (OPTAB.find(tokens[1]) == OPTAB.end()) {
string err_msg = "Invalid opcode: " + tokens[1];
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
if (tokens[1][0] == '+') {
Expand Down Expand Up @@ -338,7 +345,7 @@ void pass1(string line, bool &NOBASE,
instruction.data = tmp;
} else {
string err_msg = "Invalid literal declaration: " + tokens[2];
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
} else if (!check_lit_validity(tokens[2], LIT_INTERMEDIATE)) {
Expand Down
25 changes: 18 additions & 7 deletions include/pass2.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ void pass2(vector<Instruction> &INSTRUCTIONS, vector<VariantType> &OBJCODE,
int &LOCCTR, map<string, int> &LITTAB,
map<string, int> &SYMBOL_TABLE, bool &NOBASE,
vector<string> &MRECORDS, int &BLOCK_NUMBER,
map<int, int> &BLOCK_LOCCTR, map<string, bool> &SYMBOL_FLAG) {
map<int, int> &BLOCK_LOCCTR, map<string, bool> &SYMBOL_FLAG,
string File_Name) {
for (auto it : INSTRUCTIONS) {
// Identify the presence of a new block
if (it.new_block) {
Expand All @@ -38,8 +39,8 @@ void pass2(vector<Instruction> &INSTRUCTIONS, vector<VariantType> &OBJCODE,
LOCCTR += 2;
auto REG = get_registers(it.data);
obj.opcode = it.opcode.code;
obj.r1 = stringToRegister(REG.first);
obj.r2 = stringToRegister(REG.second);
obj.r1 = stringToRegister(REG.first, File_Name);
obj.r2 = stringToRegister(REG.second, File_Name);
OBJCODE.push_back(obj);
} else if (it.format == Format::THREE) {
formatThree obj;
Expand Down Expand Up @@ -100,6 +101,11 @@ void pass2(vector<Instruction> &INSTRUCTIONS, vector<VariantType> &OBJCODE,
obj.b = true;
obj.displacement =
SYMBOL_TABLE[it.data] - SYMBOL_TABLE["BASE"];
} else {
string err_msg =
"Displacement can't be reached from PC or BASE.";
save_error_msg(err_msg, File_Name);
exit(0);
}
} else if (LITTAB.find(it.data) != LITTAB.end()) {
if (LITTAB[it.data] - LOCCTR < 2047 and
Expand All @@ -113,11 +119,16 @@ void pass2(vector<Instruction> &INSTRUCTIONS, vector<VariantType> &OBJCODE,
obj.p = false;
obj.b = true;
obj.displacement = LITTAB[it.data] - SYMBOL_TABLE["BASE"];
} else {
string err_msg =
"Displacement can't be reached from PC or BASE.";
save_error_msg(err_msg, File_Name);
exit(0);
}
} else {
string err_msg =
"Displacement can't be reached from PC or BASE.";
save_error_msg(err_msg);
string err_msg = "INVALID OPERAND " + it.data + " at LOCCTR " +
to_string(LOCCTR);
save_error_msg(err_msg, File_Name);
exit(0);
}
OBJCODE.push_back(obj);
Expand Down Expand Up @@ -170,7 +181,7 @@ void pass2(vector<Instruction> &INSTRUCTIONS, vector<VariantType> &OBJCODE,
MRECORDS.push_back(addr);
} else {
string err_msg = "Invalid Symbol or Address out of range.";
save_error_msg(err_msg);
save_error_msg(err_msg, File_Name);
exit(0);
}
OBJCODE.push_back(obj);
Expand Down
Loading

0 comments on commit 5bdb009

Please sign in to comment.