|
| 1 | +import java.util.HashMap; |
| 2 | +import java.util.Map; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class Instruction { |
| 6 | + private static String machineCode; |
| 7 | + private static Map<String, String> lable = new HashMap<>(); |
| 8 | + |
| 9 | + public static String main(String instructs) { |
| 10 | + instructs = lable(instructs); |
| 11 | + String result = ""; |
| 12 | + Scanner scanner = new Scanner(instructs); |
| 13 | + while (scanner.hasNextLine()) { |
| 14 | + result += instruct(scanner.nextLine()) + "\n"; |
| 15 | + } |
| 16 | + |
| 17 | + return result; |
| 18 | + } |
| 19 | + private static String instruct(String instruct) { |
| 20 | + machineCode = ""; |
| 21 | + instruct = instruct.trim(); |
| 22 | + String[] type = instruct.split(" ", 2); // Command Type Separation |
| 23 | + String typeValidation = R_type.callOrder(type[0]); // To help with validation |
| 24 | + |
| 25 | + if (typeValidation != null) { // R-type |
| 26 | + String[] register = type[1].split(", "); |
| 27 | + machineCode += "000" + // OpCode |
| 28 | + RegisterFile.callRegister(register[0]) + // rs |
| 29 | + RegisterFile.callRegister(register[1]) + // rt |
| 30 | + RegisterFile.callRegister(register[2]) + // rd |
| 31 | + "00000000000000" + // Don't Care |
| 32 | + typeValidation; // Function |
| 33 | + |
| 34 | + } else { // V-type |
| 35 | + machineCode += V_type.callOrder(type[0]); |
| 36 | + |
| 37 | + if (type[0].equals("lw") || type[0].equals("sw")) { // sw & lw commands |
| 38 | + String[] register = type[1].split(", "); |
| 39 | + machineCode += RegisterFile.callRegister(register[0].substring(0, 3)) + // rs |
| 40 | + RegisterFile.callRegister(register[1].substring(register[1].indexOf('(') + 1, register[1].indexOf(')'))) + // rt |
| 41 | + DecimalToBinary.extend(register[1].substring(0, register[1].indexOf('('))); // immediate |
| 42 | + |
| 43 | + } else { |
| 44 | + String[] register = type[1].split(", "); |
| 45 | + if (type[0].equals("bnq")) { // bnq commands |
| 46 | + machineCode += RegisterFile.callRegister(register[0]) + // rs |
| 47 | + RegisterFile.callRegister(register[1]) + // rt |
| 48 | + DecimalToBinary.extend(lable.get(register[2].trim())); // immediate |
| 49 | + |
| 50 | + } else { // other commands |
| 51 | + machineCode += RegisterFile.callRegister(register[0]) + // rs |
| 52 | + RegisterFile.callRegister(register[1]) + // rt |
| 53 | + DecimalToBinary.extend(register[2]); // immediate |
| 54 | + |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return machineCode; |
| 59 | + } |
| 60 | + |
| 61 | + private static String lable(String instructs) { |
| 62 | + Scanner scanner = new Scanner(instructs); |
| 63 | + String instruct = ""; |
| 64 | + int count = 0; |
| 65 | + |
| 66 | + while (scanner.hasNextLine()) { |
| 67 | + String place = scanner.nextLine(); |
| 68 | + |
| 69 | + if (place.indexOf(':') != -1) { |
| 70 | + lable.put(place.substring(0, place.indexOf(':')).trim(), Integer.toString(count)); // lables |
| 71 | + instruct += place.substring(place.indexOf(':') + 1) + "\n"; |
| 72 | + } else { |
| 73 | + instruct += place + "\n"; |
| 74 | + } |
| 75 | + |
| 76 | + count++; |
| 77 | + } |
| 78 | + |
| 79 | + return instruct; |
| 80 | + } |
| 81 | +} |
0 commit comments