我在 switch 指令中有错误,但我不知道为什么。看主类:
try {
var result = string.Join(" ",File.ReadAllBytes(args[0]).Select(x => x.ToString("X")));
Console.WriteLine(result);
ASM asm = new ASM();
if(result != null) asm.EXEC(result); //Error here?
}
catch(Exception ex) {
Console.WriteLine(ex.Message);
}
和 ASM 类。我认为 EXEC 方法和 switch-case 指令中的错误:
public class ASM
{
public void EXEC(string ex)
{
if (ex == null)
Console.WriteLine("ASM opcode is null!");
string[] a = ex.Split(' ');
int i = 0;
while (i<a.Length)
{
switch (a [i])
{
case "AF": //hlt
Environment.FailFast("hlt");
break;
case "AB": //Write
i++;
string memorySegment = a[i];
i++;
int memoryValue = Convert.ToInt32(a[i]);
if(memorySegment == "F0") Memory.Write("0xC00",memoryValue);
else if (memorySegment == "F1") Memory.Write("0x100",memoryValue);
else if (memorySegment == "F2") Memory.Write("0x200",memoryValue);
else if (memorySegment == "F3") Memory.Write("0xA00",memoryValue);
else if (memorySegment == "F4") Memory.Write("0xB00",memoryValue);
break;
}
i++;
}
}
在控制台中,我看到AB F1 19 AF
Object reference not set to an instance of an object
如何消除此错误?代码成功写入文件的十六进制代码,但 ASM.EXEC 方法(结果)的参数不为空,但方法不可读。谢谢,对不起我的英语不好。