J:\Assembler\Assembler\[Link] using using using using using using using System; [Link]; [Link]; [Link]; [Link]; [Link]; System.
Collections;
namespace Assembler { class Program { static string[] memoryLabels = new String[20]; static string[] jumpLabels = new String[20]; static int memoryLabelcount = 0;
static void Main(string[] args) { #region Files StreamReader myReader = new StreamReader("[Link]"); StreamWriter mytemp = new StreamWriter("[Link]"); StreamWriter myWriter = new StreamWriter("[Link]"); StreamWriter myFWriter = new StreamWriter("[Link]"); StreamWriter myWriter1 = new StreamWriter("[Link]"); #endregion #region someVariables int locationCounter = 0; int count=0; int jumpLabelCount = 0; // int memoryLabelcount = 0; int lineNumber = 0; string line = ""; //string line1 = ""; //string charread = ""; string charwrite = ""; string tobeWritten = ""; string binwrite=""; string label; //string searchTerm = ""; bool labelfound; bool programHalted = false; string [] words = new String[5]; string[] symbolictable = new String[20]; // string[] jumpLabels = new String[100]; //string[] memoryLabels = new String[20]; bool flag = false; bool programEnded = false; Hashtable symbolshashtable = new Hashtable(); Hashtable memoryLocationhashtable = new Hashtable();
J:\Assembler\Assembler\[Link]
#endregion //[Link]("Area", 1000); //[Link]("Perimeter", 55); //[Link]("Mortgage", 540);
[Link]("Processing assembly file.............................."); // locationCounter = 0; while (line != null ) {
//char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; char[] delimiterChars = { ' ' }; line = [Link](); //words = [Link](delimiterChars); if (line != null ) { //[Link](); words = [Link](delimiterChars); char[] charArray = [Link]();
label = words[0]; labelfound = foundlabel(words[0]);
if (labelfound) { char[] delimiterChars2 = { ',' }; string[] labelplus = [Link](delimiterChars2); symbolictable[count] = labelplus[0]; count++; [Link](labelplus[0], [Link]()); } else { if (words[0].Length >= 4) { [Link]("you got a bad label");
J:\Assembler\Assembler\[Link] [Link](); [Link](0); }
//char[] labelchar = [Link](); //if ([Link] == 4) //{ // // // // // [Link]("Found a Label"); string string string string firstchar = labelchar[0].ToString(); sndchar = labelchar[1].ToString(); thirdchar = labelchar[2].ToString(); fourthchar = labelchar[3].ToString();
//if (IsAlpha(firstchar) && IsAlphaNumeric(sndchar) && IsAlphaNumeric (thirdchar) && fourthchar == ",") //{
// //
[Link]("Valid Label"); labelfound = true;
//} //else //{ // //} labelfound = false;
//}
//[Link](charArray); //charread =(string) charArray; //[Link](charArray); string buffer = line; //string buffer2 = [Link](0,3); //string buffer3 = [Link](0, 4);
//if (buffer2 == "ORG") //{ // [Link]("Found ORG line"); //} //else //{ // [Link]("Buffer has {0} ", buffer2);
J:\Assembler\Assembler\[Link] //}
switch (labelfound?words[1]:words[0]) { case "ORG": [Link](charArray); locationCounter = [Link](words[1]); //charwrite = [Link](); flag = true; break;
///Memory Reference Instructions /// #region AND case "AND": if (labelfound) { if ([Link]==4&&words[3] == "I") { charwrite = "8" + words[2]; string searchTerm = words[1]; foreach (string x in [Link]) { try { if () {
[Link][memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } else { charwrite = "0" + words[2];
string searchTerm = words[1]; foreach (string x in [Link]) { try { if () {
[Link][memoryLabelcount] = words[1]; memoryLabelcount++; } }
J:\Assembler\Assembler\[Link] catch { } } } } else { if ([Link]==3&&words[2] == "I") { charwrite = "8" + words[1];
string searchTerm = words[1]; foreach (string x in [Link]) { try { if () { [Link][memoryLabelcount] = words[1]; memoryLabelcount++;
} } catch {
} } } else { charwrite = "0" + words[1]; string searchTerm = words[1]; foreach (string x in [Link]) { try { if () { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } }
J:\Assembler\Assembler\[Link] if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break; #endregion #region ADD case "ADD": if (labelfound) { if ([Link]==4&&words[3] == "I") { charwrite = "9" + words[2];
if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } else { string searchTerm = words[2]; foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } } catch { } } } } else { charwrite = "1" + words[2];
if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } else { string searchTerm = words[2];
J:\Assembler\Assembler\[Link] foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } } catch { } } } } } else { if ([Link]==3&&words[2] == "I") { charwrite = "9" + words[1];
if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } else {
string searchTerm = words[1]; foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } else { charwrite = "1" + words[1];
J:\Assembler\Assembler\[Link]
8 if (memoryLabelcount == 0) { [Link][memoryLabelcount] =
words[1]; memoryLabelcount++; } else {
string searchTerm = words[1]; foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } } catch {
} } } } } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break; #endregion #region LDA case "LDA": ///checking if there is a label if (labelfound) { if ([Link]==4&&words[3] == "I") { charwrite = "A" + words[2];
if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } else {
J:\Assembler\Assembler\[Link]
string searchTerm = words[2];
foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } } catch { } } } } ///checking if there no label else { charwrite = "2" + words[2]; if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } else {
string searchTerm = words[2]; foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } } catch { }
} } }
J:\Assembler\Assembler\[Link] } else { //if([Link]==3) //{ if ([Link]==3&&words[2] == "I") { words[2] = "I"; charwrite = "A" + words[1]; if (memoryLabelcount == 0) { [Link] [memoryLabelcount] = words[1]; memoryLabelcount++; }
10
else {
string searchTerm = words[1]; foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } else { charwrite = "2" + words[1];
if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } else {
string searchTerm = words[1];
foreach (string x in Program. memoryLabels)
J:\Assembler\Assembler\[Link] { try { if () { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break;
11
#endregion #region STA case "STA": if (labelfound) { if ([Link]==4&&words[3] == "I") { charwrite = "B" + words[2];
if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } else { string searchTerm = words[2]; foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } }
J:\Assembler\Assembler\[Link] catch { } } } } else { charwrite = "3" + words[2];
12
if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } else {
string searchTerm = words[2]; foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[2]; memoryLabelcount++; } } catch {
} } } } } else { if ([Link]==3&&words[2] == "I") { charwrite = "B" + words[1]; if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } else {
string searchTerm = words[1]; foreach (string x in Program. memoryLabels)
J:\Assembler\Assembler\[Link] { try { if () { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } else { charwrite = "3" + words[1];
13
if (memoryLabelcount == 0) { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } else {
string searchTerm = words[1]; foreach (string x in Program. memoryLabels) { try { if () { [Link][memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; }
J:\Assembler\Assembler\[Link] break; #endregion #region BUN case "BUN": if (labelfound) { if ([Link]==4&&words[3] == "I") { charwrite = "C" + words[2];
14
string searchTerm = words[2];
foreach (string x in [Link]) { if () { // Process... [Link][jumpLabelCount] = words[2]; jumpLabelCount++; } }
//if() //{
//} // int? firstIndex = GetFirstIndex(jumpLabels , searchTerm);
} else { charwrite = "4" + words[2]; string searchTerm = words[2]; foreach (string x in [Link]) { try { if () { [Link][jumpLabelCount] = words[2]; jumpLabelCount++; } } catch { } } //if () //{
//} }
J:\Assembler\Assembler\[Link] } else { if ([Link]==3&&words[2] == "I") { charwrite = "C" + words[1]; // string searchTerm = words[2]; //string x = ""; if (jumpLabelCount == 0) { [Link][jumpLabelCount] = words[1]; jumpLabelCount++; } else {
15
foreach (string x in [Link]) { string searchTerm = words[1]; if () { [Link][jumpLabelCount] = words[1]; jumpLabelCount++; } } }
//if () //{
//} } else { charwrite = "4" + words[1]; string searchTerm = words[1]; foreach (string x in [Link]) { try { if () { [Link][jumpLabelCount] = words[1]; jumpLabelCount++; } }catch{ } } //if () //{
//} }
J:\Assembler\Assembler\[Link] } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break; #endregion #region BSA case "BSA": if (labelfound) { if ([Link]==4&&words[3] == "I") { charwrite = "D" + words[2]; string searchTerm = words[2];
16
foreach (string x in [Link]) { if () { [Link][jumpLabelCount] = words[2]; jumpLabelCount++; } }
//if () //{ // jumpLabels[jumpLabelCount] = words[2] ; // //} } else { charwrite = "5" + words[2]; string searchTerm = words[2]; foreach (string x in [Link]) { if () { [Link][jumpLabelCount] = words[2]; jumpLabelCount++; } } //if () //{ // jumpLabels[jumpLabelCount] = words[2] ; // //} jumpLabelCount++; jumpLabelCount++;
J:\Assembler\Assembler\[Link] } } else { if ([Link]==3&&words[2] == "I") { charwrite = "D" + words[1];
17
foreach (string x in [Link]) { string searchTerm = words[1]; try { if () { [Link][jumpLabelCount] = words[1]; jumpLabelCount++; } } catch { //do nothing } } //if () //{ // jumpLabels[jumpLabelCount] = words[1] ; // //} } else { charwrite = "5" + words[1]; string searchTerm = words[1]; foreach (string x in [Link]) { try { if () { [Link][jumpLabelCount] = words[1]; jumpLabelCount++; } } catch { } } //if () //{ // ; // jumpLabelCount++; jumpLabels[jumpLabelCount] = words[1] jumpLabelCount++;
J:\Assembler\Assembler\[Link] //} } } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break; #endregion #region ISZ case "ISZ": if (labelfound) { if ([Link]==4&&words[3] == "I") { charwrite = "E" + words[2]; string searchTerm = words[2];
18
foreach (string x in [Link]) { if () { [Link][jumpLabelCount] = words[2]; jumpLabelCount++; } } //if () //{ // jumpLabels[jumpLabelCount] = words[2] ; // //} } else { charwrite = "6" + words[2]; string searchTerm = words[2]; foreach (string x in [Link]) { if () { [Link][jumpLabelCount] = words[2]; jumpLabelCount++; } } //if () //{ // jumpLabels[jumpLabelCount] = words[2] ; // //} } } else jumpLabelCount++; jumpLabelCount++;
J:\Assembler\Assembler\[Link] { if ([Link]==3&&words[2] == "I") { charwrite = "E" + words[1]; string searchTerm = words[1]; foreach (string x in [Link]) { try { if () { [Link][jumpLabelCount] = words[1]; jumpLabelCount++; } } catch { //do nothing } }
19
//if () //{ // jumpLabels[jumpLabelCount] = words[1] ; // //} } else { charwrite = "6" + words[1]; string searchTerm = words[1]; foreach (string x in [Link]) { try { if () { [Link][jumpLabelCount] = words[1]; jumpLabelCount++; } } catch { } } //if () //{ // jumpLabels[jumpLabelCount] = words[1] ; // //} } jumpLabelCount++; jumpLabelCount++;
} if (flag) { flag = false; } else { locationCounter = locationCounter + 1;
J:\Assembler\Assembler\[Link] } break; #endregion ///Non Memory Reference Instructions case "CLA" : charwrite = "7800"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "CLE": charwrite = "7400"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "CMA": charwrite = "7200"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "CME": charwrite = "7100"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "CIR": charwrite = "7080"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "CIL": charwrite = "7040";
20
J:\Assembler\Assembler\[Link] if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break;
21
case "INC": charwrite = "7020"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "SPA": charwrite = "7010"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "SNA": charwrite = "7008"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "SZA": charwrite = "7004"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "SZE": charwrite = "7002"; if (flag) { flag = false; } else
J:\Assembler\Assembler\[Link] { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "HLT": charwrite = "7001"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; programHalted = true; } break; case "INP": charwrite = "F800"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "OUT": charwrite = "F400"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "SKI": charwrite = "F200"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "SKO": charwrite = "F100"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break;
22
J:\Assembler\Assembler\[Link] case "ION": charwrite = "F080"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "IOF": charwrite = "F040"; if (flag) { flag = false; } else { locationCounter = locationCounter + 1; lineNumber = lineNumber + 1; } break; case "END": //program ended; programEnded = true; [Link]("Program ended thank you!"); break; default: if (programHalted) { [Link](words[0], [Link]()); locationCounter = locationCounter + 1; } else {
23
[Link]("Bad Command at line number{0} ",lineNumber); } break;
if (!flag) { if (programEnded) { tobeWritten = ""; } else { tobeWritten = [Link]() + " " + charwrite;
//here we write the temp file [Link](tobeWritten);
J:\Assembler\Assembler\[Link] ///here we write the hex file [Link](tobeWritten); [Link](); //[Link](); ///here we convert the hex value to binary for the binary file
24
//binwrite = [Link](Convert.ToInt32(charwrite, 16), 2); binwrite = ""; } } [Link](binwrite); } }
[Link](); [Link](); [Link](); //StreamReader myReader1 = new StreamReader("[Link]");
//while (line1 != null) //{ // // // // // // // line1 = [Link](); if (line1 != null) { char[] charArray = [Link](); //[Link](charArray); //charread =(string) charArray; charwrite = hex2binary(line1);
// // //} }
[Link](charwrite);
[Link]("...............Finished"); [Link](); //for (int i = 0; i < count; i++) //{ // [Link]("Looping through the Symbols Hashtable "); // // //} int value2 = (int)symbolshashtable["Area"]; [Link](value2);
for (int i = 0; i < count; i++) { [Link](" Label{0} ={1}", i, symbolictable[i]); locationCounter=locationCounter+1;
J:\Assembler\Assembler\[Link] [Link]([Link]() + " " + symbolictable[i]. ToString()); } for (int i = 0; i < memoryLabelcount; i++) { [Link]("Memory Label{0}={1}", i, [Link][i]); [Link]("Memory Hashtable Contents {0}-->", memoryLocationhashtable[[Link][i]]); }
25
for (int i = 0; i < jumpLabelCount; i++) { [Link]("Looping through the Symbols Hashtable "); [Link]("Jump Labels{0}={1}", i, [Link][i]); string search = [Link][i]; int value2 = Convert.ToInt32(symbolshashtable[search]); [Link]("Line of Symbol label found {0}",value2); locationCounter = locationCounter + 1; [Link]([Link]() + " " + [Link][i]) ; }
[Link]();
[Link](); [Link]("Now Assembling and Processing temp file");
/////Processing the temp file //while (line != null) //{
//char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; //char[] delimiterChars = { ' ' };
string[] lines = [Link]("[Link]"); // Display the file contents by using a foreach loop. [Link]("Contents of [Link] = "); foreach (string linet in lines) { // Use a tab to indent each line of the file.
string tempLine = linet; for (int i = 0; i < jumpLabelCount; i++)
J:\Assembler\Assembler\[Link] { //[Link]("Looping through the Symbols Hashtable "); //[Link]("Jump Labels{0}={1}", i, [Link] [i]); string search = [Link][i]; int value2 = Convert.ToInt32(symbolshashtable[search]);
26
tempLine = [Link](search, symbolshashtable[search].ToString ()); }
for (int i = 0; i < memoryLabelcount; i++) { //[Link]("Looping through the Symbols Hashtable "); //[Link]("Jump Labels{0}={1}", i, [Link] [i]); string search = [Link][i]; int value2 = Convert.ToInt32(memoryLocationhashtable[search]); tempLine = [Link](search, memoryLocationhashtable [search].ToString()); }
[Link]("\t" + linet);
[Link](tempLine);
} [Link](); [Link](); // line = [Link]("koko"); //words = [Link](delimiterChars); // // // // // if (line != null) { //[Link](); words = [Link](delimiterChars); char[] charArray = [Link]();
// // // //}
label = words[0]; labelfound = foundlabel(words[0]); }
J:\Assembler\Assembler\[Link] ////Processing Ended here
27
//[Link]();
static string hex2binary(string hexvalue) { string binaryval = ""; [Link](); binaryval = [Link](Convert.ToInt32(hexvalue, 16), 2); return binaryval; }
// Function to test for Positive Integers. public bool IsNaturalNumber(String strNumber) { Regex objNotNaturalPattern=new Regex("[^0-9]"); Regex objNaturalPattern=new Regex("0*[1-9][0-9]*"); return  && [Link](strNumber); } // Function to test for Positive Integers with zero inclusive public bool IsWholeNumber(String strNumber) { Regex objNotWholePattern=new Regex("[^0-9]"); return ; } // Function to Test for Integers both Positive & Negative public bool IsInteger(String strNumber) { Regex objNotIntPattern=new Regex("[^0-9-]"); Regex objIntPattern=new Regex("^-[0-9]+$|^[0-9]+$"); return  && [Link] (strNumber); } // Function to Test for Positive Number both Integer & Real public bool IsPositiveNumber(String strNumber) { Regex objNotPositivePattern=new Regex("[^0-9.]"); Regex objPositivePattern=new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$"); Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*"); return  && [Link](strNumber) && ; } // Function to test whether the string is valid number or not public bool IsNumber(String strNumber) { Regex objNotNumberPattern=new Regex("[^0-9.-]"); Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*"); Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*"); String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; String strValidIntegerPattern="^([-]|[0-9])[0-9]*$"; Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")"); return  &&  &&  &&
J:\Assembler\Assembler\[Link] [Link](strNumber); } // Function To test for Alphabets. public static bool IsAlpha(String strToCheck) { Regex objAlphaPattern=new Regex("[^a-zA-Z]"); return ; } // Function to Check for AlphaNumeric. public static bool IsAlphaNumeric(String strToCheck) { Regex objAlphaNumericPattern=new Regex("[^a-zA-Z0-9]"); return ; }
28
public static bool foundlabel(String str) { //string label; char[] labelchar = [Link](); if ([Link] == 4) { [Link]("Found a Label");
string string string string
firstchar = labelchar[0].ToString(); sndchar = labelchar[1].ToString(); thirdchar = labelchar[2].ToString(); fourthchar = labelchar[3].ToString();
if (IsAlpha(firstchar) && IsAlphaNumeric(sndchar) && IsAlphaNumeric (thirdchar) && fourthchar == ",") {
[Link]("Valid Label"); return true; } else { return false; } } else return false; }
} }