本文介绍了B32assembler代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用使用System.Drawing; 使用 System.Linq; 使用 System.Text; 使用使用System.Windows.Forms; 命名空间 WindowsForm { 公共 部分 类 fmmainform:表格 { 私有 字符串 SourceProgram; 私有 System.Collections.Hashtable LabelTable; 私有 int CurrentNdx; 私有 ushort AsLength; 私有 布尔 IsEnd; 私有 ushort ExecutionAddress; 私有 枚举注册 { 未知= 0 , A = 4 , B = 2 , D = 1 , X = 16 , Y = 8 } 公共 fmmainform() { InitializeComponent(); LabelTable = 新 System.Collections.Hashtable( 50 ); CurrentNdx = 0 ; AsLength = 0 ; ExecutionAddress = 0 ; IsEnd = false ; SourceProgram = " " ; txtOrigin_Click.Text = " 1000" ; } 私有 无效 btnSourceFile_Click(对象发件人,EventArgs e) { 此 .fdSourceFile.ShowDialog(); 此 .SourceFileName.Text = fdSourceFile.FileName; } 私有 无效 btnOutputBrowse_Click(对象发件人,EventArgs e) { 此 .fdDestinationFile.ShowDialog(); 此 .OutputFileName.Text = fdDestinationFile.FileName; } 私有 无效 btnAssemble_Click(对象发件人,EventArgs e) { AsLength = Convert.ToUInt16( this .txtOrigin_Click.Text, 16 ); System.IO.BinaryWriter输出; System.IO.TextReader输入; System.IO.FileStream fs = 新 System.IO.FileStream(此 .OutputFileName.Text,System.IO.FileMode.Create); 输出= 新 System.IO.BinaryWriter(fs); 输入= 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 ); 解析(输出); output.Seek( 5 ,System.IO.SeekOrigin.Begin); output.Write(ExecutionAddress); output.Close(); fs.Close(); MessageBox.Show(" 完成!" ); } 私有 无效解析(System.IO.BinaryWriter OutputFile) { CurrentNdx = 0 ; 同时(IsEnd == false ) LabelScan(OutputFile, true ); IsEnd = false ; CurrentNdx = 0 ; AsLength = Convert.ToUInt16( this .txtOrigin_Click.Text, 16 ); 同时(IsEnd == false ) LabelScan(OutputFile, false ); } 私有 无效 LabelScan(System.IO.BinaryWriter OutputFile, bool IsLabelScan) { 如果(字符 .IsLetter(SourceProgram [CurrentNdx])) { // 必须是标签 如果(IsLabelScan)LabelTable.Add(GetLabelName(),AsLength); 同时(SourceProgram [CurrentNdx] == ' \ n' ) CurrentNdx ++; CurrentNdx ++; 返回; } EatWhiteSpaces(); ReadMneumonic(OutputFile,IsLabelScan); } 私有 无效 ReadMneumonic(System.IO.BinaryWriter OutputFile, bool IsLabelScan) { 字符串 Mneumonic = " " ; while (!( char .IsWhiteSpace(SourceProgram [CurrentNdx]))) { Mneumonic = Mneumonic + SourceProgram [CurrentNdx]; CurrentNdx ++; } 如果(Mneumonic.ToUpper()== " LDX" )InterpretLDX(OutputFile, IsLabelScan); 如果(Mneumonic.ToUpper()== " LDA" )InterpretLDA(OutputFile, IsLabelScan); 如果(Mneumonic.ToUpper()== " STA" )InterpretSTA(OutputFile, IsLabelScan); 如果(Mneumonic.ToUpper()== " END" ) { IsEnd = true ; DoEnd(OutputFile,IsLabelScan); EatWhiteSpaces(); ExecutionAddress = ( ushort )LabelTable [(GetLabelName())]; 返回; } 同时(SourceProgram [CurrentNdx]!= ' \ n' ) { CurrentNdx ++; } CurrentNdx ++; } 私有 无效 InterpretLDA(System.IO.BinaryWriter OutputFile, bool IsLabelScan) { EatWhiteSpaces(); 如果(SourceProgram [CurrentNdx] == ' #') { CurrentNdx ++; 字节 val = ReadByteValue(); AsLength + = 2 ; 如果(!IsLabelScan) { OutputFile.Write(( byte )0x01); OutputFile.Write(val); } } } 私有 无效 InterpretLDX(System.IO.BinaryWriter OutputFile, bool IsLabelScan) { EatWhiteSpaces(); 如果(SourceProgram [CurrentNdx] == ' #') { CurrentNdx ++; ushort val = ReadWordValue(); AsLength + = 3 ; 如果(!IsLabelScan) { OutputFile.Write(( byte )0x02); OutputFile.Write(val); } } } 私有 无效 InterpretSTA(System.IO.BinaryWriter OutputFile, bool IsLabelScan) { EatWhiteSpaces(); 如果(SourceProgram [CurrentNdx] == ' ,') { 寄存器r; 字节 opcode = 0x00; CurrentNdx ++; EatWhiteSpaces(); r = ReadRegister(); 开关(r) { 大小写 Registers.X: 操作码= 0x03; break ; } AsLength + = 1 ; 如果(!IsLabelScan) { OutputFile.Write(opcode); } } } 私有 void DoEnd(System.IO.BinaryWriter OutputFile,> bool IsLabelScan) { AsLength ++; 如果(!IsLabelScan) { OutputFile.Write(( byte )0x04); } } 私有注册ReadRegister() { 寄存器r = Registers.Unknown; 如果(((SourceProgram [CurrentNdx] == ' X' )|| (SourceProgram [CurrentNdx] == ' x')r = Registers.X; 如果(((SourceProgram [CurrentNdx] == ' Y' )|| (SourceProgram [CurrentNdx] == ' y')r = Registers.Y; 如果((SourceProgram [CurrentNdx] == ' D' )|| (SourceProgram [CurrentNdx] == ' d'))r = Registers.D; 如果((SourceProgram [CurrentNdx] == ' A' )|| (SourceProgram [CurrentNdx] == ' a'))r = Registers.A; 如果((SourceProgram [CurrentNdx] == ' B' )|| (SourceProgram [CurrentNdx] == ' b'))r = Registers.B; CurrentNdx ++; 返回 r; } 私有 ushort ReadWordValue() { ushort val = 0 ; 布尔 IsHex = false ; 字符串 sval = " " ; 如果(SourceProgram [CurrentNdx] == ' $') { CurrentNdx ++; IsHex = true ; } 同时(字符.IsLetterOrDigit(SourceProgram [CurrentNdx])) { sval = sval + SourceProgram [CurrentNdx]; CurrentNdx ++; } 如果(IsHex) { val = Convert.ToUInt16(sval, 16 ); } 其他 { val = ushort .Parse(sval); } 返回 val; } 私有 字节 ReadByteValue() { byte val = 0 ; 布尔 IsHex = false ; 字符串 sval = " " ; 如果(SourceProgram [CurrentNdx] == ' $') { CurrentNdx ++; IsHex = true ; } 同时(字符.IsLetterOrDigit(SourceProgram [CurrentNdx])) { sval = sval + SourceProgram [CurrentNdx]; CurrentNdx ++; } 如果(IsHex) { val = Convert.ToByte(sval, 16 ); } 其他 { val = 字节 .Parse(sval); } 返回 val; } 私有 无效 EatWhiteSpaces() { 同时(字符 .IsWhiteSpace(SourceProgram [CurrentNdx])) { CurrentNdx ++; } } 私有 字符串 GetLabelName() { 字符串 lblname = " " ; 同时(字符.IsLetterOrDigit(SourceProgram [CurrentNdx])) { 如果(SourceProgram [CurrentNdx] == ' :') { CurrentNdx ++; break ; } lblname = lblname + SourceProgram [CurrentNdx]; CurrentNdx ++; } 返回 lblname.ToUpper(); } 私有 无效 button3_Click(对象发件人,EventArgs e) { AsLength = Convert.ToUInt16( this .txtOrigin_Click.Text, 16 ); System.IO.BinaryWriter输出; System.IO.TextReader输入; System.IO.FileStream fs = 新 System.IO.FileStream(此 .OutputFileName.Text,System.IO.FileMode.Create); 输出= 新 System.IO.BinaryWriter(fs); 输入= 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 ); 解析(输出); output.Seek( 5 ,System.IO.SeekOrigin.Begin); output.Write(ExecutionAddress); output.Close(); fs.Close(); MessageBox.Show(" 完成!" ); } 私有 无效 SourceFile_Click_Click(对象发件人,EventArgs e) { 此 .fdSourceFile.ShowDialog(); 此 .SourceFileName.Text = fdSourceFile.FileName; } 私有 无效 OutputBrowse_Click_Click(对象发件人,EventArgs e) { 此 .fdDestinationFile.ShowDialog(); 此 .OutputFileName.Text = fdDestinationFile.FileName; } } }
这是我的编码错误是在while语句中如何清除getlabelname中的indexoutofbound期望..plz帮助我
[edit]添加了代码块,将我的内容作为纯文本..."选项已禁用-OriginalGriff [/edit]
解决方案
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]
解决方案
这篇关于B32assembler代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!