Skip to content

Commit

Permalink
Untested and incomplete code for USE.
Browse files Browse the repository at this point in the history
  • Loading branch information
faizal3199 committed Mar 31, 2018
1 parent 977698b commit 690dcfa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 38 deletions.
33 changes: 27 additions & 6 deletions pass1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'){
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -137,6 +140,7 @@ void pass1(){
LITTAB[tempOperand].value = tempOperand;
LITTAB[tempOperand].exists = 'y';
LITTAB[tempOperand].address = "?";
LITTAB[tempOperand].blockNumber = -1;
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -312,30 +316,31 @@ 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;
}
}

SYMTAB[label].name = label;
SYMTAB[label].address = tempOperand;
SYMTAB[label].relative = relative;
SYMTAB[label].blockNumber = currentBlockNumber;
lastDeltaLOCCTR = LOCCTR - stringHexToInt(tempOperand);
}
else{
Expand Down Expand Up @@ -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<totalBlocks;i++){
LocctrArr[i] += LocctrArr[i-1];
}

for(auto const& it: BLOCKS){
if(it.second.startAddress=="?"){
it.second.startAddress = intToStringHex(LocctrArr[it.second.number - 1]);
}
}

program_length = LocctrArr[totalBlocks - 1] - startAddress;

sourceFile.close();
intermediateFile.close();
Expand Down
70 changes: 39 additions & 31 deletions pass2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bool isComment;
string label,opcode,operand,comment;
string operand1,operand2;

int lineNumber,address,startAddress;
int lineNumber,blockNumber,address,startAddress;
string objectCode, writeData, currentRecord, modificationRecord, endRecord;

int program_counter, base_register_value, currentTextRecordLength;
Expand All @@ -29,7 +29,7 @@ string readTillTab(string data,int& index){
index++;
return tempBuffer;
}
bool readIntermediateFile(ifstream& readFile,bool& isComment, int& lineNumber, int& address, string& label, string& opcode, string& operand, string& comment){
bool readIntermediateFile(ifstream& readFile,bool& isComment, int& lineNumber, int& address, int& blockNumber, string& label, string& opcode, string& operand, string& comment){
string fileLine="";
string tempBuffer="";
bool tempStatus;
Expand All @@ -44,8 +44,8 @@ bool readIntermediateFile(ifstream& readFile,bool& isComment, int& lineNumber, i
readFirstNonWhiteSpace(fileLine,index,tempStatus,comment,true);
return true;
}

address = stringHexToInt(readTillTab(fileLine,index));
blockNumber = stoi(readTillTab(fileLine,index));
label = readTillTab(fileLine,index);
opcode = readTillTab(fileLine,index);
if(opcode=="BYTE"){
Expand Down Expand Up @@ -73,10 +73,10 @@ string createObjectCodeFormat34(){
}

string tempOperand = operand.substr(1,operand.length()-1);
if(if_all_num(tempOperand)){
if(if_all_num(tempOperand)){//Imidiate integer value
int immediateValue = stoi(tempOperand);
/*Process Immediate value*/
if(immediateValue>=(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);
Expand All @@ -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*/
Expand All @@ -115,17 +115,19 @@ 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';
objcode += intToStringHex(relativeAddress,halfBytes);
return objcode;
}

/*Try base based*/
if(!nobase){
relativeAddress = operandAddress - base_register_value;
if(relativeAddress>=0 && relativeAddress<=4095){
Expand All @@ -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';

Expand All @@ -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){
Expand Down Expand Up @@ -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';

Expand All @@ -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';

Expand All @@ -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'){
Expand All @@ -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){
Expand Down Expand Up @@ -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';

Expand All @@ -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';

Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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';

Expand All @@ -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';

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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"){
Expand Down Expand Up @@ -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{
Expand All @@ -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));
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 690dcfa

Please sign in to comment.