Skip to content

Commit

Permalink
Untested code for USE in pass1 and string evaluate in expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
faizal3199 committed Mar 31, 2018
1 parent e0cabca commit 977698b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 11 deletions.
46 changes: 35 additions & 11 deletions pass1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace std;
bool error_flag=false;
int program_length;

void handle_LTORG(string& litPrefix, int& lineNumberDelta,int lineNumber,int& LOCCTR, int& lastDeltaLOCCTR){
void handle_LTORG(string& litPrefix, int& lineNumberDelta,int lineNumber,int& LOCCTR, int& lastDeltaLOCCTR, int currentBlockNumber){
string litAddress,litValue;
litPrefix = "";
for(auto const& it: LITTAB){
Expand All @@ -21,7 +21,7 @@ void handle_LTORG(string& litPrefix, int& lineNumberDelta,int lineNumber,int& LO
lineNumber += 5;
lineNumberDelta += 5;
LITTAB[it.first].address = intToStringHex(LOCCTR);
litPrefix += "\n" + to_string(lineNumber) + "\t" + intToStringHex(LOCCTR) + "\t" + "*" + "\t" + "="+litValue + "\t" + " " + "\t" + " ";
litPrefix += "\n" + to_string(lineNumber) + "\t" + intToStringHex(LOCCTR) + "\t" + to_string(currentBlockNumber) + "\t" + "*" + "\t" + "="+litValue + "\t" + " " + "\t" + " ";

if(litValue[0]=='X'){
LOCCTR += (litValue.length() -3)/2;
Expand All @@ -48,6 +48,10 @@ void pass1(){
string writeData,writeDataSuffix="",writeDataPrefix="";
int index=0;

string currentBlockName = "DEFAULT";
int currentBlockNumber = 0;
int totalBlocks = 1;

bool statusCode;
string label,opcode,operand,comment;
string tempOperand;
Expand Down Expand Up @@ -76,7 +80,7 @@ void pass1(){
// cout<<startAddress<<endl;
// exit(0);
LOCCTR = startAddress;
writeData = to_string(lineNumber) + "\t" + intToStringHex(LOCCTR-lastDeltaLOCCTR) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + comment;
writeData = to_string(lineNumber) + "\t" + intToStringHex(LOCCTR-lastDeltaLOCCTR) + "\t" + to_string(currentBlockNumber) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + comment;
writeToFile(intermediateFile,writeData); // Write file to intermediate file

getline(sourceFile,fileLine); //Read next line
Expand Down Expand Up @@ -186,7 +190,7 @@ void pass1(){
}
else if(opcode=="LTORG"){
operand = " ";
handle_LTORG(writeDataSuffix,lineNumberDelta,lineNumber,LOCCTR,lastDeltaLOCCTR);
handle_LTORG(writeDataSuffix,lineNumberDelta,lineNumber,LOCCTR,lastDeltaLOCCTR,currentBlockNumber);
}
else if(opcode=="ORG"){
readFirstNonWhiteSpace(fileLine,index,statusCode,operand);
Expand All @@ -199,6 +203,19 @@ void pass1(){
LOCCTR = stringHexToInt(SYMTAB[operand].address);
}
}
else if(opcode=="USE"){
readFirstNonWhiteSpace(fileLine,index,statusCode,operand);
BLOCKS[currentBlockName].LOCCTR = intToStringHex(LOCCTR);

if(BLOCKS[operand].exists=='n'){
BLOCKS[operand].exists = 'y';
BLOCKS[operand].name = operand;
BLOCKS[operand].number = totalBlocks++;
BLOCKS[operand].LOCCTR = intToStringHex(LOCCTR);
}
currentBlockNumber = BLOCKS[operand].number;
LOCCTR = stringHexToInt(BLOCKS[operand].LOCCTR);
}
else if(opcode=="EQU"){
readFirstNonWhiteSpace(fileLine,index,statusCode,operand);
tempOperand = "";
Expand Down Expand Up @@ -257,10 +274,10 @@ void pass1(){
}
else if((singleOperator=="-" || singleOperator=="+" || singleOperator=="?")&&lastOperand==1){
if(singleOperator=="-"){
pairCount += -1;
pairCount--;
}
else{
pairCount += 1;
pairCount++;
}
}

Expand Down Expand Up @@ -294,12 +311,14 @@ void pass1(){
if(pairCount==1){
/*relative*/
relative = 1;
/*tempOperand = eval(valueString)*/
EvaluateString tempOBJ(valueString);
tempOperand = intToStringHex(tempOBJ.getResult(),6);
}
else if(pairCount==0){
/*absolute*/
relative = 0;
/*tempOperand = eval(valueString)*/
EvaluateString tempOBJ(valueString);
tempOperand = intToStringHex(tempOBJ.getResult(),6);
}
else{
writeData = "Line: "+to_string(lineNumber)+" : Illegal expression";
Expand All @@ -326,7 +345,12 @@ void pass1(){
error_flag = true;
}
readFirstNonWhiteSpace(fileLine,index,statusCode,comment,true);
writeData = writeDataPrefix + to_string(lineNumber) + "\t" + intToStringHex(LOCCTR-lastDeltaLOCCTR) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + comment + writeDataSuffix;
if(opcode=="EQU" && SYMTAB[label].relative == 0){
writeData = writeDataPrefix + to_string(lineNumber) + "\t" + intToStringHex(LOCCTR-lastDeltaLOCCTR) + "\t" + " " + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + comment + writeDataSuffix;
}
else{
writeData = writeDataPrefix + to_string(lineNumber) + "\t" + intToStringHex(LOCCTR-lastDeltaLOCCTR) + "\t" + to_string(currentBlockNumber) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + comment + writeDataSuffix;
}
writeDataPrefix = "";
writeDataSuffix = "";
}
Expand All @@ -346,9 +370,9 @@ void pass1(){
readFirstNonWhiteSpace(fileLine,index,statusCode,operand);
readFirstNonWhiteSpace(fileLine,index,statusCode,comment,true);

handle_LTORG(writeDataSuffix,lineNumberDelta,lineNumber,LOCCTR,lastDeltaLOCCTR);
handle_LTORG(writeDataSuffix,lineNumberDelta,lineNumber,LOCCTR,lastDeltaLOCCTR,currentBlockNumber);

writeData = to_string(lineNumber) + "\t" + intToStringHex(LOCCTR-lastDeltaLOCCTR) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + comment + writeDataSuffix;
writeData = to_string(lineNumber) + "\t" + intToStringHex(LOCCTR-lastDeltaLOCCTR) + "\t" + " " + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + comment + writeDataSuffix;
writeToFile(intermediateFile,writeData);

program_length = LOCCTR - startAddress;
Expand Down
24 changes: 24 additions & 0 deletions tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ struct struct_label{
relative = 0;
}
};
struct struct_blocks{
string startAddress;
string name;
string LOCCTR;
int number;
char exists;
struct_blocks(){
name="undefined";
startAddress="?";
exists='n';
number = -1;
LOCCTR = "0";
}
};
struct struct_register{
char num;
char exists;
Expand All @@ -50,11 +64,13 @@ typedef map<string,struct_label> SYMBOL_TABLE_TYPE;
typedef map<string,struct_opcode> OPCODE_TABLE_TYPE;
typedef map<string,struct_register> REG_TABLE_TYPE;
typedef map<string,struct_literal> LIT_TABLE_TYPE;
typedef map<string,struct_blocks> BLOCK_TABLE_TYPE;

SYMBOL_TABLE_TYPE SYMTAB;
OPCODE_TABLE_TYPE OPTAB;
REG_TABLE_TYPE REGTAB;
LIT_TABLE_TYPE LITTAB;
BLOCK_TABLE_TYPE BLOCKS;

void load_REGTAB(){
REGTAB["A"].num='0';
Expand Down Expand Up @@ -322,7 +338,15 @@ void load_OPTAB(){
OPTAB["WD"].exists='y';
}

void load_BLOCKS(){
BLOCKS["DEFAULT"].exists = 'y';
BLOCKS["DEFAULT"].name = "DEFAULT";
BLOCKS["DEFAULT"].startAddress = "?";
BLOCKS["DEFAULT"].number=0;
BLOCKS["DEFAULT"].LOCCTR = "0";
}
void load_tables(){
load_BLOCKS();
load_OPTAB();
load_REGTAB();
}
77 changes: 77 additions & 0 deletions utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,80 @@ char getFlagFormat(string data){
}
return ' ';
}

class EvaluateString{
public:
int getResult();
EvaluateString(string data);
private:
string storedData;
int index;
char peek();
char get();
int term();
int factor();
int number();
};

EvaluateString::EvaluateString(string data){
storedData = data;
index=0;
}

int EvaluateString::getResult(){
int result = term();
while(peek()=='+' || peek() == '-'){
if(get() == '+'){
result += term();
}else{
result -= term();
}
}
return result;
}

int EvaluateString::term(){
int result = factor();
while(peek() == '*' || peek() == '/'){
if(get()=='*'){
result *= factor();
}
else{
result /= factor();
}
}
return result;
}

int EvaluateString::factor(){
if(peek() >= '0' && peek() <= '9'){
return number();
}
else if(peek() == '('){
get();
int result = getResult();
get();
return result;
}
else if(peek()=='-'){
get();
return -factor();
}
return 0;
}

int EvaluateString::number(){
int result = get() - '0';
while(peek() >= '0' && peek() <= '9'){
result = 10*result + get()-'0';
}
return result;
}

char EvaluateString::get(){
return storedData[index++];
}

char EvaluateString::peek(){
return storedData[index];
}

0 comments on commit 977698b

Please sign in to comment.