private static void InterpretFile(MipsToBinary mips, string[] args) { bool isWithNewLines = false; bool isHex = false; bool isBytePartition = false; // file operation mode if (args.Length > 1) { string mipsFile = ""; string binPath = ""; int i; for (i = 0; i < args.Length; i++) { if (args[i] == "-s" && i + 1 < args.Length) { mipsFile = args[i + 1]; } else if (args[i] == "-o" && i + 1 < args.Length) { binPath = args[i + 1]; } else if (args[i] == "-n") { isWithNewLines = true; } else if (args[i] == "-h") { isHex = true; } else if (args[i] == "-p") { isBytePartition = true; } } if (mipsFile == "" || binPath == "") { Console.WriteLine("Input error!"); return; } string input = File.ReadAllText(mipsFile); string binary; if (!isHex) { binary = mips.GetBinaryString(input, isWithNewLines); } else { binary = mips.GetHexString(input, isWithNewLines, isBytePartition); } File.WriteAllText(binPath, binary); Console.WriteLine("Done"); return; } }
static void Main(string[] args) { MipsMachine vm = new MipsMachine(); MipsToBinary converter = new MipsToBinary(); Prologue(); // interactive while (true) { bool isLittleEndian = true; uint address; uint length4Bytes = 1; Console.Write("> "); string[] input = Console.ReadLine().Split(); switch (input[0]) { case "c": // continue Continue(vm); break; case "s": // go to next step Step(vm); break; case "r": // read register // `r t0` RegisterType reg = (RegisterType)Enum.Parse(typeof(RegisterType), input[1]); if (input.Length >= 3) { bool.TryParse(input[2], out isLittleEndian); } Console.WriteLine($"0x{vm.QueryRegister(reg).ToHexString(isLittleEndian ? Endian.LittleEndian : Endian.BigEndian)}"); break; case "d": // read RAM // `d <address> <length/4Bytes> <0:little-endian/1:big-endian>` try { address = (uint)new System.ComponentModel.UInt32Converter().ConvertFromString(input[1]); } catch (Exception) { Console.WriteLine("[Error] Invalid Address"); continue; } if (input.Length >= 3) { uint.TryParse(input[2], out length4Bytes); } if (input.Length >= 4) { bool.TryParse(input[3], out isLittleEndian); } Console.WriteLine($"0x {vm.QueryRamAsHex(address, length4Bytes, isLittleEndian ? Endian.LittleEndian : Endian.BigEndian)}"); break; case "u": // read RAM as instructions // binarty -> MIPS asm // `u <address> <length/4Bytes>` try { address = (uint)new System.ComponentModel.UInt32Converter().ConvertFromString(input[1]); } catch (Exception) { Console.WriteLine("[Error] Invalid Address"); continue; } if (input.Length >= 3) { uint.TryParse(input[2], out length4Bytes); } Console.WriteLine(vm.GetMipsString(address, length4Bytes)); break; case "a": // write MIPS codes to RAM // `a` // `a <path to asm>` string mips; if (input.Length > 1) { mips = File.ReadAllText(input[1]); } else { mips = AskForMipsCode(); } ProgramInfo prog = converter.ParseMips(mips); MachineCodePack machineCode = prog.ToMachineCode(); vm.Reset(machineCode); // print the next instructions PrintNextInstructions(vm, 16); // do not log the changes caused by program loading vm.CleanLog(); break; case "h": // write hex to RAM // `h <path to hex>` WriteRawTextCodeToRam(input[1], vm, CodingSystem.Hex); break; case "b": // write binary to RAM // `b <path to binary>` WriteRawTextCodeToRam(input[1], vm, CodingSystem.Binary); break; } } }
static void Main(string[] args) { MipsToBinary mips = new MipsToBinary(); InterpretFile(mips, args); // instruction Console.WriteLine("Usage:"); Console.WriteLine(" ./MIPS.Interpreter.exe -s <file of MIPS code> -o <path to save binary> [-n] [-h] [-p]"); Console.WriteLine(" [-n] := (optional) enable new line for each instruction."); Console.WriteLine(" [-h] := (optional) hex."); Console.WriteLine(" [-p] := (optional) (hex only) add space partion for each byte."); bool isInteractiveMode = false; while (!isInteractiveMode) { // read file or interactive Console.WriteLine("[R]Read instructions from file or [I]Interactively interpret instruction?"); Console.Write("> "); string choice = Console.ReadLine(); switch (choice) { case "r": case "R": Console.WriteLine("Usage:"); Console.WriteLine(" -s <file of MIPS code> -o <path to save binary> [-n] [-h] [-p]"); Console.WriteLine(" [-n] := (optional) enable new line for each instruction."); Console.WriteLine(" [-h] := (optional) hex."); Console.WriteLine(" [-p] := (optional) (hex only) add space partion for each byte."); Console.Write("> "); string input = Console.ReadLine(); InterpretFile(mips, input.Split()); break; case "I": case "i": isInteractiveMode = true; break; } } // interactive mode Console.WriteLine("Please input a complete instruction for each line."); while (true) { Console.Write("> "); string input = Console.ReadLine(); string binary = mips.GetBinaryString(input, true); Console.WriteLine(binary); } // string input = ""; // input += "# this is comment\n"; // input += "add $t3, $t1, $zero\n"; // input += "sw $t1, 0x86($t2)\n"; // input += " sw $t1 , 0x86($t2)\n"; // input += "j label\n"; // input += "bne $t3, $t1, label\n"; // input += "move $t3, $t1\n"; // input += "bgt $t3, $t1, label \n"; // input += "addi $s3, $a1, -12 \n"; }