C# 如何从 ROM 文件中一次读取一个字节
How to read one byte at a time from ROM file in C#
我正在编写一个 space 入侵者模拟器作为个人学习项目。我遇到了 运行 问题,似乎 FileStream
class 中的 ReadByte()
方法一次读取了多个字节。这是代码:
private unsafe static void ReadROM()
{
using (FileStream fileStream = new FileStream(@"ROMpath", FileMode.Open))
{
IList<byte> codeBuffer = new List<byte>();
int hexIn;
while (fileStream.ReadByte() != -1)
{
hexIn = fileStream.ReadByte();
codeBuffer.Add((byte)hexIn);
}
byte[] arrayOfBytes = codeBuffer.ToArray();
fixed (byte* pntrToArrayOfBytes = arrayOfBytes)
{
I8080Disassembler disassembler = new Disassembler8080();
int pc = 0;
while (pc < arrayOfBytes.Length)
{
pc += disassembler.Disassemble(pntrToArrayOfBytes, pc);
}
}
}
}
static void Main(string[] args)
{
ReadROM();
Console.Read();
}
}
这是我正在阅读的一些 ROM 文件:
0000000 00 00 00 c3 d4 18 00 00 f5 c5 d5 e5 c3 8c 00 00
0000010 f5 c5 d5 e5 3e 80 32 72 20 21 c0 20 35 cd cd 17
0000020 db 01 0f da 67 00 3a ea 20 a7 ca 42 00 3a eb 20
0000030 fe 99 ca 3e 00 c6 01 27 32 eb 20 cd 47 19 af 32
正如您在ROM文件中看到的,前三个字节是00,即NOP
指令。我的代码似乎跳过了最后两个 NOP
指令,几乎就像它一次读取和前进超过一个字节一样。这可能意味着我的其余输出也是错误的。这是我得到的输出:
NOP
JMP 00 18
PUSH B
PUSH H
ADC H
NOP
PUSH B
PUSH H
ADD B
MOV M,D
LXI H, CD 20
三个前导 NOP
指令不存在。就好像他们被跳过了一样。这是反汇编代码:
public unsafe int Disassemble(byte* codeBuffer, int pc)
{
byte* code = &codeBuffer[pc];
int opBytes = 1; //the default length of the op
//Console.WriteLine("{0:X4}", pc);
switch (*code)
{
case 0x00: Console.WriteLine("NOP"); break;
case 0x01: Console.WriteLine("LXI B, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x02: Console.WriteLine("STAX B"); break;
case 0x03: Console.WriteLine("INX B"); break;
case 0x04: Console.WriteLine("INR B"); break;
case 0x05: Console.WriteLine("DCR B"); break;
case 0x06: Console.WriteLine("MVI B, {0:X2} ", code[1]); opBytes = 2; break;
case 0x07: Console.WriteLine("RLC"); break;
case 0x08: Console.WriteLine("NOP"); break;
case 0x09: Console.WriteLine("DAD B"); break;
case 0x0a: Console.WriteLine("LDAX B"); break;
case 0x0b: Console.WriteLine("DCX B"); break;
case 0x0c: Console.WriteLine("INR C"); break;
case 0x0d: Console.WriteLine("DCR C"); break;
case 0x0e: Console.WriteLine("MVI C, {0:X2} ", code[1]); opBytes = 2; break;
case 0x0f: Console.WriteLine("RRC"); break;
case 0x10: Console.WriteLine("NOP"); break;
case 0x11: Console.WriteLine("LXI D {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x12: Console.WriteLine("STAX D"); break;
case 0x13: Console.WriteLine("INX D"); break;
case 0x14: Console.WriteLine("INR D"); break;
case 0x15: Console.WriteLine("DCR D"); break;
case 0x16: Console.WriteLine("MVI D, {0:X2} ", code[1]); opBytes = 2; break;
case 0x17: Console.WriteLine("RAL"); break;
case 0x18: Console.WriteLine("NOP"); break;
case 0x19: Console.WriteLine("DAD D"); break;
case 0x1a: Console.WriteLine("LDAX D"); break;
case 0x1b: Console.WriteLine("DCX D"); break;
case 0x1c: Console.WriteLine("INR E"); break;
case 0x1d: Console.WriteLine("DCR E"); break;
case 0x1e: Console.WriteLine("MVI E, {0:X2} ", code[1]); opBytes = 2; break;
case 0x1f: Console.WriteLine("RAR"); break;
case 0x20: Console.WriteLine("NOP"); break;
case 0x21: Console.WriteLine("LXI H, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x22: Console.WriteLine("SHLD adr, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x23: Console.WriteLine("INX H"); break;
case 0x24: Console.WriteLine("INR H"); break;
case 0x25: Console.WriteLine("DCR H"); break;
case 0x26: Console.WriteLine("MVI H, {0:X2} ", code[1]); opBytes = 2; break;
case 0x27: Console.WriteLine("DAA"); break;
case 0x28: Console.WriteLine("NOP"); break;
case 0x29: Console.WriteLine("DAD H"); break;
case 0x2a: Console.WriteLine("LHLD adr, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x2b: Console.WriteLine("DCX H"); break;
case 0x2c: Console.WriteLine("INR L"); break;
case 0x2d: Console.WriteLine("DCR L"); break;
case 0x2e: Console.WriteLine("MVI L {0:X2}", code[1]); opBytes = 2; break;
case 0x2f: Console.WriteLine("CMA"); break;
case 0x30: Console.WriteLine("NOP"); break;
case 0x31: Console.WriteLine("LXI SP, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x32: Console.WriteLine("STA adr, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x33: Console.WriteLine("INX SP"); break;
case 0x34: Console.WriteLine("INR M"); break;
case 0x35: Console.WriteLine("DCR M"); break;
case 0x36: Console.WriteLine("MVI M, {0:X2} ", code[1]); opBytes = 2; break;
case 0x37: Console.WriteLine("STC"); break;
case 0x38: Console.WriteLine("NOP"); break;
case 0x39: Console.WriteLine("DAD SP"); break;
case 0x3a: Console.WriteLine("LDA adr, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x3b: Console.WriteLine("DCX SP"); break;
case 0x3c: Console.WriteLine("INR A"); break;
case 0x3d: Console.WriteLine("DCR A"); break;
case 0x3e: Console.WriteLine("MVI A, {0:X2} ", code[1]); opBytes = 2; break;
case 0x3f: Console.WriteLine("CMC"); break;
case 0x40: Console.WriteLine("MOV B,B"); break;
case 0x41: Console.WriteLine("MOV B,C"); break;
case 0x42: Console.WriteLine("MOV B,D"); break;
case 0x43: Console.WriteLine("MOV B,E"); break;
case 0x44: Console.WriteLine("MOV B,H"); break;
case 0x45: Console.WriteLine("MOV B,L"); break;
case 0x46: Console.WriteLine("MOV B,M"); break;
case 0x47: Console.WriteLine("MOV B,A"); break;
case 0x48: Console.WriteLine("MOV C,B"); break;
case 0x49: Console.WriteLine("MOV C,C"); break;
case 0x4a: Console.WriteLine("MOV C,D"); break;
case 0x4b: Console.WriteLine("MOV C,E"); break;
case 0x4c: Console.WriteLine("MOV C,H"); break;
case 0x4d: Console.WriteLine("MOV C,L"); break;
case 0x4e: Console.WriteLine("MOV C,M"); break;
case 0x4f: Console.WriteLine("MOV C,A"); break;
case 0x50: Console.WriteLine("MOV D,B"); break;
case 0x51: Console.WriteLine("MOV D,C"); break;
case 0x52: Console.WriteLine("MOV D,D"); break;
case 0x53: Console.WriteLine("MOV D,E"); break;
case 0x54: Console.WriteLine("MOV D,H"); break;
case 0x55: Console.WriteLine("MOV D,L"); break;
case 0x56: Console.WriteLine("MOV D,M"); break;
case 0x57: Console.WriteLine("MOV D,A"); break;
case 0x58: Console.WriteLine("MOV E,B"); break;
case 0x59: Console.WriteLine("MOV E,C"); break;
case 0x5a: Console.WriteLine("MOV E,D"); break;
case 0x5b: Console.WriteLine("MOV E,E"); break;
case 0x5c: Console.WriteLine("MOV E,H"); break;
case 0x5d: Console.WriteLine("MOV E,L"); break;
case 0x5e: Console.WriteLine("MOV E,M"); break;
case 0x5f: Console.WriteLine("MOV E,A"); break;
case 0x60: Console.WriteLine("MOV H,B"); break;
case 0x61: Console.WriteLine("MOV H,C"); break;
case 0x62: Console.WriteLine("MOV H,D"); break;
case 0x63: Console.WriteLine("MOV H,E"); break;
case 0x64: Console.WriteLine("MOV H,H"); break;
case 0x65: Console.WriteLine("MOV H,L"); break;
case 0x66: Console.WriteLine("MOV H,M"); break;
case 0x67: Console.WriteLine("MOV H,A"); break;
case 0x68: Console.WriteLine("MOV L,B"); break;
case 0x69: Console.WriteLine("MOV L,C"); break;
case 0x6a: Console.WriteLine("MOV L,D"); break;
case 0x6b: Console.WriteLine("MOV L,E"); break;
case 0x6c: Console.WriteLine("MOV L,H"); break;
case 0x6d: Console.WriteLine("MOV L,L"); break;
case 0x6e: Console.WriteLine("MOV L,M"); break;
case 0x6f: Console.WriteLine("MOV L,A"); break;
case 0x70: Console.WriteLine("MOV M,B"); break;
case 0x71: Console.WriteLine("MOV M,C"); break;
case 0x72: Console.WriteLine("MOV M,D"); break;
case 0x73: Console.WriteLine("MOV M,E"); break;
case 0x74: Console.WriteLine("MOV M,H"); break;
case 0x75: Console.WriteLine("MOV M,L"); break;
case 0x76: Console.WriteLine("HLT"); break;
case 0x77: Console.WriteLine("MOV M,A"); break;
case 0x78: Console.WriteLine("MOV A,B"); break;
case 0x79: Console.WriteLine("MOV A,C"); break;
case 0x7a: Console.WriteLine("MOV A,D"); break;
case 0x7b: Console.WriteLine("MOV A,E"); break;
case 0x7c: Console.WriteLine("MOV A,H"); break;
case 0x7d: Console.WriteLine("MOV A,L"); break;
case 0x7e: Console.WriteLine("MOV A,M"); break;
case 0x7f: Console.WriteLine("MOV A,A"); break;
case 0x80: Console.WriteLine("ADD B"); break;
case 0x81: Console.WriteLine("ADD C"); break;
case 0x82: Console.WriteLine("ADD D"); break;
case 0x83: Console.WriteLine("ADD E"); break;
case 0x84: Console.WriteLine("ADD H"); break;
case 0x85: Console.WriteLine("ADD L"); break;
case 0x86: Console.WriteLine("ADD M"); break;
case 0x87: Console.WriteLine("ADD A"); break;
case 0x88: Console.WriteLine("ADC B"); break;
case 0x89: Console.WriteLine("ADC C"); break;
case 0x8a: Console.WriteLine("ADC D"); break;
case 0x8b: Console.WriteLine("ADC E"); break;
case 0x8c: Console.WriteLine("ADC H"); break;
case 0x8d: Console.WriteLine("ADC L"); break;
case 0x8e: Console.WriteLine("ADC M"); break;
case 0x8f: Console.WriteLine("ADC A"); break;
case 0x90: Console.WriteLine("SUB B"); break;
case 0x91: Console.WriteLine("SUB C"); break;
case 0x92: Console.WriteLine("SUB D"); break;
case 0x93: Console.WriteLine("SUB E"); break;
case 0x94: Console.WriteLine("SUB H"); break;
case 0x95: Console.WriteLine("SUB L"); break;
case 0x96: Console.WriteLine("SUB M"); break;
case 0x97: Console.WriteLine("SUB A"); break;
case 0x98: Console.WriteLine("SBB B"); break;
case 0x99: Console.WriteLine("SBB C"); break;
case 0x9a: Console.WriteLine("SBB D"); break;
case 0x9b: Console.WriteLine("SBB E"); break;
case 0x9c: Console.WriteLine("SBB H"); break;
case 0x9d: Console.WriteLine("SBB L"); break;
case 0x9e: Console.WriteLine("SBB M"); break;
case 0x9f: Console.WriteLine("SBB A"); break;
case 0xa0: Console.WriteLine("ANA B"); break;
case 0xa1: Console.WriteLine("ANA C"); break;
case 0xa2: Console.WriteLine("ANA D"); break;
case 0xa3: Console.WriteLine("ANA E"); break;
case 0xa4: Console.WriteLine("ANA H"); break;
case 0xa5: Console.WriteLine("ANA L"); break;
case 0xa6: Console.WriteLine("ANA M"); break;
case 0xa7: Console.WriteLine("ANA A"); break;
case 0xa8: Console.WriteLine("XRA B"); break;
case 0xa9: Console.WriteLine("XRA C"); break;
case 0xaa: Console.WriteLine("XRA D"); break;
case 0xab: Console.WriteLine("XRA E"); break;
case 0xac: Console.WriteLine("XRA H"); break;
case 0xad: Console.WriteLine("XRA L"); break;
case 0xae: Console.WriteLine("XRA M"); break;
case 0xaf: Console.WriteLine("XRA A"); break;
case 0xb0: Console.WriteLine("ORA B"); break;
case 0xb1: Console.WriteLine("ORA C"); break;
case 0xb2: Console.WriteLine("ORA D"); break;
case 0xb3: Console.WriteLine("ORA E"); break;
case 0xb4: Console.WriteLine("ORA H"); break;
case 0xb5: Console.WriteLine("ORA L"); break;
case 0xb6: Console.WriteLine("ORA M"); break;
case 0xb7: Console.WriteLine("ORA A"); break;
case 0xb8: Console.WriteLine("CMP B"); break;
case 0xb9: Console.WriteLine("CMP C"); break;
case 0xba: Console.WriteLine("CMP D"); break;
case 0xbb: Console.WriteLine("CMP E"); break;
case 0xbc: Console.WriteLine("CMP H"); break;
case 0xbd: Console.WriteLine("CMP L"); break;
case 0xbe: Console.WriteLine("CMP M"); break;
case 0xbf: Console.WriteLine("CMP A"); break;
case 0xc0: Console.WriteLine("RNZ"); break;
case 0xc1: Console.WriteLine("POP B"); break;
case 0xc2: Console.WriteLine("JNZ {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xc3: Console.WriteLine("JMP {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xc4: Console.WriteLine("CNZ {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xc5: Console.WriteLine("PUSH B"); break;
case 0xc6: Console.WriteLine("ADI {0:X2} ", code[1]); opBytes = 2; break;
case 0xc7: Console.WriteLine("RST 0"); break;
case 0xc8: Console.WriteLine("RZ"); break;
case 0xc9: Console.WriteLine("RET"); break;
case 0xca: Console.WriteLine("JZ {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xcb: Console.WriteLine("NOP"); break;
case 0xcc: Console.WriteLine("CZ {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xcd: Console.WriteLine("CALL {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xce: Console.WriteLine("ACI {0:X2} {1:X2} ", code[1]); opBytes = 2; break;
case 0xcf: Console.WriteLine("RST 1"); break;
case 0xd0: Console.WriteLine("RNC"); break;
case 0xd1: Console.WriteLine("POP D"); break;
case 0xd2: Console.WriteLine("JNC {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xd3: Console.WriteLine("OUT {0:X2} ", code[1]); opBytes = 2; break;
case 0xd4: Console.WriteLine("CNC {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xd5: Console.WriteLine("PUSH D"); break;
case 0xd6: Console.WriteLine("SUI {0:X2} ", code[1]); opBytes = 2; break;
case 0xd7: Console.WriteLine("RST 2"); break;
case 0xd8: Console.WriteLine("RC"); break;
case 0xd9: Console.WriteLine("NOP"); break;
case 0xda: Console.WriteLine("JC {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xdb: Console.WriteLine("IN {0:X2} ", code[1]); opBytes = 2; break;
case 0xdc: Console.WriteLine("CC {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xdd: Console.WriteLine("NOP"); break;
case 0xde: Console.WriteLine("SBI {0:X2} ", code[1]); opBytes = 2; break;
case 0xdf: Console.WriteLine("RST 3"); break;
case 0xe0: Console.WriteLine("RPO"); break;
case 0xe1: Console.WriteLine("POP H"); break;
case 0xe2: Console.WriteLine("JPO {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xe3: Console.WriteLine("XTHL"); break;
case 0xe4: Console.WriteLine("CPO {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xe5: Console.WriteLine("PUSH H"); break;
case 0xe6: Console.WriteLine("ANI {0:X2} ", code[1]); opBytes = 2; break;
case 0xe7: Console.WriteLine("RST 4"); break;
case 0xe8: Console.WriteLine("RPE"); break;
case 0xe9: Console.WriteLine("PCHL"); break;
case 0xea: Console.WriteLine("JPE {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xeb: Console.WriteLine("XCHG"); break;
case 0xec: Console.WriteLine("CPE {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xed: Console.WriteLine("NOP"); break;
case 0xee: Console.WriteLine("XRI {0:X2} ", code[1]); opBytes = 2; break;
case 0xef: Console.WriteLine("RST 5"); break;
case 0xf0: Console.WriteLine("RP"); break;
case 0xf2: Console.WriteLine("JP {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xf3: Console.WriteLine("DI"); break;
case 0xf4: Console.WriteLine("CP {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xf5: Console.WriteLine("PUSH PSW"); break;
case 0xf6: Console.WriteLine("ORI {0:X2} ", code[1]); opBytes = 2; break;
case 0xf7: Console.WriteLine("RST 6"); break;
case 0xf8: Console.WriteLine("RM"); break;
case 0xf9: Console.WriteLine("SPHL"); break;
case 0xfa: Console.WriteLine("JM {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xfb: Console.WriteLine("EI"); break;
case 0xfc: Console.WriteLine("CM {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xfd: Console.WriteLine("NOP"); break;
case 0xfe: Console.WriteLine("CPI {0:X2} ", code[1]); opBytes = 2; break;
case 0xff: Console.WriteLine("RST 7"); break;
}
return opBytes;
}
我不认为问题出在反汇编程序中,但我可能是错的。 Disassemble
方法 return 是每条指令在内存中占用的字节数(一、二或三)。该 return 值然后用作获取下一条指令的偏移量。谁能确定我做错了什么?我感谢任何帮助。我正在读取的 ROM 文件是 invaders.h,可以通过快速 Google 搜索轻松找到,以防有人想知道。
这里有两个ReadByte
调用;
while (fileStream.ReadByte() != -1)
{
hexIn = fileStream.ReadByte();
codeBuffer.Add((byte)hexIn);
}
你应该在 for 循环中阅读它;
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);
// Read and verify the data.
for(int i = 0; i < fileStream.Length; i++)
{
hexIn = fileStream.ReadByte();
codeBuffer.Add((byte)hexIn);
}
我正在编写一个 space 入侵者模拟器作为个人学习项目。我遇到了 运行 问题,似乎 FileStream
class 中的 ReadByte()
方法一次读取了多个字节。这是代码:
private unsafe static void ReadROM()
{
using (FileStream fileStream = new FileStream(@"ROMpath", FileMode.Open))
{
IList<byte> codeBuffer = new List<byte>();
int hexIn;
while (fileStream.ReadByte() != -1)
{
hexIn = fileStream.ReadByte();
codeBuffer.Add((byte)hexIn);
}
byte[] arrayOfBytes = codeBuffer.ToArray();
fixed (byte* pntrToArrayOfBytes = arrayOfBytes)
{
I8080Disassembler disassembler = new Disassembler8080();
int pc = 0;
while (pc < arrayOfBytes.Length)
{
pc += disassembler.Disassemble(pntrToArrayOfBytes, pc);
}
}
}
}
static void Main(string[] args)
{
ReadROM();
Console.Read();
}
}
这是我正在阅读的一些 ROM 文件:
0000000 00 00 00 c3 d4 18 00 00 f5 c5 d5 e5 c3 8c 00 00
0000010 f5 c5 d5 e5 3e 80 32 72 20 21 c0 20 35 cd cd 17
0000020 db 01 0f da 67 00 3a ea 20 a7 ca 42 00 3a eb 20
0000030 fe 99 ca 3e 00 c6 01 27 32 eb 20 cd 47 19 af 32
正如您在ROM文件中看到的,前三个字节是00,即NOP
指令。我的代码似乎跳过了最后两个 NOP
指令,几乎就像它一次读取和前进超过一个字节一样。这可能意味着我的其余输出也是错误的。这是我得到的输出:
NOP
JMP 00 18
PUSH B
PUSH H
ADC H
NOP
PUSH B
PUSH H
ADD B
MOV M,D
LXI H, CD 20
三个前导 NOP
指令不存在。就好像他们被跳过了一样。这是反汇编代码:
public unsafe int Disassemble(byte* codeBuffer, int pc)
{
byte* code = &codeBuffer[pc];
int opBytes = 1; //the default length of the op
//Console.WriteLine("{0:X4}", pc);
switch (*code)
{
case 0x00: Console.WriteLine("NOP"); break;
case 0x01: Console.WriteLine("LXI B, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x02: Console.WriteLine("STAX B"); break;
case 0x03: Console.WriteLine("INX B"); break;
case 0x04: Console.WriteLine("INR B"); break;
case 0x05: Console.WriteLine("DCR B"); break;
case 0x06: Console.WriteLine("MVI B, {0:X2} ", code[1]); opBytes = 2; break;
case 0x07: Console.WriteLine("RLC"); break;
case 0x08: Console.WriteLine("NOP"); break;
case 0x09: Console.WriteLine("DAD B"); break;
case 0x0a: Console.WriteLine("LDAX B"); break;
case 0x0b: Console.WriteLine("DCX B"); break;
case 0x0c: Console.WriteLine("INR C"); break;
case 0x0d: Console.WriteLine("DCR C"); break;
case 0x0e: Console.WriteLine("MVI C, {0:X2} ", code[1]); opBytes = 2; break;
case 0x0f: Console.WriteLine("RRC"); break;
case 0x10: Console.WriteLine("NOP"); break;
case 0x11: Console.WriteLine("LXI D {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x12: Console.WriteLine("STAX D"); break;
case 0x13: Console.WriteLine("INX D"); break;
case 0x14: Console.WriteLine("INR D"); break;
case 0x15: Console.WriteLine("DCR D"); break;
case 0x16: Console.WriteLine("MVI D, {0:X2} ", code[1]); opBytes = 2; break;
case 0x17: Console.WriteLine("RAL"); break;
case 0x18: Console.WriteLine("NOP"); break;
case 0x19: Console.WriteLine("DAD D"); break;
case 0x1a: Console.WriteLine("LDAX D"); break;
case 0x1b: Console.WriteLine("DCX D"); break;
case 0x1c: Console.WriteLine("INR E"); break;
case 0x1d: Console.WriteLine("DCR E"); break;
case 0x1e: Console.WriteLine("MVI E, {0:X2} ", code[1]); opBytes = 2; break;
case 0x1f: Console.WriteLine("RAR"); break;
case 0x20: Console.WriteLine("NOP"); break;
case 0x21: Console.WriteLine("LXI H, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x22: Console.WriteLine("SHLD adr, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x23: Console.WriteLine("INX H"); break;
case 0x24: Console.WriteLine("INR H"); break;
case 0x25: Console.WriteLine("DCR H"); break;
case 0x26: Console.WriteLine("MVI H, {0:X2} ", code[1]); opBytes = 2; break;
case 0x27: Console.WriteLine("DAA"); break;
case 0x28: Console.WriteLine("NOP"); break;
case 0x29: Console.WriteLine("DAD H"); break;
case 0x2a: Console.WriteLine("LHLD adr, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x2b: Console.WriteLine("DCX H"); break;
case 0x2c: Console.WriteLine("INR L"); break;
case 0x2d: Console.WriteLine("DCR L"); break;
case 0x2e: Console.WriteLine("MVI L {0:X2}", code[1]); opBytes = 2; break;
case 0x2f: Console.WriteLine("CMA"); break;
case 0x30: Console.WriteLine("NOP"); break;
case 0x31: Console.WriteLine("LXI SP, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x32: Console.WriteLine("STA adr, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x33: Console.WriteLine("INX SP"); break;
case 0x34: Console.WriteLine("INR M"); break;
case 0x35: Console.WriteLine("DCR M"); break;
case 0x36: Console.WriteLine("MVI M, {0:X2} ", code[1]); opBytes = 2; break;
case 0x37: Console.WriteLine("STC"); break;
case 0x38: Console.WriteLine("NOP"); break;
case 0x39: Console.WriteLine("DAD SP"); break;
case 0x3a: Console.WriteLine("LDA adr, {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0x3b: Console.WriteLine("DCX SP"); break;
case 0x3c: Console.WriteLine("INR A"); break;
case 0x3d: Console.WriteLine("DCR A"); break;
case 0x3e: Console.WriteLine("MVI A, {0:X2} ", code[1]); opBytes = 2; break;
case 0x3f: Console.WriteLine("CMC"); break;
case 0x40: Console.WriteLine("MOV B,B"); break;
case 0x41: Console.WriteLine("MOV B,C"); break;
case 0x42: Console.WriteLine("MOV B,D"); break;
case 0x43: Console.WriteLine("MOV B,E"); break;
case 0x44: Console.WriteLine("MOV B,H"); break;
case 0x45: Console.WriteLine("MOV B,L"); break;
case 0x46: Console.WriteLine("MOV B,M"); break;
case 0x47: Console.WriteLine("MOV B,A"); break;
case 0x48: Console.WriteLine("MOV C,B"); break;
case 0x49: Console.WriteLine("MOV C,C"); break;
case 0x4a: Console.WriteLine("MOV C,D"); break;
case 0x4b: Console.WriteLine("MOV C,E"); break;
case 0x4c: Console.WriteLine("MOV C,H"); break;
case 0x4d: Console.WriteLine("MOV C,L"); break;
case 0x4e: Console.WriteLine("MOV C,M"); break;
case 0x4f: Console.WriteLine("MOV C,A"); break;
case 0x50: Console.WriteLine("MOV D,B"); break;
case 0x51: Console.WriteLine("MOV D,C"); break;
case 0x52: Console.WriteLine("MOV D,D"); break;
case 0x53: Console.WriteLine("MOV D,E"); break;
case 0x54: Console.WriteLine("MOV D,H"); break;
case 0x55: Console.WriteLine("MOV D,L"); break;
case 0x56: Console.WriteLine("MOV D,M"); break;
case 0x57: Console.WriteLine("MOV D,A"); break;
case 0x58: Console.WriteLine("MOV E,B"); break;
case 0x59: Console.WriteLine("MOV E,C"); break;
case 0x5a: Console.WriteLine("MOV E,D"); break;
case 0x5b: Console.WriteLine("MOV E,E"); break;
case 0x5c: Console.WriteLine("MOV E,H"); break;
case 0x5d: Console.WriteLine("MOV E,L"); break;
case 0x5e: Console.WriteLine("MOV E,M"); break;
case 0x5f: Console.WriteLine("MOV E,A"); break;
case 0x60: Console.WriteLine("MOV H,B"); break;
case 0x61: Console.WriteLine("MOV H,C"); break;
case 0x62: Console.WriteLine("MOV H,D"); break;
case 0x63: Console.WriteLine("MOV H,E"); break;
case 0x64: Console.WriteLine("MOV H,H"); break;
case 0x65: Console.WriteLine("MOV H,L"); break;
case 0x66: Console.WriteLine("MOV H,M"); break;
case 0x67: Console.WriteLine("MOV H,A"); break;
case 0x68: Console.WriteLine("MOV L,B"); break;
case 0x69: Console.WriteLine("MOV L,C"); break;
case 0x6a: Console.WriteLine("MOV L,D"); break;
case 0x6b: Console.WriteLine("MOV L,E"); break;
case 0x6c: Console.WriteLine("MOV L,H"); break;
case 0x6d: Console.WriteLine("MOV L,L"); break;
case 0x6e: Console.WriteLine("MOV L,M"); break;
case 0x6f: Console.WriteLine("MOV L,A"); break;
case 0x70: Console.WriteLine("MOV M,B"); break;
case 0x71: Console.WriteLine("MOV M,C"); break;
case 0x72: Console.WriteLine("MOV M,D"); break;
case 0x73: Console.WriteLine("MOV M,E"); break;
case 0x74: Console.WriteLine("MOV M,H"); break;
case 0x75: Console.WriteLine("MOV M,L"); break;
case 0x76: Console.WriteLine("HLT"); break;
case 0x77: Console.WriteLine("MOV M,A"); break;
case 0x78: Console.WriteLine("MOV A,B"); break;
case 0x79: Console.WriteLine("MOV A,C"); break;
case 0x7a: Console.WriteLine("MOV A,D"); break;
case 0x7b: Console.WriteLine("MOV A,E"); break;
case 0x7c: Console.WriteLine("MOV A,H"); break;
case 0x7d: Console.WriteLine("MOV A,L"); break;
case 0x7e: Console.WriteLine("MOV A,M"); break;
case 0x7f: Console.WriteLine("MOV A,A"); break;
case 0x80: Console.WriteLine("ADD B"); break;
case 0x81: Console.WriteLine("ADD C"); break;
case 0x82: Console.WriteLine("ADD D"); break;
case 0x83: Console.WriteLine("ADD E"); break;
case 0x84: Console.WriteLine("ADD H"); break;
case 0x85: Console.WriteLine("ADD L"); break;
case 0x86: Console.WriteLine("ADD M"); break;
case 0x87: Console.WriteLine("ADD A"); break;
case 0x88: Console.WriteLine("ADC B"); break;
case 0x89: Console.WriteLine("ADC C"); break;
case 0x8a: Console.WriteLine("ADC D"); break;
case 0x8b: Console.WriteLine("ADC E"); break;
case 0x8c: Console.WriteLine("ADC H"); break;
case 0x8d: Console.WriteLine("ADC L"); break;
case 0x8e: Console.WriteLine("ADC M"); break;
case 0x8f: Console.WriteLine("ADC A"); break;
case 0x90: Console.WriteLine("SUB B"); break;
case 0x91: Console.WriteLine("SUB C"); break;
case 0x92: Console.WriteLine("SUB D"); break;
case 0x93: Console.WriteLine("SUB E"); break;
case 0x94: Console.WriteLine("SUB H"); break;
case 0x95: Console.WriteLine("SUB L"); break;
case 0x96: Console.WriteLine("SUB M"); break;
case 0x97: Console.WriteLine("SUB A"); break;
case 0x98: Console.WriteLine("SBB B"); break;
case 0x99: Console.WriteLine("SBB C"); break;
case 0x9a: Console.WriteLine("SBB D"); break;
case 0x9b: Console.WriteLine("SBB E"); break;
case 0x9c: Console.WriteLine("SBB H"); break;
case 0x9d: Console.WriteLine("SBB L"); break;
case 0x9e: Console.WriteLine("SBB M"); break;
case 0x9f: Console.WriteLine("SBB A"); break;
case 0xa0: Console.WriteLine("ANA B"); break;
case 0xa1: Console.WriteLine("ANA C"); break;
case 0xa2: Console.WriteLine("ANA D"); break;
case 0xa3: Console.WriteLine("ANA E"); break;
case 0xa4: Console.WriteLine("ANA H"); break;
case 0xa5: Console.WriteLine("ANA L"); break;
case 0xa6: Console.WriteLine("ANA M"); break;
case 0xa7: Console.WriteLine("ANA A"); break;
case 0xa8: Console.WriteLine("XRA B"); break;
case 0xa9: Console.WriteLine("XRA C"); break;
case 0xaa: Console.WriteLine("XRA D"); break;
case 0xab: Console.WriteLine("XRA E"); break;
case 0xac: Console.WriteLine("XRA H"); break;
case 0xad: Console.WriteLine("XRA L"); break;
case 0xae: Console.WriteLine("XRA M"); break;
case 0xaf: Console.WriteLine("XRA A"); break;
case 0xb0: Console.WriteLine("ORA B"); break;
case 0xb1: Console.WriteLine("ORA C"); break;
case 0xb2: Console.WriteLine("ORA D"); break;
case 0xb3: Console.WriteLine("ORA E"); break;
case 0xb4: Console.WriteLine("ORA H"); break;
case 0xb5: Console.WriteLine("ORA L"); break;
case 0xb6: Console.WriteLine("ORA M"); break;
case 0xb7: Console.WriteLine("ORA A"); break;
case 0xb8: Console.WriteLine("CMP B"); break;
case 0xb9: Console.WriteLine("CMP C"); break;
case 0xba: Console.WriteLine("CMP D"); break;
case 0xbb: Console.WriteLine("CMP E"); break;
case 0xbc: Console.WriteLine("CMP H"); break;
case 0xbd: Console.WriteLine("CMP L"); break;
case 0xbe: Console.WriteLine("CMP M"); break;
case 0xbf: Console.WriteLine("CMP A"); break;
case 0xc0: Console.WriteLine("RNZ"); break;
case 0xc1: Console.WriteLine("POP B"); break;
case 0xc2: Console.WriteLine("JNZ {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xc3: Console.WriteLine("JMP {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xc4: Console.WriteLine("CNZ {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xc5: Console.WriteLine("PUSH B"); break;
case 0xc6: Console.WriteLine("ADI {0:X2} ", code[1]); opBytes = 2; break;
case 0xc7: Console.WriteLine("RST 0"); break;
case 0xc8: Console.WriteLine("RZ"); break;
case 0xc9: Console.WriteLine("RET"); break;
case 0xca: Console.WriteLine("JZ {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xcb: Console.WriteLine("NOP"); break;
case 0xcc: Console.WriteLine("CZ {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xcd: Console.WriteLine("CALL {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xce: Console.WriteLine("ACI {0:X2} {1:X2} ", code[1]); opBytes = 2; break;
case 0xcf: Console.WriteLine("RST 1"); break;
case 0xd0: Console.WriteLine("RNC"); break;
case 0xd1: Console.WriteLine("POP D"); break;
case 0xd2: Console.WriteLine("JNC {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xd3: Console.WriteLine("OUT {0:X2} ", code[1]); opBytes = 2; break;
case 0xd4: Console.WriteLine("CNC {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xd5: Console.WriteLine("PUSH D"); break;
case 0xd6: Console.WriteLine("SUI {0:X2} ", code[1]); opBytes = 2; break;
case 0xd7: Console.WriteLine("RST 2"); break;
case 0xd8: Console.WriteLine("RC"); break;
case 0xd9: Console.WriteLine("NOP"); break;
case 0xda: Console.WriteLine("JC {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xdb: Console.WriteLine("IN {0:X2} ", code[1]); opBytes = 2; break;
case 0xdc: Console.WriteLine("CC {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xdd: Console.WriteLine("NOP"); break;
case 0xde: Console.WriteLine("SBI {0:X2} ", code[1]); opBytes = 2; break;
case 0xdf: Console.WriteLine("RST 3"); break;
case 0xe0: Console.WriteLine("RPO"); break;
case 0xe1: Console.WriteLine("POP H"); break;
case 0xe2: Console.WriteLine("JPO {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xe3: Console.WriteLine("XTHL"); break;
case 0xe4: Console.WriteLine("CPO {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xe5: Console.WriteLine("PUSH H"); break;
case 0xe6: Console.WriteLine("ANI {0:X2} ", code[1]); opBytes = 2; break;
case 0xe7: Console.WriteLine("RST 4"); break;
case 0xe8: Console.WriteLine("RPE"); break;
case 0xe9: Console.WriteLine("PCHL"); break;
case 0xea: Console.WriteLine("JPE {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xeb: Console.WriteLine("XCHG"); break;
case 0xec: Console.WriteLine("CPE {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xed: Console.WriteLine("NOP"); break;
case 0xee: Console.WriteLine("XRI {0:X2} ", code[1]); opBytes = 2; break;
case 0xef: Console.WriteLine("RST 5"); break;
case 0xf0: Console.WriteLine("RP"); break;
case 0xf2: Console.WriteLine("JP {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xf3: Console.WriteLine("DI"); break;
case 0xf4: Console.WriteLine("CP {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xf5: Console.WriteLine("PUSH PSW"); break;
case 0xf6: Console.WriteLine("ORI {0:X2} ", code[1]); opBytes = 2; break;
case 0xf7: Console.WriteLine("RST 6"); break;
case 0xf8: Console.WriteLine("RM"); break;
case 0xf9: Console.WriteLine("SPHL"); break;
case 0xfa: Console.WriteLine("JM {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xfb: Console.WriteLine("EI"); break;
case 0xfc: Console.WriteLine("CM {0:X2} {1:X2} ", code[2], code[1]); opBytes = 3; break;
case 0xfd: Console.WriteLine("NOP"); break;
case 0xfe: Console.WriteLine("CPI {0:X2} ", code[1]); opBytes = 2; break;
case 0xff: Console.WriteLine("RST 7"); break;
}
return opBytes;
}
我不认为问题出在反汇编程序中,但我可能是错的。 Disassemble
方法 return 是每条指令在内存中占用的字节数(一、二或三)。该 return 值然后用作获取下一条指令的偏移量。谁能确定我做错了什么?我感谢任何帮助。我正在读取的 ROM 文件是 invaders.h,可以通过快速 Google 搜索轻松找到,以防有人想知道。
这里有两个ReadByte
调用;
while (fileStream.ReadByte() != -1)
{
hexIn = fileStream.ReadByte();
codeBuffer.Add((byte)hexIn);
}
你应该在 for 循环中阅读它;
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);
// Read and verify the data.
for(int i = 0; i < fileStream.Length; i++)
{
hexIn = fileStream.ReadByte();
codeBuffer.Add((byte)hexIn);
}