Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Ass2Mac class for assembling the assembly code to the machine code.
- */
- import java.io.*;
- public class Ass2Mac
- {
- public static int counter = 0;
- /*File to be executed*/
- // public static String fileName="input.txt";
- public static String fileName="mult.txt";
- // public static String fileName="comb2.txt";
- // public static String fileName="sum.txt";
- public static void main(String[] args) throws IOException
- {
- int maxIns = 1000; //Declaring maximum of instructions
- String[] ins = new String[maxIns];
- /*Reading the executed file*/
- String line;
- FileReader inputFile = new FileReader(fileName);
- BufferedReader bufferReader = new BufferedReader(inputFile);
- while ((line = bufferReader.readLine()) != null)
- {
- ins[counter] = line; //Declaring instructions arrays
- counter++; //Instruction Counting
- }
- int maxCol = 100; //Declaring maximum of instruction array's Column
- /*Declaring 2D arrays that split instructions to column*/
- String[][] subins = new String[counter][maxCol];
- for (int i = 0; i < counter ; i++ )
- {
- subins[i] = ins[i].split("\\s+"); //Spliting by whitespace
- }
- /*Assembling the assembly to machine codes*/
- for (int i = 0; i < counter ; i++ )
- {
- /*Checking for ".fill" instructions*/
- if(subins[i][1].equals(".fill"))
- {
- /*If found the Label then check for duplicate lable at another lines*/
- for(int j = 0 ; j < counter ; j++)
- {
- if(subins[j][1].equals(".fill") && subins[i][0].equals(subins[j][0]) && i != j)
- {
- System.out.println("Duplicate Label at line"+(j+1));
- }
- }
- /*If .fill instructions contain number then keep the number and get rid of others */
- if(isInteger(subins[i][2]))
- {
- ins[i] = subins[i][2];
- }
- /*If .fill instructions contain a symbolic address then keep its address*/
- else
- {
- for(int j = 0 ; j < counter ; j++)
- {
- if(subins[i][2].equals(subins[j][0]))
- {
- ins[i] = ""+j;
- }
- }
- }
- }
- /*To check instructions*/
- else
- {
- /*Passing the split word to get its OP*/
- String op = chkOP(subins[i][1]);
- /*If get O-type instructions then immediately assemble it to its number*/
- if(subins[i][1].equals("noop") || subins[i][1].equals("halt")){
- ins[i] = op;
- }
- else
- {
- /*If get R or I or J type instructions then split the word for rs and rt */
- String rs = dec2bin(Integer.parseInt(subins[i][2]));
- String rt = dec2bin(Integer.parseInt(subins[i][3]));
- /*If get R-type instructions then assemble it by OP+RS+RT+ZERO+RD*/
- if(subins[i][1].equals("add") || subins[i][1].equals("nand"))
- {
- ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+"0000000000000"+dec2bin(Integer.parseInt(subins[i][4])), 2));
- }
- /*If get J-type instructions then assemble it by OP+RS+RT+ZERO*/
- else if(subins[i][1].equals("jalr"))
- {
- ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+"0000000000000000", 2));
- }
- /*If get I-type instructions*/
- else if(subins[i][1].equals("lw") || subins[i][1].equals("sw") || subins[i][1].equals("beq"))
- {
- /*If it contain offset number then assemble it by OP+RS+RT+OFFSET*/
- if(isInteger(subins[i][4]))
- {
- if(Integer.parseInt(offset(subins[i][4]),2) < 32768 && Integer.parseInt(offset(subins[i][4]),2) > -32768)
- {
- ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+offset(subins[i][4]), 2));
- }
- /*If offset is more than 16-bit binary then return an error log*/
- else
- {
- System.out.println("Error Offset has more than 16 bits at line "+(i+1));
- }
- }
- /*If it contain a symbolic address then find its address*/
- else
- {
- boolean foundLabel = false;
- for ( int j = 0 ; j < counter ; j++ ) {
- if(subins[i][4].equals(subins[j][0]))
- {
- foundLabel = true;
- /*For beq instruction assemble by OP+RS+RT+OFFSET that from j-1-i for jump's reason*/
- if(subins[i][1].equals("beq"))
- ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+offset(String.valueOf(j-1-i)), 2));
- /*For lw and sw instruction assemble by OP+RS+RT+OFFSET that from j*/
- else
- ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+offset(String.valueOf(j)), 2));
- }
- }
- /*Displaying the error message if not found label*/
- if(!foundLabel)
- {
- System.out.println("Error Used Undefine Label at line "+(i+1));
- }
- }
- }
- else
- {
- System.out.println("OPCODE ERROR AT LINE "+(i+1));
- }
- }
- }
- /*Displaying the result machine code*/
- System.out.println(ins[i]);
- }
- bufferReader.close(); //Closing the input file
- }
- /*Converting decimal int to 3-bit binary string and return if error found*/
- public static String dec2bin(int dec)
- {
- switch(dec)
- {
- case 0 :
- return "000";
- case 1 :
- return "001";
- case 2 :
- return "010";
- case 3 :
- return "011";
- case 4 :
- return "100";
- case 5 :
- return "101";
- case 6 :
- return "110";
- case 7 :
- return "111";
- default:
- return "Wrong number of rs or rt";
- }
- }
- /*Converting opcode to binary string and return if error found*/
- public static String chkOP(String str)
- {
- switch (str)
- {
- case "add":
- return "0000000000";
- case "nand":
- return "0000000001";
- case "lw":
- return "0000000010";
- case "sw":
- return "0000000011";
- case "beq":
- return "0000000100";
- case "jalr":
- return "0000000101";
- case "halt":
- return String.valueOf(Integer.parseInt("00000001100000000000000000000000", 2));
- case "noop":
- return String.valueOf(Integer.parseInt("00000001110000000000000000000000", 2));
- default:
- return "Wrong opcode";
- }
- }
- /*Checking number string return true when string is numeric*/
- public static boolean isInteger(String str)
- {
- if (str == null)
- {
- return false;
- }
- int length = str.length();
- if (length == 0)
- {
- return false;
- }
- int i = 0;
- if (str.charAt(0) == '-')
- {
- if (length == 1)
- {
- return false;
- }
- i = 1;
- }
- for (; i < length; i++)
- {
- char c = str.charAt(i);
- if (c <= '/' || c >= ':')
- {
- return false;
- }
- }
- return true;
- }
- /*Converting string to two's complement binary string*/
- public static String offset(String str)
- {
- int dec = Integer.valueOf(str);
- if (dec < 0)
- return String.format("%16s", Integer.toString(65536 + dec,2)).replace(' ', '0');
- else
- return String.format("%16s", Integer.toString(dec,2)).replace(' ', '0');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement