Click here to Skip to main content
15,868,062 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsForm
{
         public partial class fmmainform : Form
{
private string SourceProgram;
private System.Collections.Hashtable LabelTable;
private int CurrentNdx;
private ushort AsLength;
private bool IsEnd;
private ushort ExecutionAddress;
        
private enum Registers
        {
            Unknown = 0,
            A = 4,
            B = 2,
            D = 1,
            X = 16,
            Y = 8
        }
    
    
      public fmmainform()
        {
            InitializeComponent();
            LabelTable = new System.Collections.Hashtable(50);
            CurrentNdx = 0;
            AsLength = 0;
            ExecutionAddress = 0;
            IsEnd = false;
            SourceProgram = "";
            txtOrigin_Click.Text = "1000";
        }

        private void btnSourceFile_Click(object sender, EventArgs e)
        {
            this.fdSourceFile.ShowDialog();
            this.SourceFileName.Text = fdSourceFile.FileName;
        }
        private void btnOutputBrowse_Click(object sender, EventArgs e)
        {
            this.fdDestinationFile.ShowDialog();
            this.OutputFileName.Text = fdDestinationFile.FileName;
        }
       
        private void btnAssemble_Click(object sender, EventArgs e)
        {
            AsLength = Convert.ToUInt16(this.txtOrigin_Click.Text, 16);
            System.IO.BinaryWriter output;
            System.IO.TextReader input;
            System.IO.FileStream fs = new
            System.IO.FileStream(this.OutputFileName.Text, System.IO.FileMode.Create);
            output = new System.IO.BinaryWriter(fs);
            input = System.IO.File.OpenText(this.SourceFileName.Text);
            SourceProgram = input.ReadToEnd();
            input.Close();
            output.Write('B');
            output.Write('3');
            output.Write('2');
            output.Write(Convert.ToUInt16(this.txtOrigin_Click.Text, 16));
            output.Write((ushort)0);
            Parse(output);
            output.Seek(5, System.IO.SeekOrigin.Begin);
            output.Write(ExecutionAddress);
            output.Close();
            fs.Close();
            MessageBox.Show("Done!");
          }
    
        
        private void Parse(System.IO.BinaryWriter OutputFile)
        {
            CurrentNdx = 0;
            while (IsEnd == false)
                LabelScan(OutputFile, true);
            IsEnd = false;
            CurrentNdx = 0;
            AsLength = Convert.ToUInt16(this.txtOrigin_Click.Text, 16);
            while (IsEnd == false)
                LabelScan(OutputFile, false);
        }
        private void LabelScan(System.IO.BinaryWriter OutputFile, bool
IsLabelScan)
        {
            if (char.IsLetter(SourceProgram[CurrentNdx]))
            {
                // Must be a label
                if (IsLabelScan) LabelTable.Add(GetLabelName(), AsLength);
                while (SourceProgram[CurrentNdx] == '\n')
                    CurrentNdx++;
                CurrentNdx++;
                return;
            }
            EatWhiteSpaces();
            ReadMneumonic(OutputFile, IsLabelScan);
        }
        private void ReadMneumonic(System.IO.BinaryWriter OutputFile, bool IsLabelScan)
        {
            string Mneumonic = "";
            while (!(char.IsWhiteSpace(SourceProgram[CurrentNdx])))
            {
                Mneumonic = Mneumonic + SourceProgram[CurrentNdx];
                CurrentNdx++;
            }
            if (Mneumonic.ToUpper() == "LDX") InterpretLDX(OutputFile,
            IsLabelScan);
            if (Mneumonic.ToUpper() == "LDA") InterpretLDA(OutputFile,
            IsLabelScan);
            if (Mneumonic.ToUpper() == "STA") InterpretSTA(OutputFile,
            IsLabelScan);
            if (Mneumonic.ToUpper() == "END")
            {
                IsEnd = true;
                DoEnd(OutputFile, IsLabelScan); EatWhiteSpaces(); ExecutionAddress =
                 (ushort)LabelTable[(GetLabelName())]; return;
            }
            while (SourceProgram[CurrentNdx] != '\n')
            {
                CurrentNdx++;
            }
            CurrentNdx++;
        }
        private void InterpretLDA(System.IO.BinaryWriter OutputFile, bool
IsLabelScan)
        {
            EatWhiteSpaces();
            if (SourceProgram[CurrentNdx] == '#')
            {
                CurrentNdx++;
                byte val = ReadByteValue();
                AsLength += 2;
                if (!IsLabelScan)
                {
                    OutputFile.Write((byte)0x01);
                    OutputFile.Write(val);
                }
            }
        }
               
               private void InterpretLDX(System.IO.BinaryWriter OutputFile, bool
IsLabelScan)
        {
            EatWhiteSpaces();
            if (SourceProgram[CurrentNdx] == '#')
            {
                CurrentNdx++;
                ushort val = ReadWordValue();
                AsLength += 3;
                if (!IsLabelScan)
                {
                    OutputFile.Write((byte)0x02);
                    OutputFile.Write(val);
                }
            }
        }
        private void InterpretSTA(System.IO.BinaryWriter OutputFile, bool
IsLabelScan)
{
EatWhiteSpaces();
if (SourceProgram[CurrentNdx] == ',')
{
Registers r;
byte opcode = 0x00;
CurrentNdx++;
EatWhiteSpaces();
r = ReadRegister();
switch (r)
{
case Registers.X:
opcode = 0x03;
break;
}
AsLength += 1;
if (!IsLabelScan)
{
    OutputFile.Write(opcode);
}
}
}
        private void DoEnd(System.IO.BinaryWriter OutputFile, bool
IsLabelScan)
        {
            AsLength++;
            if (!IsLabelScan)
            {
                OutputFile.Write((byte)0x04);
            }
        }
        private Registers ReadRegister()
        {
            Registers r = Registers.Unknown;
            if ((SourceProgram[CurrentNdx] == 'X') ||
            (SourceProgram[CurrentNdx] == 'x')) r = Registers.X;
            if ((SourceProgram[CurrentNdx] == 'Y') ||
            (SourceProgram[CurrentNdx] == 'y')) r = Registers.Y;
            if ((SourceProgram[CurrentNdx] == 'D') ||
            (SourceProgram[CurrentNdx] == 'd')) r = Registers.D;
            if ((SourceProgram[CurrentNdx] == 'A') ||
            (SourceProgram[CurrentNdx] == 'a')) r = Registers.A;
            if ((SourceProgram[CurrentNdx] == 'B') ||
            (SourceProgram[CurrentNdx] == 'b')) r = Registers.B;
            CurrentNdx++;
            return r;
        }
        private ushort ReadWordValue()
        {
            ushort val = 0;
            bool IsHex = false;
            string sval = "";
            if (SourceProgram[CurrentNdx] == '$')
            {
                CurrentNdx++;
                IsHex = true;
            }
            while (char.IsLetterOrDigit(SourceProgram[CurrentNdx]))
            {
                sval = sval + SourceProgram[CurrentNdx];
                CurrentNdx++;
            }
            if (IsHex)
            {
                val = Convert.ToUInt16(sval, 16);
            }
            else
            {
                val = ushort.Parse(sval);
            }
            return val;
        }
        private byte ReadByteValue()
        {
         byte val = 0;
          bool IsHex = false;
         string sval = "";
         if (SourceProgram[CurrentNdx] == '$')
        {
         CurrentNdx++;
         IsHex = true;
         }
           while (char.IsLetterOrDigit(SourceProgram[CurrentNdx]))
           {
            sval = sval + SourceProgram[CurrentNdx];
            CurrentNdx++;
           }
              if (IsHex)
              {
               val = Convert.ToByte(sval, 16);
              }
                 else
              {
               val = byte.Parse(sval);
              }
               return val;
           }
        private void EatWhiteSpaces()
        {
            while (char.IsWhiteSpace(SourceProgram[CurrentNdx]))
            {
                CurrentNdx++;
            }
        }
        private string GetLabelName()
        {
            string lblname = "";
        
            while (char.IsLetterOrDigit(SourceProgram[CurrentNdx]))
            {
                if (SourceProgram[CurrentNdx] == ':')
                {
                    CurrentNdx++;
                    break;
                }
                lblname = lblname + SourceProgram[CurrentNdx];
                CurrentNdx++;
            }
            return lblname.ToUpper();
        }
        private void button3_Click(object sender, EventArgs e)
        {
                        AsLength = Convert.ToUInt16(this.txtOrigin_Click.Text, 16);
            System.IO.BinaryWriter output;
            System.IO.TextReader input;
            System.IO.FileStream fs = new
            System.IO.FileStream(this.OutputFileName.Text, System.IO.FileMode.Create);
            output = new System.IO.BinaryWriter(fs);
            input = System.IO.File.OpenText(this.SourceFileName.Text);
            SourceProgram = input.ReadToEnd();
            input.Close();
            output.Write('p');
            output.Write('3');
            output.Write('2');
            output.Write(Convert.ToUInt16(this.txtOrigin_Click.Text, 16));
            output.Write((ushort)0);
            Parse(output);
            output.Seek(5, System.IO.SeekOrigin.Begin);
            output.Write(ExecutionAddress);
            output.Close();
            fs.Close();
            MessageBox.Show("Done!");
        }
        private void SourceFile_Click_Click(object sender, EventArgs e)
        {
            this.fdSourceFile.ShowDialog();
            this.SourceFileName.Text = fdSourceFile.FileName;
        }
        private void OutputBrowse_Click_Click(object sender, EventArgs e)
        {
            this.fdDestinationFile.ShowDialog();
            this.OutputFileName.Text = fdDestinationFile.FileName;
        }
        
    }
}

this is my coding error is indexoutofbound expection in getlabelname in while statement how to clear it.. plz help me

[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 26-Dec-11 21:03pm
v2

1 solution

The problem occurs because you have no checking on CurrentNdx to make sure you do not run out of the array.
You need to check that it does not exceed the available space each time you increment it.

BTW: Don't just post your whole program: it is called a Code Dump and considered lazy and rude. Post just the relevant code fragments in future, please. This helps us to help you as we do not have to wade through your badly formatted code to find the relevant parts.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900