private void ExecuteCode(OpcodeWriter writer) { if (running) { if (MessageBox.Show("Already running do you want to stop ?", "Asm.Net - x86 Emulator", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { hALTToolStripMenuItem_Click(null, null); } } listViewEx1.ClearInstructions(); asm = new AsmNet(writer.Generate(true)); asm.CurrentInstructionEvent += onCurrentInstruction; asm.HALTEvent += onHALT; Stopwatch InstructionTicks = Stopwatch.StartNew(); cpu = asm.InitializeCPU(); Instruction[] instructions = new Instruction[cpu.RamMemory.Instructions.Values.Count]; cpu.RamMemory.Instructions.Values.CopyTo(instructions, 0); listViewEx1.AddInstructions(instructions); asm.DebugMode = true; toolStripStatusLabel3.Text = "Status: Running"; running = true; }
private void loadFromFileToolStripMenuItem_Click(object sender, EventArgs e) { using(OpenFileDialog dialog = new OpenFileDialog()) { dialog.Multiselect = false; if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (running) { if (MessageBox.Show("Already running do you want to stop ?", "Asm.Net - x86 Emulator", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { hALTToolStripMenuItem_Click(null, null); } } listViewEx1.ClearInstructions(); asm = new AsmNet(File.ReadAllBytes(dialog.FileName)); asm.CurrentInstructionEvent += onCurrentInstruction; asm.HALTEvent += onHALT; Stopwatch InstructionTicks = Stopwatch.StartNew(); cpu = asm.InitializeCPU(); Instruction[] instructions = new Instruction[cpu.RamMemory.Instructions.Values.Count]; cpu.RamMemory.Instructions.Values.CopyTo(instructions, 0); listViewEx1.AddInstructions(instructions); asm.DebugMode = true; toolStripStatusLabel3.Text = "Status: Running"; running = true; } } }
private void loadPortableExecutableToolStripMenuItem_Click(object sender, EventArgs e) { if (running) { if (MessageBox.Show("Already running do you want to stop ?", "Asm.Net - x86 Emulator", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { hALTToolStripMenuItem_Click(null, null); listViewEx1.ClearInstructions(); } } //lets load a real program and try to emulate it at 0.4 MHz lol its even slower then a 8086 cpu what the hell using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Filter = "Portable Executable|*.exe"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { asm = new AsmNet(dialog.FileName); } else { return; } } asm.CurrentInstructionEvent += onCurrentInstruction; asm.HALTEvent += onHALT; Stopwatch InstructionTicks = Stopwatch.StartNew(); cpu = asm.InitializeCPU(); listViewEx1.ClearInstructions(); Instruction[] instructions = new Instruction[cpu.RamMemory.Instructions.Values.Count]; cpu.RamMemory.Instructions.Values.CopyTo(instructions, 0); listViewEx1.AddInstructions(instructions); asm.DebugMode = true; toolStripStatusLabel3.Text = "Status: Running"; }
///<summary> /// <para Name="AddressChecking"> This will re-check the JUMP addresses and such to prevent errors at runtime </para> ///</summary> public byte[] Generate(bool AddressChecking) { codeSection.FixJumps(); List <Byte> stream = new List <Byte>(); //checksums byte[] DataSectionChecksum = new MD5CryptoServiceProvider().ComputeHash(dataSection.stream.ToArray()); byte[] CodeSectionChecksum = new MD5CryptoServiceProvider().ComputeHash(codeSection.stream.ToArray()); byte[] ApiSectionChecksum = new MD5CryptoServiceProvider().ComputeHash(ApiSection.ToByteArray); byte[] dataTmp = dataSection.stream.ToArray(); byte[] codeTmp = codeSection.stream.ToArray(); byte[] apiTmp = ApiSection.ToByteArray; for (int i = 0; i < dataTmp.Length; i++) { dataTmp[i] ^= DataSectionChecksum[i % DataSectionChecksum.Length]; } for (int i = 0; i < codeTmp.Length; i++) { codeTmp[i] ^= CodeSectionChecksum[i % CodeSectionChecksum.Length]; } for (int i = 0; i < apiTmp.Length; i++) { apiTmp[i] ^= ApiSectionChecksum[i % ApiSectionChecksum.Length]; } //create data section stream.AddRange(BitConverter.GetBytes(dataTmp.Length)); stream.AddRange(dataTmp); //create code section stream.AddRange(BitConverter.GetBytes(codeTmp.Length)); stream.AddRange(codeTmp); //create api section stream.AddRange(BitConverter.GetBytes(apiTmp.Length)); stream.AddRange(apiTmp); stream.AddRange(DataSectionChecksum); stream.AddRange(CodeSectionChecksum); stream.AddRange(ApiSectionChecksum); byte[] ret = stream.ToArray(); ret = QuickLZ.Compress(ret, 0, (uint)ret.Length); //lets see if 1 of our JUMPs is having a unreachable address //This is just a extra check for runtime if a error will occur if (AddressChecking) { AsmNet asm = new AsmNet(ret); asm.InitializeCPU(); foreach (Instruction i in asm.processor.RamMemory.Instructions.Values) { if (i.ToString().StartsWith("JE") || i.ToString().StartsWith("JMP") || i.ToString().StartsWith("JNE") || i.ToString().StartsWith("JNZ")) { //ok lets see if our jump address is reachable bool found = false; foreach (Instruction x in asm.processor.RamMemory.Instructions.Values) { if (((IJump)i).JumpAddress == x.VirtualAddress.Address) { found = true; break; } } if (!found) { if (codeSection.labels.ContainsValue(((IJump)i).JumpAddress - Options.MemoryBaseAddress)) { throw new Exception(i.ToString() + ", Unreachable memory address, Label: " + codeSection.labels.Keys[codeSection.labels.IndexOfValue(((IJump)i).JumpAddress - Options.MemoryBaseAddress)]); } throw new Exception(i.ToString() + ", Unreachable memory address"); } } } } return(ret); }