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]))
{
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]