Advertisement
ptrawt

[CompArch] Ass2MacV2.5

Nov 26th, 2014
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.19 KB | None | 0 0
  1. /*
  2.     Ass2Mac class for assembling the assembly code to the machine code.
  3.  */
  4. import java.io.*;
  5.  
  6. public class Ass2Mac
  7. {
  8.         public static int counter = 0;
  9.  
  10.         /*File to be executed*/
  11.         // public static String fileName="input.txt";
  12.         public static String fileName="mult.txt";
  13.         // public static String fileName="comb2.txt";
  14.         // public static String fileName="sum.txt";
  15.  
  16.         public static void main(String[] args) throws IOException
  17.         {
  18.             int maxIns = 1000;                                     //Declaring maximum of instructions
  19.             String[] ins = new String[maxIns];
  20.  
  21.             /*Reading the executed file*/
  22.             String line;
  23.             FileReader inputFile = new FileReader(fileName);
  24.             BufferedReader bufferReader = new BufferedReader(inputFile);
  25.             while ((line = bufferReader.readLine()) != null)
  26.             {
  27.                 ins[counter] = line;                               //Declaring instructions arrays
  28.                 counter++;                                         //Instruction Counting
  29.             }
  30.  
  31.             int maxCol = 100;                                       //Declaring maximum of instruction array's Column
  32.  
  33.             /*Declaring 2D arrays that split instructions to column*/
  34.             String[][] subins = new String[counter][maxCol];
  35.  
  36.             for (int i = 0; i < counter ; i++ )
  37.             {
  38.                 subins[i] = ins[i].split("\\s+");                   //Spliting by whitespace
  39.             }
  40.  
  41.             /*Assembling the assembly to machine codes*/
  42.             for (int i = 0; i < counter ; i++ )
  43.             {
  44.                 /*Checking for ".fill" instructions*/
  45.                 if(subins[i][1].equals(".fill"))
  46.                 {
  47.                     /*If found the Label then check for duplicate lable at another lines*/
  48.                     for(int j = 0 ; j < counter ; j++)
  49.                     {
  50.                         if(subins[j][1].equals(".fill") && subins[i][0].equals(subins[j][0]) && i != j)
  51.                         {
  52.                             System.out.println("Duplicate Label at line"+(j+1));
  53.                         }
  54.                     }
  55.                    
  56.                     /*If .fill instructions contain number then keep the number and get rid of others */
  57.                     if(isInteger(subins[i][2]))
  58.                     {
  59.                         ins[i] = subins[i][2];
  60.                     }
  61.                     /*If .fill instructions contain a symbolic address then keep its address*/
  62.                     else
  63.                     {
  64.                         for(int j = 0 ; j < counter ; j++)
  65.                         {
  66.                             if(subins[i][2].equals(subins[j][0]))
  67.                             {
  68.                                 ins[i] = ""+j;
  69.                             }
  70.                         }
  71.                     }
  72.                 }
  73.                 /*To check instructions*/
  74.                 else
  75.                 {
  76.                     /*Passing the split word to get its OP*/
  77.                     String op = chkOP(subins[i][1]);
  78.                     /*If get O-type instructions then immediately assemble it to its number*/
  79.                     if(subins[i][1].equals("noop") || subins[i][1].equals("halt")){
  80.                         ins[i] = op;
  81.                     }
  82.                     else
  83.                     {
  84.                         /*If get R or I or J type instructions then split the word for rs and rt */
  85.                         String rs = dec2bin(Integer.parseInt(subins[i][2]));
  86.                         String rt = dec2bin(Integer.parseInt(subins[i][3]));
  87.                         /*If get R-type instructions then assemble it by OP+RS+RT+ZERO+RD*/
  88.                         if(subins[i][1].equals("add") || subins[i][1].equals("nand"))
  89.                         {
  90.                             ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+"0000000000000"+dec2bin(Integer.parseInt(subins[i][4])), 2));
  91.                         }
  92.                         /*If get J-type instructions then assemble it by OP+RS+RT+ZERO*/
  93.                         else if(subins[i][1].equals("jalr"))
  94.                         {
  95.                             ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+"0000000000000000", 2));
  96.                         }
  97.                         /*If get I-type instructions*/
  98.                         else if(subins[i][1].equals("lw") || subins[i][1].equals("sw") || subins[i][1].equals("beq"))
  99.                         {
  100.                             /*If it contain offset number then assemble it by OP+RS+RT+OFFSET*/
  101.                             if(isInteger(subins[i][4]))
  102.                             {
  103.                                 if(Integer.parseInt(offset(subins[i][4]),2) < 32768 && Integer.parseInt(offset(subins[i][4]),2) > -32768)
  104.                                 {
  105.                                     ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+offset(subins[i][4]), 2));
  106.                                 }
  107.                                 /*If offset is more than 16-bit binary then return an error log*/
  108.                                 else
  109.                                 {
  110.                                     System.out.println("Error Offset has more than 16 bits at line "+(i+1));
  111.                                 }
  112.                             }
  113.                             /*If it contain a symbolic address then find its address*/
  114.                             else
  115.                             {
  116.                                 boolean foundLabel = false;
  117.                                 for ( int j = 0 ; j < counter ; j++ ) {
  118.                                     if(subins[i][4].equals(subins[j][0]))
  119.                                     {
  120.                                         foundLabel = true;
  121.                                         /*For beq instruction assemble by OP+RS+RT+OFFSET that from j-1-i for jump's reason*/
  122.                                         if(subins[i][1].equals("beq"))
  123.                                             ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+offset(String.valueOf(j-1-i)), 2));
  124.                                         /*For lw and sw instruction assemble by OP+RS+RT+OFFSET that from j*/
  125.                                         else
  126.                                             ins[i] = String.valueOf(Integer.parseInt(op+rs+rt+offset(String.valueOf(j)), 2));
  127.                                     }
  128.                                 }
  129.                                
  130.                                 /*Displaying the error message if not found label*/
  131.                                 if(!foundLabel)
  132.                                 {
  133.                                     System.out.println("Error Used Undefine Label at line "+(i+1));
  134.                                 }
  135.                             }
  136.                         }
  137.                         else
  138.                         {
  139.                             System.out.println("OPCODE ERROR AT LINE "+(i+1));
  140.                         }
  141.                     }
  142.                 }
  143.                 /*Displaying the result machine code*/
  144.                 System.out.println(ins[i]);
  145.             }
  146.             bufferReader.close();               //Closing the input file
  147.         }
  148.  
  149.         /*Converting decimal int to 3-bit binary string and return if error found*/
  150.         public static String dec2bin(int dec)
  151.         {
  152.                 switch(dec)
  153.                 {
  154.                         case 0 :
  155.                                 return "000";
  156.                         case 1 :
  157.                                 return "001";
  158.                         case 2 :
  159.                                 return "010";
  160.                         case 3 :
  161.                                 return "011";
  162.                         case 4 :
  163.                                 return "100";
  164.                         case 5 :
  165.                                 return "101";
  166.                         case 6 :
  167.                                 return "110";
  168.                         case 7 :
  169.                                 return "111";
  170.                         default:
  171.                                 return "Wrong number of rs or rt";
  172.                 }
  173.         }
  174.        
  175.         /*Converting opcode to binary string and return if error found*/
  176.         public static String chkOP(String str)
  177.         {
  178.                 switch (str)
  179.                 {
  180.                         case "add":
  181.                                 return "0000000000";
  182.                         case "nand":
  183.                                 return "0000000001";
  184.                         case "lw":
  185.                                 return "0000000010";
  186.                         case "sw":
  187.                                 return "0000000011";
  188.                         case "beq":
  189.                                 return "0000000100";
  190.                         case "jalr":
  191.                                 return "0000000101";
  192.                         case "halt":
  193.                                 return String.valueOf(Integer.parseInt("00000001100000000000000000000000", 2));
  194.                         case "noop":
  195.                                 return String.valueOf(Integer.parseInt("00000001110000000000000000000000", 2));
  196.  
  197.                         default:
  198.                                 return "Wrong opcode";
  199.                 }
  200.         }
  201.  
  202.         /*Checking number string return true when string is numeric*/
  203.         public static boolean isInteger(String str)
  204.         {
  205.             if (str == null)
  206.             {
  207.                 return false;
  208.             }
  209.             int length = str.length();
  210.             if (length == 0)
  211.             {
  212.                 return false;
  213.             }
  214.             int i = 0;
  215.             if (str.charAt(0) == '-')
  216.             {
  217.                 if (length == 1)
  218.                 {
  219.                     return false;
  220.                 }
  221.                 i = 1;
  222.             }
  223.             for (; i < length; i++)
  224.             {
  225.                 char c = str.charAt(i);
  226.                 if (c <= '/' || c >= ':')
  227.                 {
  228.                     return false;
  229.                 }
  230.             }
  231.             return true;
  232.         }
  233.  
  234.         /*Converting string to two's complement binary string*/
  235.         public static String offset(String str)
  236.         {
  237.             int dec = Integer.valueOf(str);
  238.             if (dec < 0)
  239.                 return String.format("%16s", Integer.toString(65536 + dec,2)).replace(' ', '0');
  240.             else
  241.                 return String.format("%16s", Integer.toString(dec,2)).replace(' ', '0');
  242.         }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement