From 690dcfa8d5b89ca914c32fcf8317a3127585ed45 Mon Sep 17 00:00:00 2001 From: Faizal Hasanwala Date: Sun, 1 Apr 2018 03:42:29 +0530 Subject: [PATCH] Untested and incomplete code for USE. --- pass1.cpp | 33 ++++++++++++++++++++----- pass2.cpp | 70 ++++++++++++++++++++++++++++++------------------------ tables.cpp | 6 ++++- 3 files changed, 71 insertions(+), 38 deletions(-) diff --git a/pass1.cpp b/pass1.cpp index 0df7211..b362de5 100644 --- a/pass1.cpp +++ b/pass1.cpp @@ -7,6 +7,7 @@ using namespace std; /*Variable to keep persisted*/ bool error_flag=false; int program_length; +string BLocksNumToName[totalBlocks]; void handle_LTORG(string& litPrefix, int& lineNumberDelta,int lineNumber,int& LOCCTR, int& lastDeltaLOCCTR, int currentBlockNumber){ string litAddress,litValue; @@ -21,6 +22,7 @@ void handle_LTORG(string& litPrefix, int& lineNumberDelta,int lineNumber,int& LO lineNumber += 5; lineNumberDelta += 5; LITTAB[it.first].address = intToStringHex(LOCCTR); + LITTAB[it.first].blockNumber = currentBlockNumber; litPrefix += "\n" + to_string(lineNumber) + "\t" + intToStringHex(LOCCTR) + "\t" + to_string(currentBlockNumber) + "\t" + "*" + "\t" + "="+litValue + "\t" + " " + "\t" + " "; if(litValue[0]=='X'){ @@ -102,6 +104,7 @@ void pass1(){ SYMTAB[label].address = intToStringHex(LOCCTR); SYMTAB[label].relative = 1; SYMTAB[label].exists = 'y'; + SYMTAB[label].blockNumber = currentBlockNumber; } else{ writeData = "Line: "+to_string(lineNumber)+" : Duplicate symbol for '"+label+"'. Previously defined at "+SYMTAB[label].address; @@ -137,6 +140,7 @@ void pass1(){ LITTAB[tempOperand].value = tempOperand; LITTAB[tempOperand].exists = 'y'; LITTAB[tempOperand].address = "?"; + LITTAB[tempOperand].blockNumber = -1; } } } @@ -211,7 +215,7 @@ void pass1(){ BLOCKS[operand].exists = 'y'; BLOCKS[operand].name = operand; BLOCKS[operand].number = totalBlocks++; - BLOCKS[operand].LOCCTR = intToStringHex(LOCCTR); + BLOCKS[operand].LOCCTR = "0"; } currentBlockNumber = BLOCKS[operand].number; LOCCTR = stringHexToInt(BLOCKS[operand].LOCCTR); @@ -312,23 +316,23 @@ void pass1(){ /*relative*/ relative = 1; EvaluateString tempOBJ(valueString); - tempOperand = intToStringHex(tempOBJ.getResult(),6); + tempOperand = intToStringHex(tempOBJ.getResult()); } else if(pairCount==0){ /*absolute*/ relative = 0; EvaluateString tempOBJ(valueString); - tempOperand = intToStringHex(tempOBJ.getResult(),6); + tempOperand = intToStringHex(tempOBJ.getResult()); } else{ writeData = "Line: "+to_string(lineNumber)+" : Illegal expression"; writeToFile(errorFile,writeData); - tempOperand = "000000"; + tempOperand = "00000"; relative = 0; } } else{ - tempOperand = "000000"; + tempOperand = "00000"; relative = 0; } } @@ -336,6 +340,7 @@ void pass1(){ SYMTAB[label].name = label; SYMTAB[label].address = tempOperand; SYMTAB[label].relative = relative; + SYMTAB[label].blockNumber = currentBlockNumber; lastDeltaLOCCTR = LOCCTR - stringHexToInt(tempOperand); } else{ @@ -375,7 +380,23 @@ void pass1(){ 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; + int LocctrArr[totalBlocks]; + for(auto const& it: BLOCKS){ + LocctrArr[it.second.number] = stringHexToInt(it.second.LOCCTR); + BLocksNumToName[it.second.number] = it.first; + } + + for(int i = 1 ;i=(1<<4*halfBytes)){ + if(immediateValue>=(1<<4*halfBytes)){//Can't fit it writeData = "Line: "+to_string(lineNumber)+" Immediate value exceeds format limit"; writeToFile(errorFile,writeData); objcode = intToStringHex(stringHexToInt(OPTAB[getRealOpcode(opcode)].opcode)+1,2); @@ -98,7 +98,7 @@ string createObjectCodeFormat34(){ return objcode; } else{ - int operandAddress = stringHexToInt(SYMTAB[tempOperand].address); + int operandAddress = stringHexToInt(SYMTAB[tempOperand].address) + stringHexToInt(BLOCKS[BLocksNumToName[SYMTAB[tempOperand].blockNumber]].startAddress); /*Process Immediate symbol value*/ if(halfBytes==5){/*If format 4*/ @@ -115,10 +115,11 @@ string createObjectCodeFormat34(){ } /*Handle format 3*/ - program_counter = address; + program_counter = address + stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress); program_counter += (halfBytes==5)?4:3; int relativeAddress = operandAddress - program_counter; + /*Try PC based*/ if(relativeAddress>=(-2048) && relativeAddress<=2047){ objcode = intToStringHex(stringHexToInt(OPTAB[getRealOpcode(opcode)].opcode)+1,2); objcode += '2'; @@ -126,6 +127,7 @@ string createObjectCodeFormat34(){ return objcode; } + /*Try base based*/ if(!nobase){ relativeAddress = operandAddress - base_register_value; if(relativeAddress>=0 && relativeAddress<=4095){ @@ -136,13 +138,14 @@ string createObjectCodeFormat34(){ } } + /*Use direct addressing with no PC or base*/ if(operandAddress<=4095){ objcode = intToStringHex(stringHexToInt(OPTAB[getRealOpcode(opcode)].opcode)+1,2); objcode += '0'; objcode += intToStringHex(operandAddress,halfBytes); /*add modifacation record here*/ - modificationRecord += "M^" + intToStringHex(address+1,6) + '^'; + modificationRecord += "M^" + intToStringHex(address+1+stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress),6) + '^'; modificationRecord += (halfBytes==5)?"05":"03"; modificationRecord += '\n'; @@ -161,8 +164,8 @@ string createObjectCodeFormat34(){ return objcode; } - int operandAddress = stringHexToInt(SYMTAB[tempOperand].address); - program_counter = address; + int operandAddress = stringHexToInt(SYMTAB[tempOperand].address) + stringHexToInt(BLOCKS[BLocksNumToName[SYMTAB[tempOperand].blockNumber]].startAddress); + program_counter = address + stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress); program_counter += (halfBytes==5)?4:3; if(halfBytes==3){ @@ -190,7 +193,7 @@ string createObjectCodeFormat34(){ objcode += intToStringHex(operandAddress,halfBytes); /*add modifacation record here*/ - modificationRecord += "M^" + intToStringHex(address+1,6) + '^'; + modificationRecord += "M^" + intToStringHex(address+1+stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress),6) + '^'; modificationRecord += (halfBytes==5)?"05":"03"; modificationRecord += '\n'; @@ -203,7 +206,7 @@ string createObjectCodeFormat34(){ objcode += intToStringHex(operandAddress,halfBytes); /*add modifacation record here*/ - modificationRecord += "M^" + intToStringHex(address+1,6) + '^'; + modificationRecord += "M^" + intToStringHex(address+1+stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress),6) + '^'; modificationRecord += (halfBytes==5)?"05":"03"; modificationRecord += '\n'; @@ -223,6 +226,10 @@ string createObjectCodeFormat34(){ if(tempOperand=="*"){ tempOperand = "X'" + intToStringHex(address,6) + "'"; + /*Add modification record for value created by operand `*` */ + modificationRecord += "M^" + intToStringHex(stringHexToInt(LITTAB[tempOperand].address)+stringHexToInt(BLOCKS[BLocksNumToName[LITTAB[tempOperand].blockNumber]].startAddress),6) + '^'; + modificationRecord += intToStringHex(6,2); + modificationRecord += '\n'; } if(LITTAB[tempOperand].exists=='n'){ @@ -235,8 +242,8 @@ string createObjectCodeFormat34(){ return objcode; } - int operandAddress = stringHexToInt(LITTAB[tempOperand].address); - program_counter = address; + int operandAddress = stringHexToInt(LITTAB[tempOperand].address) + stringHexToInt(BLOCKS[BLocksNumToName[LITTAB[tempOperand].blockNumber]].startAddress); + program_counter = address + stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress); program_counter += (halfBytes==5)?4:3; if(halfBytes==3){ @@ -264,7 +271,7 @@ string createObjectCodeFormat34(){ objcode += intToStringHex(operandAddress,halfBytes); /*add modifacation record here*/ - modificationRecord += "M^" + intToStringHex(address+1,6) + '^'; + modificationRecord += "M^" + intToStringHex(address+1+stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress),6) + '^'; modificationRecord += (halfBytes==5)?"05":"03"; modificationRecord += '\n'; @@ -277,7 +284,7 @@ string createObjectCodeFormat34(){ objcode += intToStringHex(operandAddress,halfBytes); /*add modifacation record here*/ - modificationRecord += "M^" + intToStringHex(address+1,6) + '^'; + modificationRecord += "M^" + intToStringHex(address+1+stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress),6) + '^'; modificationRecord += (halfBytes==5)?"05":"03"; modificationRecord += '\n'; @@ -311,8 +318,8 @@ string createObjectCodeFormat34(){ return objcode; } - int operandAddress = stringHexToInt(SYMTAB[tempOperand].address); - program_counter = address; + int operandAddress = stringHexToInt(SYMTAB[tempOperand].address) + stringHexToInt(BLOCKS[SYMTAB[tempOperand].BLocksNumToName[blockNumber]].startAddress); + program_counter = address + stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress); program_counter += (halfBytes==5)?4:3; if(halfBytes==3){ @@ -340,7 +347,7 @@ string createObjectCodeFormat34(){ objcode += intToStringHex(operandAddress,halfBytes); /*add modifacation record here*/ - modificationRecord += "M^" + intToStringHex(address+1,6) + '^'; + modificationRecord += "M^" + intToStringHex(address+1+stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress),6) + '^'; modificationRecord += (halfBytes==5)?"05":"03"; modificationRecord += '\n'; @@ -353,7 +360,7 @@ string createObjectCodeFormat34(){ objcode += intToStringHex(operandAddress,halfBytes); /*add modifacation record here*/ - modificationRecord += "M^" + intToStringHex(address+1,6) + '^'; + modificationRecord += "M^" + intToStringHex(address+1+stringHexToInt(BLOCKS[BLocksNumToName[blockNumber]].startAddress),6) + '^'; modificationRecord += (halfBytes==5)?"05":"03"; modificationRecord += '\n'; @@ -401,7 +408,7 @@ void writeTextRecord(bool lastRecord=false){ } else{ /*Assembler directive which doesn't result in address genrenation*/ - if(opcode=="START"||opcode=="END"||opcode=="BASE"||opcode=="NOBASE"||opcode=="LTORG"||opcode=="ORG"){ + if(opcode=="START"||opcode=="END"||opcode=="BASE"||opcode=="NOBASE"||opcode=="LTORG"||opcode=="ORG"||opcode=="EQU"){ /*DO nothing*/ } else{ @@ -456,18 +463,19 @@ void pass2(){ currentTextRecordLength=0; currentRecord = ""; modificationRecord = ""; + blockNumber = 0; nobase = true; - readIntermediateFile(intermediateFile,isComment,lineNumber,address,label,opcode,operand,comment); + readIntermediateFile(intermediateFile,isComment,lineNumber,address,blockNumber,label,opcode,operand,comment); while(isComment){//Handle with previous comments writeData = to_string(lineNumber) + "\t" + comment; writeToFile(ListingFile,writeData); - readIntermediateFile(intermediateFile,isComment,lineNumber,address,label,opcode,operand,comment); + readIntermediateFile(intermediateFile,isComment,lineNumber,address,blockNumber,label,opcode,operand,comment); } if(opcode=="START"){ startAddress = address; - writeData = to_string(lineNumber) + "\t" + intToStringHex(address) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + objectCode +"\t" + comment; + writeData = to_string(lineNumber) + "\t" + intToStringHex(address) + "\t" + to_string(blockNumber) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + objectCode +"\t" + comment; writeToFile(ListingFile,writeData); } else{ @@ -479,7 +487,7 @@ void pass2(){ writeData = "H^"+expandString(label,6,' ',true)+'^'+intToStringHex(address,6)+'^'+intToStringHex(program_length,6); writeToFile(objectFile,writeData); - readIntermediateFile(intermediateFile,isComment,lineNumber,address,label,opcode,operand,comment); + readIntermediateFile(intermediateFile,isComment,lineNumber,address,blockNumber,label,opcode,operand,comment); currentTextRecordLength = 0; while(opcode!="END"){ @@ -555,7 +563,7 @@ void pass2(){ } else if(opcode=="BASE"){ if(SYMTAB[operand].exists=='y'){ - base_register_value = stringHexToInt(SYMTAB[operand].address); + base_register_value = stringHexToInt(SYMTAB[operand].address) + stringHexToInt(BLOCKS[BLocksNumToName[SYMTAB[tempOperand].blockNumber]].startAddress); nobase = false; } else{ @@ -580,23 +588,23 @@ void pass2(){ //Write to text record if any generated writeTextRecord(); - writeData = to_string(lineNumber) + "\t" + intToStringHex(address) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + objectCode +"\t" + comment; + writeData = to_string(lineNumber) + "\t" + intToStringHex(address) + "\t" to_string(blockNumber) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + objectCode +"\t" + comment; }//if not comment else{ writeData = to_string(lineNumber) + "\t" + comment; } writeToFile(ListingFile,writeData);//Write listing line - readIntermediateFile(intermediateFile,isComment,lineNumber,address,label,opcode,operand,comment);//Read next line + readIntermediateFile(intermediateFile,isComment,lineNumber,address,blockNumber,label,opcode,operand,comment);//Read next line objectCode = ""; }//while opcode not end writeTextRecord(); //Save end record writeEndRecord(false); - writeData = to_string(lineNumber) + "\t" + intToStringHex(address) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + "" +"\t" + comment; + writeData = to_string(lineNumber) + "\t" + intToStringHex(address) + "\t" + to_string(blockNumber) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + "" +"\t" + comment; writeToFile(ListingFile,writeData); - while(readIntermediateFile(intermediateFile,isComment,lineNumber,address,label,opcode,operand,comment)){ + while(readIntermediateFile(intermediateFile,isComment,lineNumber,address,blockNumber,label,opcode,operand,comment)){ if(label=="*"){ if(opcode[1]=='C'){ objectCode = stringToHexString(opcode.substr(3,opcode.length()-4)); @@ -606,7 +614,7 @@ void pass2(){ } writeTextRecord(); } - writeData = to_string(lineNumber) + "\t" + intToStringHex(address) + "\t" + label + "\t" + opcode + "\t" + operand + "\t" + objectCode +"\t" + comment; + writeData = to_string(lineNumber) + "\t" + intToStringHex(address) + "\t" + to_string(blockNumber) + label + "\t" + opcode + "\t" + operand + "\t" + objectCode +"\t" + comment; writeToFile(ListingFile,writeData); } writeTextRecord(true); diff --git a/tables.cpp b/tables.cpp index 575b0c0..2468aa5 100644 --- a/tables.cpp +++ b/tables.cpp @@ -19,9 +19,11 @@ struct struct_literal{ string value; string address; char exists; + int blockNumber = 0; struct_literal(){ value=""; address="?"; + blockNumber = 0; exists='n'; } }; @@ -29,10 +31,12 @@ struct struct_label{ string address; string name; int relative; + int blockNumber; char exists; struct_label(){ name="undefined"; address="0"; + blockNumber = 0; exists='n'; relative = 0; } @@ -341,7 +345,7 @@ void load_OPTAB(){ void load_BLOCKS(){ BLOCKS["DEFAULT"].exists = 'y'; BLOCKS["DEFAULT"].name = "DEFAULT"; - BLOCKS["DEFAULT"].startAddress = "?"; + BLOCKS["DEFAULT"].startAddress = "00000"; BLOCKS["DEFAULT"].number=0; BLOCKS["DEFAULT"].LOCCTR = "0"; }