private void Verify_Offset2(int list_index, string offset) //offeset represent label ,adress of label { //ADRESARE IMEDIATA la adresa data de eticheta try { string signed_value_to_be_written = null; new_type label_state = new new_type(Labels[offset]);//here i will get adress of brench //if label adress is smaller than brench adress=>-offset if (Convert.ToUInt16((list_index)) > Convert.ToUInt16(label_state.GetAdress())) { signed_value_to_be_written = toSignedBinary8(Convert.ToUInt16((list_index + 2) - label_state.GetAdress())); } //if label adress is bigger than brench =>+offset else if (Convert.ToUInt16((list_index)) < Convert.ToUInt16(label_state.GetAdress())) { signed_value_to_be_written = toBinary8(Convert.ToUInt16(label_state.GetAdress() - (list_index + 2))); } string value_to_be_written = signed_value_to_be_written; BinaryElements[list_index] = BinaryElements[list_index] + value_to_be_written; } catch (Exception exc) { MessageBox.Show(exc.Message, "Error in the assembler file", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Verify_Offset(string opcode, int list_index, string offset)//offeset represent label ,adress of label { //ADRESARE IMEDIATA la adresa data de eticheta try { string signed_value_to_be_written = null; new_type label_state = new new_type(Labels[offset]); index = GetNumeberOfLocationWritten(BinaryElements); if (Convert.ToUInt16((index + 2)) > Convert.ToUInt16(label_state.GetAdress())) { signed_value_to_be_written = toSignedBinary8(Convert.ToUInt16((index + 2) - label_state.GetAdress())); } else if (Convert.ToUInt16((index + 2)) < Convert.ToUInt16(label_state.GetAdress())) { signed_value_to_be_written = toBinary8(Convert.ToUInt16(label_state.GetAdress() - (index + 2))); } //else //{ // signed_value_to_be_written = toBinary8(Convert.ToUInt16(index - Labels[offset])); //} string value_to_be_written = opcode + signed_value_to_be_written; BinaryElements[list_index] = value_to_be_written; } catch (Exception exc) { MessageBox.Show(exc.Message, "Error in the assembler file", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ConvertASMButton_Click(object sender, EventArgs e) { try { GetOpcode(asmGlobalElements); foreach (KeyValuePair <string, new_type> kvp in Brench) { new_type brench_status = new new_type(kvp.Value); if (brench_status.GetState() == false) { Verify_Offset2(brench_status.GetAdress(), kvp.Key); kvp.Value.SetState(true); } } DisplayBinaryCodeArray(BinaryElements); WriteBinary(); //writing instruction in to binary output file } catch (Exception exc) { MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public new_type(new_type param) { this.address = param.address; this.state = param.state; }
public void GetOpcode(List <String> asmElements) { bool classB1 = false; bool classB2 = false; bool classB3 = false; bool classB4 = false; bool register = false; bool number = false; bool label = false; //contor used to save the number of instruction in list //index used like PC for (int i = 0; i < asmGlobalElements.Count();) { String s = asmGlobalElements[i]; if (B1.ContainsKey(s)) { classB1 = true; string value = B1[s]; int old_contor = contor; Verify_First_Operand(value, contor, asmGlobalElements[i + 1]); string value2 = BinaryElements[old_contor]; Verify_Second_Operand(value2, old_contor, asmGlobalElements[i + 2]); i += 3;//used to go to the next instruction element contor += 2; } //instructiuni cu un singur operand else if (B2.ContainsKey(s)) { classB2 = true; // OPCODE.Add(s); String value = B2[s]; // binaryElements.Insert(contor, value); Verify_First_Operand(value, contor, asmGlobalElements[i + 1]); // OutputTextBox2.Text += "stringul este de clasa B2 " + classB2 + " " + s +" Value:"+value +Environment.NewLine; i += 2; contor += 2; } //instructiuni de salt else if (B3.ContainsKey(s)) { classB3 = true; String value = B3[s]; string offset = asmGlobalElements[i + 1]; bool state = Labels[offset].GetState(); if (Labels.ContainsKey(offset) && (state == true)) { Verify_Offset(value, contor, offset); } else { BinaryElements[contor] = value; new_type brench_state = new new_type(contor, false); Brench.Add(offset, brench_state);//saving those brench that do not have declared an label yet } i += 2; //index += 2;//used to go to the next instruction element contor += 2;//used to now at which instruction we are } //instructiuni diverse else if (B4.ContainsKey(s)) { classB4 = true; // OPCODE.Add(s); String value = B4[s]; //binaryElements.Insert(contor,value); BinaryElements[contor] = value; i++; // index += 1;//used to go to the next instruction element contor += 2; //used to now at which instruction we are } if (classB1 == false && classB2 == false && classB3 == false & classB4 == false) { if (Labels.ContainsKey(s)) { new_type label_state = new new_type(contor, true); Labels[s] = label_state; //reactualize data for labels(adress depending on actual program size and state label_state = Labels[s]; //get data(adress ,state) int adress_to_jump = label_state.GetAdress(); BinaryElements[contor] = toBinary16(adress_to_jump); contor += 2; label = true; i++; } } classB1 = false; classB2 = false; classB3 = false; classB4 = false; label = false; } }
private void ParseFileButton_Click(object sender, EventArgs e) { try { /* local variable used for debugging only */ int lineCounter = 0; /* List which will store each token (element) read from ASM file */ List <String> asmElements = new List <String>(); /* Create a parser object used for ASM file * REMEMBER: this parser can be used for all kind of text files!!! */ TextFieldParser parser = new TextFieldParser(filename); /* Reinitialize the Text property of OutputTextBox */ OutputTextBox.Text = ""; /* Define delimiters in ASM file */ String[] delimiters = { ",", " " }; /* Specify that the elements in ASM file are delimited by some characters */ parser.TextFieldType = FieldType.Delimited; /* Set-up the specified delimiters */ parser.SetDelimiters(delimiters); /* Parse the entire ASM file based on previous specifications*/ while (!parser.EndOfData) {//here was pc-2 PC += 2; /* Read an entire line in ASM file * and split this line in many strings delimited by delimiters */ string[] asmFields = parser.ReadFields(); /* Store each string as a single element in the list * if this string is not empty */ foreach (string s in asmFields) { if (!s.Equals("")) { if (s.EndsWith(":")) {//here was pc-2 new_type label_state = new new_type(PC - 2, false); Labels.Add(s.Split(':')[0], label_state); asmElements.Add(s.Split(':')[0]); asmGlobalElements.Add(s.Split(':')[0]); } else { asmElements.Add(s); //add in to a global list parsed elements of asm file asmGlobalElements.Add(s); } } } /* Counting the number of lines stored in ASM file */ lineCounter++; } /* Close the parser */ parser.Close(); /* If the file is empty, trigger a new exception which * in turn will display an error message */ if (lineCounter == 0) { Exception exc = new Exception("The ASM file is empty!"); throw exc; } else { /* Display every token in OutputTextBox */ foreach (String s in asmElements) { OutputTextBox.Text += s + Environment.NewLine; } /* Display an information about the process completion */ MessageBox.Show("Parsing is completed!", "Assembler information", MessageBoxButtons.OK, MessageBoxIcon.Information); } //verifying if there is any elements in global list ,if the initial asm file have been parsed if (asmGlobalElements.Count() != 0) { ConvertASMButton.Enabled = true; } else { ConvertASMButton.Enabled = false; } } catch (Exception exc) { MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }