Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        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;
            }
        }