Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
77 views28 pages

Assembler

This document contains the code for an assembler program. It defines variables and data structures to store labels, locations, and other assembly code metadata. It includes a main method that reads an assembly file, parses the lines, determines the operation code, and writes the corresponding machine code or other output files. Switch statements process common instructions like AND, ADD, LDA, STA, and BUN by looking up or adding labels, and generating the coded opcode.

Uploaded by

AmroNasr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views28 pages

Assembler

This document contains the code for an assembler program. It defines variables and data structures to store labels, locations, and other assembly code metadata. It includes a main method that reads an assembly file, parses the lines, determines the operation code, and writes the corresponding machine code or other output files. Switch statements process common instructions like AND, ADD, LDA, STA, and BUN by looking up or adding labels, and generating the coded opcode.

Uploaded by

AmroNasr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

J:\Assembler\Assembler\Program.cs using using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.IO; System.Text.RegularExpressions; 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("asm.txt"); StreamWriter mytemp = new StreamWriter("temp.txt"); StreamWriter myWriter = new StreamWriter("hex.txt"); StreamWriter myFWriter = new StreamWriter("fhex.txt"); StreamWriter myWriter1 = new StreamWriter("bin.txt"); #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\Program.cs

#endregion //hashtable.Add("Area", 1000); //hashtable.Add("Perimeter", 55); //hashtable.Add("Mortgage", 540);

Console.WriteLine("Processing assembly file.............................."); // locationCounter = 0; while (line != null ) {

//char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; char[] delimiterChars = { ' ' }; line = myReader.ReadLine(); //words = line.Split(delimiterChars); if (line != null ) { //line.Trim(); words = line.Split(delimiterChars); char[] charArray = line.ToCharArray();

label = words[0]; labelfound = foundlabel(words[0]);

if (labelfound) { char[] delimiterChars2 = { ',' }; string[] labelplus = label.Split(delimiterChars2); symbolictable[count] = labelplus[0]; count++; symbolshashtable.Add(labelplus[0], locationCounter.ToString()); } else { if (words[0].Length >= 4) { Console.WriteLine("you got a bad label");

J:\Assembler\Assembler\Program.cs Console.ReadLine(); Environment.Exit(0); }

//char[] labelchar = label.ToCharArray(); //if (labelchar.Length == 4) //{ // // // // // Console.WriteLine("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 == ",") //{

// //

Console.WriteLine("Valid Label"); labelfound = true;

//} //else //{ // //} labelfound = false;

//}

//Array.Reverse(charArray); //charread =(string) charArray; //Console.WriteLine(charArray); string buffer = line; //string buffer2 = buffer.Substring(0,3); //string buffer3 = buffer.Substring(0, 4);

//if (buffer2 == "ORG") //{ // Console.WriteLine("Found ORG line"); //} //else //{ // Console.WriteLine("Buffer has {0} ", buffer2);

J:\Assembler\Assembler\Program.cs //}

switch (labelfound?words[1]:words[0]) { case "ORG": Console.WriteLine(charArray); locationCounter = Int16.Parse(words[1]); //charwrite = locationCounter.ToString(); flag = true; break;

///Memory Reference Instructions /// #region AND case "AND": if (labelfound) { if (words.Length==4&&words[3] == "I") { charwrite = "8" + words[2]; string searchTerm = words[1]; foreach (string x in Program.memoryLabels) { try { if (!x.Contains(searchTerm)) {

Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } else { charwrite = "0" + words[2];

string searchTerm = words[1]; foreach (string x in Program.memoryLabels) { try { if (!x.Contains(searchTerm)) {

Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } }

J:\Assembler\Assembler\Program.cs catch { } } } } else { if (words.Length==3&&words[2] == "I") { charwrite = "8" + words[1];

string searchTerm = words[1]; foreach (string x in Program.memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++;

} } catch {

} } } else { charwrite = "0" + words[1]; string searchTerm = words[1]; foreach (string x in Program.memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } }

J:\Assembler\Assembler\Program.cs if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break; #endregion #region ADD case "ADD": if (labelfound) { if (words.Length==4&&words[3] == "I") { charwrite = "9" + words[2];

if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } else { string searchTerm = words[2]; foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } } catch { } } } } else { charwrite = "1" + words[2];

if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } else { string searchTerm = words[2];

J:\Assembler\Assembler\Program.cs foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } } catch { } } } } } else { if (words.Length==3&&words[2] == "I") { charwrite = "9" + words[1];

if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } else {

string searchTerm = words[1]; foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } else { charwrite = "1" + words[1];

J:\Assembler\Assembler\Program.cs

8 if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] =

words[1]; memoryLabelcount++; } else {

string searchTerm = words[1]; foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[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 (words.Length==4&&words[3] == "I") { charwrite = "A" + words[2];

if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } else {

J:\Assembler\Assembler\Program.cs

string searchTerm = words[2];

foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } } catch { } } } } ///checking if there no label else { charwrite = "2" + words[2]; if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } else {

string searchTerm = words[2]; foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } } catch { }

} } }

J:\Assembler\Assembler\Program.cs } else { //if(words.Length==3) //{ if (words.Length==3&&words[2] == "I") { words[2] = "I"; charwrite = "A" + words[1]; if (memoryLabelcount == 0) { Program.memoryLabels [memoryLabelcount] = words[1]; memoryLabelcount++; }

10

else {

string searchTerm = words[1]; foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } else { charwrite = "2" + words[1];

if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } else {

string searchTerm = words[1];

foreach (string x in Program. memoryLabels)

J:\Assembler\Assembler\Program.cs { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break;

11

#endregion #region STA case "STA": if (labelfound) { if (words.Length==4&&words[3] == "I") { charwrite = "B" + words[2];

if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } else { string searchTerm = words[2]; foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } }

J:\Assembler\Assembler\Program.cs catch { } } } } else { charwrite = "3" + words[2];

12

if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } else {

string searchTerm = words[2]; foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[2]; memoryLabelcount++; } } catch {

} } } } } else { if (words.Length==3&&words[2] == "I") { charwrite = "B" + words[1]; if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } else {

string searchTerm = words[1]; foreach (string x in Program. memoryLabels)

J:\Assembler\Assembler\Program.cs { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } else { charwrite = "3" + words[1];

13

if (memoryLabelcount == 0) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } else {

string searchTerm = words[1]; foreach (string x in Program. memoryLabels) { try { if (!x.Contains(searchTerm)) { Program.memoryLabels[memoryLabelcount] = words[1]; memoryLabelcount++; } } catch { } } } } } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; }

J:\Assembler\Assembler\Program.cs break; #endregion #region BUN case "BUN": if (labelfound) { if (words.Length==4&&words[3] == "I") { charwrite = "C" + words[2];

14

string searchTerm = words[2];

foreach (string x in Program.jumpLabels) { if (!x.Contains(searchTerm)) { // Process... Program.jumpLabels[jumpLabelCount] = words[2]; jumpLabelCount++; } }

//if(!jumpLabels.Contains(searchTerm)) //{

//} // int? firstIndex = GetFirstIndex(jumpLabels , searchTerm);

} else { charwrite = "4" + words[2]; string searchTerm = words[2]; foreach (string x in Program.jumpLabels) { try { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[2]; jumpLabelCount++; } } catch { } } //if (!jumpLabels.Contains(searchTerm)) //{

//} }

J:\Assembler\Assembler\Program.cs } else { if (words.Length==3&&words[2] == "I") { charwrite = "C" + words[1]; // string searchTerm = words[2]; //string x = ""; if (jumpLabelCount == 0) { Program.jumpLabels[jumpLabelCount] = words[1]; jumpLabelCount++; } else {

15

foreach (string x in Program.jumpLabels) { string searchTerm = words[1]; if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[1]; jumpLabelCount++; } } }

//if (!jumpLabels.Contains(searchTerm)) //{

//} } else { charwrite = "4" + words[1]; string searchTerm = words[1]; foreach (string x in Program.jumpLabels) { try { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[1]; jumpLabelCount++; } }catch{ } } //if (!jumpLabels.Contains(searchTerm)) //{

//} }

J:\Assembler\Assembler\Program.cs } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break; #endregion #region BSA case "BSA": if (labelfound) { if (words.Length==4&&words[3] == "I") { charwrite = "D" + words[2]; string searchTerm = words[2];

16

foreach (string x in Program.jumpLabels) { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[2]; jumpLabelCount++; } }

//if (!jumpLabels.Contains(searchTerm)) //{ // jumpLabels[jumpLabelCount] = words[2] ; // //} } else { charwrite = "5" + words[2]; string searchTerm = words[2]; foreach (string x in Program.jumpLabels) { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[2]; jumpLabelCount++; } } //if (!jumpLabels.Contains(searchTerm)) //{ // jumpLabels[jumpLabelCount] = words[2] ; // //} jumpLabelCount++; jumpLabelCount++;

J:\Assembler\Assembler\Program.cs } } else { if (words.Length==3&&words[2] == "I") { charwrite = "D" + words[1];

17

foreach (string x in Program.jumpLabels) { string searchTerm = words[1]; try { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[1]; jumpLabelCount++; } } catch { //do nothing } } //if (!jumpLabels.Contains(searchTerm)) //{ // jumpLabels[jumpLabelCount] = words[1] ; // //} } else { charwrite = "5" + words[1]; string searchTerm = words[1]; foreach (string x in Program.jumpLabels) { try { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[1]; jumpLabelCount++; } } catch { } } //if (!jumpLabels.Contains(searchTerm)) //{ // ; // jumpLabelCount++; jumpLabels[jumpLabelCount] = words[1] jumpLabelCount++;

J:\Assembler\Assembler\Program.cs //} } } if (flag) { flag = false; } else { locationCounter = locationCounter + 1; } break; #endregion #region ISZ case "ISZ": if (labelfound) { if (words.Length==4&&words[3] == "I") { charwrite = "E" + words[2]; string searchTerm = words[2];

18

foreach (string x in Program.jumpLabels) { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[2]; jumpLabelCount++; } } //if (!jumpLabels.Contains(searchTerm)) //{ // jumpLabels[jumpLabelCount] = words[2] ; // //} } else { charwrite = "6" + words[2]; string searchTerm = words[2]; foreach (string x in Program.jumpLabels) { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[2]; jumpLabelCount++; } } //if (!jumpLabels.Contains(searchTerm)) //{ // jumpLabels[jumpLabelCount] = words[2] ; // //} } } else jumpLabelCount++; jumpLabelCount++;

J:\Assembler\Assembler\Program.cs { if (words.Length==3&&words[2] == "I") { charwrite = "E" + words[1]; string searchTerm = words[1]; foreach (string x in Program.jumpLabels) { try { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[1]; jumpLabelCount++; } } catch { //do nothing } }

19

//if (!jumpLabels.Contains(searchTerm)) //{ // jumpLabels[jumpLabelCount] = words[1] ; // //} } else { charwrite = "6" + words[1]; string searchTerm = words[1]; foreach (string x in Program.jumpLabels) { try { if (!x.Contains(searchTerm)) { Program.jumpLabels[jumpLabelCount] = words[1]; jumpLabelCount++; } } catch { } } //if (!jumpLabels.Contains(searchTerm)) //{ // jumpLabels[jumpLabelCount] = words[1] ; // //} } jumpLabelCount++; jumpLabelCount++;

} if (flag) { flag = false; } else { locationCounter = locationCounter + 1;

J:\Assembler\Assembler\Program.cs } 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\Program.cs 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\Program.cs { 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\Program.cs 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; Console.WriteLine("Program ended thank you!"); break; default: if (programHalted) { memoryLocationhashtable.Add(words[0], locationCounter.ToString()); locationCounter = locationCounter + 1; } else {

23

Console.WriteLine("Bad Command at line number{0} ",lineNumber); } break;

if (!flag) { if (programEnded) { tobeWritten = ""; } else { tobeWritten = locationCounter.ToString() + " " + charwrite;

//here we write the temp file mytemp.WriteLine(tobeWritten);

J:\Assembler\Assembler\Program.cs ///here we write the hex file myWriter.WriteLine(tobeWritten); tobeWritten.Trim(); //charwrite.Trim(); ///here we convert the hex value to binary for the binary file

24

//binwrite = Convert.ToString(Convert.ToInt32(charwrite, 16), 2); binwrite = ""; } } myWriter1.WriteLine(binwrite); } }

myReader.Close(); myWriter.Close(); myWriter1.Close(); //StreamReader myReader1 = new StreamReader("hex.txt");

//while (line1 != null) //{ // // // // // // // line1 = myReader1.ReadLine(); if (line1 != null) { char[] charArray = line1.ToCharArray(); //Array.Reverse(charArray); //charread =(string) charArray; charwrite = hex2binary(line1);

// // //} }

myWriter1.WriteLine(charwrite);

Console.WriteLine("...............Finished"); Console.ReadLine(); //for (int i = 0; i < count; i++) //{ // Console.WriteLine("Looping through the Symbols Hashtable "); // // //} int value2 = (int)symbolshashtable["Area"]; Console.WriteLine(value2);

for (int i = 0; i < count; i++) { Console.WriteLine(" Label{0} ={1}", i, symbolictable[i]); locationCounter=locationCounter+1;

J:\Assembler\Assembler\Program.cs mytemp.WriteLine(locationCounter.ToString() + " " + symbolictable[i]. ToString()); } for (int i = 0; i < memoryLabelcount; i++) { Console.WriteLine("Memory Label{0}={1}", i, Program.memoryLabels[i]); Console.WriteLine("Memory Hashtable Contents {0}-->", memoryLocationhashtable[Program.memoryLabels[i]]); }

25

for (int i = 0; i < jumpLabelCount; i++) { Console.WriteLine("Looping through the Symbols Hashtable "); Console.WriteLine("Jump Labels{0}={1}", i, Program.jumpLabels[i]); string search = Program.jumpLabels[i]; int value2 = Convert.ToInt32(symbolshashtable[search]); Console.WriteLine("Line of Symbol label found {0}",value2); locationCounter = locationCounter + 1; mytemp.WriteLine(locationCounter.ToString() + " " + Program.jumpLabels[i]) ; }

mytemp.Close();

Console.ReadLine(); Console.WriteLine("Now Assembling and Processing temp file");

/////Processing the temp file //while (line != null) //{

//char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; //char[] delimiterChars = { ' ' };

string[] lines = System.IO.File.ReadAllLines("temp.txt"); // Display the file contents by using a foreach loop. System.Console.WriteLine("Contents of WriteLines2.txt = "); 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\Program.cs { //Console.WriteLine("Looping through the Symbols Hashtable "); //Console.WriteLine("Jump Labels{0}={1}", i, Program.jumpLabels [i]); string search = Program.jumpLabels[i]; int value2 = Convert.ToInt32(symbolshashtable[search]);

26

tempLine = linet.Replace(search, symbolshashtable[search].ToString ()); }

for (int i = 0; i < memoryLabelcount; i++) { //Console.WriteLine("Looping through the Symbols Hashtable "); //Console.WriteLine("Jump Labels{0}={1}", i, Program.jumpLabels [i]); string search = Program.memoryLabels[i]; int value2 = Convert.ToInt32(memoryLocationhashtable[search]); tempLine = tempLine.Replace(search, memoryLocationhashtable [search].ToString()); }

Console.WriteLine("\t" + linet);

myFWriter.WriteLine(tempLine);

} myFWriter.Close(); Console.ReadLine(); // line = mytemp.WriteLine("koko"); //words = line.Split(delimiterChars); // // // // // if (line != null) { //line.Trim(); words = line.Split(delimiterChars); char[] charArray = line.ToCharArray();

// // // //}

label = words[0]; labelfound = foundlabel(words[0]); }

J:\Assembler\Assembler\Program.cs ////Processing Ended here

27

//myReader1.Close();

static string hex2binary(string hexvalue) { string binaryval = ""; hexvalue.Trim(); binaryval = Convert.ToString(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 !objNotNaturalPattern.IsMatch(strNumber) && objNaturalPattern.IsMatch(strNumber); } // Function to test for Positive Integers with zero inclusive public bool IsWholeNumber(String strNumber) { Regex objNotWholePattern=new Regex("[^0-9]"); return !objNotWholePattern.IsMatch(strNumber); } // 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 !objNotIntPattern.IsMatch(strNumber) && objIntPattern.IsMatch (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 !objNotPositivePattern.IsMatch(strNumber) && objPositivePattern.IsMatch(strNumber) && !objTwoDotPattern.IsMatch(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 !objNotNumberPattern.IsMatch(strNumber) && !objTwoDotPattern.IsMatch(strNumber) && !objTwoMinusPattern.IsMatch(strNumber) &&

J:\Assembler\Assembler\Program.cs objNumberPattern.IsMatch(strNumber); } // Function To test for Alphabets. public static bool IsAlpha(String strToCheck) { Regex objAlphaPattern=new Regex("[^a-zA-Z]"); return !objAlphaPattern.IsMatch(strToCheck); } // Function to Check for AlphaNumeric. public static bool IsAlphaNumeric(String strToCheck) { Regex objAlphaNumericPattern=new Regex("[^a-zA-Z0-9]"); return !objAlphaNumericPattern.IsMatch(strToCheck); }

28

public static bool foundlabel(String str) { //string label; char[] labelchar = str.ToCharArray(); if (labelchar.Length == 4) { Console.WriteLine("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 == ",") {

Console.WriteLine("Valid Label"); return true; } else { return false; } } else return false; }

} }

You might also like