literal

 import java.util.*;


class Literal {

    String value;

    int address;


    public Literal(String value) {

        this.value = value;

        this.address = -1; // not assigned yet

    }

}


public class Ass2{


    // Check if a literal already exists in the list

    public static boolean literalExists(List<Literal> list, String value) {

        for (Literal l : list) {

            if (l.value.equals(value)) return true;

        }

        return false;

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        List<String> code = new ArrayList<>();

        List<Literal> literalTable = new ArrayList<>();

        List<Literal> literalPool = new ArrayList<>();


        int locationCounter = 0;

        boolean startFound = false;


        System.out.println("Enter Assembly code (type 'END' alone on a line to finish):");


        // Read code from terminal

        while (true) {

            String line = scanner.nextLine().trim();

            if (line.equalsIgnoreCase("END")) {

                code.add("END");

                break;

            }

            if (!line.isEmpty()) {

                code.add(line);

            }

        }


        // --- PASS 1 ---

        for (String stmt : code) {

            String[] tokens = stmt.trim().split("\\s+");


            if (tokens.length == 0) continue;


            if (tokens[0].equalsIgnoreCase("START")) {

                if (tokens.length > 1) {

                    locationCounter = Integer.parseInt(tokens[1]);

                    startFound = true;

                    continue;

                } else {

                    System.out.println("Error: START must be followed by an address.");

                    return;

                }

            }


            if (!startFound) {

                System.out.println("Error: START not found.");

                return;

            }


            // Assign literals on LTORG or END

            if (tokens[0].equalsIgnoreCase("LTORG") || tokens[0].equalsIgnoreCase("END")) {

                for (Literal l : literalPool) {

                    l.address = locationCounter;

                    literalTable.add(l);

                    locationCounter++;

                }

                literalPool.clear();

                if (tokens[0].equalsIgnoreCase("END")) break;

                continue;

            }


            // Look for literals (starting with =')

            for (String token : tokens) {

                if (token.startsWith("='")) {

                    if (!literalExists(literalPool, token) && !literalExists(literalTable, token)) {

                        literalPool.add(new Literal(token));

                    }

                }

            }


            locationCounter++;

        }


        // --- PRINTING LITERAL TABLE ---

        System.out.println("\n--- LITERAL TABLE ---");

        System.out.printf("%-10s %-10s %-10s\n", "Index", "Literal", "Address");

        for (int i = 0; i < literalTable.size(); i++) {

            Literal l = literalTable.get(i);

            System.out.printf("%-10d %-10s %-10d\n", i, l.value, l.address);

        }

    }

}

#ip
START 200
MOVER AREG, ='5'
MOVEM AREG, ='1'
LTORG
ADD BREG, ='2'
SUB CREG, ='3'
END

Comments

Popular posts from this blog

yacc evaluate

built in functions