• 大小: 3KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2021-05-17
  • 语言: Java
  • 标签: MIPS  反编译器  Java  

资源简介

资源包含可运行的java代码,以及sample.txt(二进制源文件)作为输入测试用例。运行结果为解析后的汇编代码,和模拟出来的每个周期的寄存器状态。

资源截图

代码片段和文件信息

/*On my honour I have neither given nor received unauthorized aid on this assignment.*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MIPSsim {

public static void main(String[] args) throws FileNotFoundException IOException Throwable {
File file = new File(args[0]);
Reader reader = new Reader(file);
Parser parser = new Parser();

Simulator simulator = new Simulator(256 file.getAbsoluteFile().getParent());
Instruction instruction = new Instruction();

String line;
while ((line = reader.read()) != null) {
instruction = parser.parseInstruction(line);
simulator.addInstruction(instruction);
if (instruction.name.equals(“BREAK“)) {
break;
}
}
PrintStream dis_out = new PrintStream(file.getAbsoluteFile().getParent() + “/disassembly.txt“);
//if(instruction.isEmpty()){
if(line == null){
dis_out.print(“The input file is empty.“);
}

ArrayList data = new ArrayList();
while ((line = reader.read()) != null) {
data.add(parser.parserData(line));
}
simulator.setData(data.toArray());

//simulator.printInstructions();

simulator.printInstructions(dis_out);
dis_out.close();
simulator.run();
reader.close();
}
}

class Parser {

public Parser() {
}

public Instruction parseInstruction(String line) {
Instruction instruction = new Instruction();
instruction.binary = line;
String prefix = line.substring(0 2);
int opCode = binaryToDec(line.substring(2 6) false);
line = line.substring(6); 
InstructionFormat definition = Config.nullDefinition;

if (prefix.equals(“01“)) {
definition = Config.formatCategory1[opCode];
} else if (prefix.equals(“11“)) {
definition = Config.formatCategory2[opCode];
} else {
// unrecogonized instruction
}

instruction.name = definition.name;
instruction.template = definition.template;

//System.out.print(instruction.name + “ “);
// fill args
int[] args = new int[definition.argsLength.length];
for (int i = 0; i != args.length; i++) {
boolean isSignedArgs = false;
int argsLength = definition.argsLength[i];
if (argsLength < 0) {
isSignedArgs = true;
argsLength = -argsLength;
}
String rawArgs = line.substring(0 argsLength);
args[i] = Parser.binaryToDec(rawArgs isSignedArgs);
//System.out.print(“ “ + args[i]);
line = line.substring(argsLength);
}
//System.out.println();
int[] orderedArgs = new int[definition.argsOrder.length];
for (int i = 0; i != orderedArgs.length; i++) {
orderedArgs[i] = args[definitio

评论

共有 条评论