sidrs

Assembler Pass 2

Jul 17th, 2025
302
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.FileWriter;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.LinkedHashMap;
  9. import java.util.Map;
  10. import java.util.StringTokenizer;
  11.  
  12. import java.util.*;
  13.     class Tuple {
  14.         //m_class specifies class of the mnemonic such as IS, DL, or AD
  15.         String mnemonic, m_class, opcode;
  16.         int length;
  17.        
  18.         Tuple() {}
  19.        
  20.         Tuple(String s1, String s2, String s3, String s4) {
  21.             mnemonic = s1;
  22.             m_class = s2;
  23.             opcode = s3;
  24.             length = Integer.parseInt(s4);
  25.            
  26.         }
  27.     }
  28.  
  29.     class SymTuple {
  30.         String symbol, address, length;
  31.        
  32.         SymTuple(String s1, String s2, String i1) {
  33.             symbol = s1;
  34.             address = s2;
  35.             length = i1;
  36.         }
  37.     }
  38.  
  39.     class LitTuple {
  40.         String literal, address, length;
  41.        
  42.         LitTuple() {}
  43.        
  44.         LitTuple(String s1,  String s2, String i1) {
  45.             literal = s1;
  46.             address = s2;
  47.             length = i1;       
  48.         }
  49.     }
  50.    
  51.     public class Assembler_PassTwo {
  52.        
  53.     static int lc,iSymTabPtr=0, iLitTabPtr=0, iPoolTabPtr=0;
  54.     static int poolTable[] = new int[10];
  55.     static Map<String,Tuple> MOT;
  56.     static ArrayList<SymTuple> symtable;
  57.     static ArrayList<LitTuple> littable;
  58.     static Map<String, String> regAddressTable;
  59.     static PrintWriter out_pass2;
  60.  
  61.     static void initiallizeTables() throws Exception{
  62.         symtable = new ArrayList<>();
  63.         littable = new ArrayList<>();
  64.         regAddressTable = new HashMap<>();
  65.         //MOT = new HashMap<>();
  66.         String s;
  67.         BufferedReader br;
  68.         br = new BufferedReader(new InputStreamReader(new FileInputStream("/home/student/Downloads/PASS I ASSEMBLER-20240710T090734Z-001/PASS II ASSEMBLER/Assembler_PassTwo/symtable.txt")));
  69.         while((s = br.readLine()) != null) {
  70.             StringTokenizer st = new StringTokenizer(s, "\t", false);
  71.             symtable.add(new SymTuple(st.nextToken(), st.nextToken(), ""));
  72.         }
  73.         br.close();
  74.         br = new BufferedReader(new InputStreamReader(new FileInputStream("/home/student/Downloads/PASS I ASSEMBLER-20240710T090734Z-001/PASS II ASSEMBLER/Assembler_PassTwo/littable.txt")));
  75.         while((s = br.readLine()) != null) {
  76.             StringTokenizer st = new StringTokenizer(s, "\t", false);
  77.             littable.add(new LitTuple(st.nextToken(), st.nextToken(), ""));
  78.         }
  79.         br.close();
  80.         //Initiallize register address table
  81.         regAddressTable.put("AREG", "1");
  82.         regAddressTable.put("BREG", "2");
  83.         regAddressTable.put("CREG", "3");
  84.         regAddressTable.put("DREG", "4");
  85.     }
  86.    
  87.     static void pass2() throws Exception{
  88.         BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("/home/student/Downloads/PASS I ASSEMBLER-20240710T090734Z-001/PASS II ASSEMBLER/Assembler_PassTwo/output_pass1.txt")));
  89.         out_pass2 = new PrintWriter(new FileWriter("/home/student/Downloads/PASS I ASSEMBLER-20240710T090734Z-001/PASS II ASSEMBLER/Assembler_PassTwo/output_pass2.txt"), true);
  90.         String s;
  91.        
  92.         //Read from intermediate file one line at a time
  93.         while((s = input.readLine()) != null) {
  94.             //Replace all ( and ) characters by a blank string
  95.             s=s.replaceAll("(\\()", " ");
  96.             s=s.replaceAll("(\\))", " ");
  97.  
  98.             //For each line, separate out the tokens
  99.             String ic_tokens[] = tokenizeString(s, " ");
  100.             if(ic_tokens == null || ic_tokens.length==0){
  101.                 continue;
  102.             }
  103.             String output_str = "";
  104.                        
  105.             //Second token contains mnemonic class and opcode
  106.             String mnemonic_class = ic_tokens[1];
  107.             //Separate the mnemonic and its opcode which are separated by a comma
  108.             String m_tokens[] = tokenizeString(mnemonic_class, ",");
  109.            
  110.             //Write the second token as is in the output file
  111.              
  112.             if(m_tokens[0].equalsIgnoreCase("IS")){
  113.                 //First token is location counter which will be output as it is
  114.                 output_str += ic_tokens[0] + " ";
  115.                 //Output the opcode of the instruction
  116.                 output_str += m_tokens[1] + " ";
  117.                 String opr_tokens[];
  118.                 for(int i = 2; i <ic_tokens.length; i++){
  119.                     opr_tokens = tokenizeString(ic_tokens[i], ",");
  120.                     if(opr_tokens[0].equalsIgnoreCase("RG")){
  121.                         output_str += opr_tokens[1] + " ";
  122.                     }
  123.                     else if(opr_tokens[0].equalsIgnoreCase("S")){
  124.                         int index = Integer.parseInt(opr_tokens[1]);
  125.                         output_str += symtable.get(index).address + " ";
  126.                     }
  127.                     else if(opr_tokens[0].equalsIgnoreCase("L")){
  128.                         int index = Integer.parseInt(opr_tokens[1]);
  129.                         output_str += littable.get(index).address + " ";
  130.                     }
  131.                 }
  132.             }
  133.             else if(m_tokens[0].equalsIgnoreCase("DL")){
  134.                 //First token is location counter which will be output as it is
  135.                 output_str += ic_tokens[0] + " ";
  136.                 if(m_tokens[1].equalsIgnoreCase("02")){
  137.                     //Process for operands of mnemonic DC
  138.                     String opr_tokens[] = tokenizeString(ic_tokens[2], ",");
  139.                     output_str += "00 00 " + opr_tokens[1] + " ";
  140.                 }
  141.             }
  142.             System.out.println(output_str);
  143.             out_pass2.println(output_str);
  144.            
  145.         }
  146.     }
  147.  
  148.     static String[] tokenizeString(String str, String separator){
  149.         StringTokenizer st = new StringTokenizer(str, separator, false);
  150.         //Construct an array of the separated tokens
  151.         String s_arr[] = new String[st.countTokens()];
  152.         for(int i=0 ; i < s_arr.length ; i++) {
  153.             s_arr[i] = st.nextToken();
  154.         }
  155.         return s_arr;
  156.     }
  157.     public static void main(String[] args) throws Exception {
  158.         initiallizeTables();
  159.         pass2();
  160.     }
  161. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment