SlideShare a Scribd company logo
1 of 17
Download to read offline
COMP360 Assembler Write an assembler that reads the source code of an assembly program
(for an imaginary machine) and displays the machine language for that program. The following
assembler instructions must be supported. Instruction opcode Length Format Example add add
R1, R2, R3 sub R1, R2, R3 mult mult RI, R2, R3 I ld R1, addr R7) la R1, addr (RTT st R1, addr
R71 jump addr R71 R1, addr R71 3 push R1 Machine language instructions have the following
formats: 0: arithmetic Lopcode Reg I Reg 2 Reg3 4bits 4 bits 4 bits 1: Load, store. conditional
jump Dopcode eg index ad 4 bits 4 bits 4 bits 20 bits 2: call, jump de unused index addr 4 bits 4
bits 4 bits 20 bits 3: push, pop, lpsw. spsw opcode Reg 4 bits 4 bits 4: rtn opcode unused. 4 bits 4
bits
Solution
//First Create Package Collection then create all classes mentioned below
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;
/**
*
* @author Rashmi Tiwari
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Assembler {
public static final String COMP_ADD = "ADD";
public static final String COMP_SUB = "SUB";
public static final String COMP_LOAD = "LOAD";
public static final String COMP_STORE = "STORE";
public static final String COMP_GOTO = "GOTO";
public static final String COMP_GOTOZ = "GOTOZ";
public static final String COMP_GOTOP = "GOTOP";
public static final String COMP_READ = "READ";
public static final String COMP_WRITE = "WRITE";
public static final String COMP_STOP = "STOP";
private static Map assemblyTable;
private static Map getAssemblyTable() {
if (assemblyTable == null) {
assemblyTable = new HashMap();
assemblyTable.put(COMP_ADD, Comp360Assembler.COMP_ADD);
assemblyTable.put(COMP_SUB, Comp360Assembler.COMP_SUB);
assemblyTable.put(COMP_LOAD, Comp360Assembler.COMP_LOAD);
assemblyTable.put(COMP_STORE, Comp360Assembler.COMP_STORE);
assemblyTable.put(COMP_GOTO, Comp360Assembler.COMP_GOTO);
assemblyTable.put(COMP_GOTOZ, Comp360Assembler.COMP_GOTOZ);
assemblyTable.put(COMP_GOTOP, Comp360Assembler.COMP_GOTOP);
assemblyTable.put(COMP_READ, Comp360Assembler.COMP_READ);
assemblyTable.put(COMP_WRITE, Comp360Assembler.COMP_WRITE);
assemblyTable.put(COMP_STOP, Comp360Assembler.COMP_STOP);
}
return assemblyTable;
}
public static int[] assemble(String program) throws Comp360ParsingException {
if (program == null) return null;
program = preProcess(program);
Map symTable = buildSymTable(program);
return translateCommands(program, symTable);
}
public static String preProcess(String text) throws Comp360ParsingException {
text = text.replaceAll("(?ms)/*.*?*/", "");
text = text.replaceAll("(?m)//.*$", "");
text = text.replaceAll("(?m)#.*$", "");
text = text.replaceAll("(?m)'.*$", "");
text = text.replaceAll("(?m)`.*$", "");
return text;
}
private static int[] translateCommands(String program, Map symTable) throws
Comp360ParsingException {
ArrayList code = new ArrayList();
Scanner scan = new Scanner(program);
int lineNumber = 0;
int opNumber = 0;
while (scan.hasNextLine()) {
lineNumber++;
String line = scan.nextLine();
Scanner tokenizer = new Scanner(line);
if (!tokenizer.hasNext())
continue; // blank line
String token = tokenizer.next();
// IL position 2
if (isLineLabel(token)) {
if (!tokenizer.hasNext())
continue;
token = tokenizer.next();
}
// IL position 4
if (isInstruction(token)) {
token = token.toUpperCase();
Integer opCode = getAssemblyTable().get(token);
Integer address = 0;
if (!token.equals(COMP_STOP) && !token.equals(COMP_READ) &&
!token.equals(COMP_WRITE)) {
token = tokenizer.next();
if (!isNumber(token)) {
token = token.toUpperCase();
address = symTable.get(token);
} else {
address = Integer.parseInt(token);
}
}
code.add(opCode * 100 + address);
opNumber++;
}
}
return RunTools.deboxInteger(code);
}
public static Map buildSymbols(String program) throws Comp360ParsingException {
return buildSymTable(preProcess(program));
}
private static Map buildSymTable(String program) throws Comp360ParsingException {
Map lineLabels = new HashMap();
Map variables = new HashMap();
variables.put("ZERO", 98);
variables.put("ONE", 99);
Scanner scan = new Scanner(program);
int nextVariable = 50;
int lineNumber = 0;
int opNumber = 0;
while (scan.hasNextLine()) {
lineNumber++;
String line = scan.nextLine();
Scanner tokenizer = new Scanner(line);
if (!tokenizer.hasNext())
continue;
String token = tokenizer.next();
// IL position 2
if (isLineLabel(token)) {
token = token.substring(0, token.length() - 1);
if (lineLabels.get(token) != null)
throw new Comp360ParsingException("Duplicate line label "+token+"
encountered on line "+lineNumber);
lineLabels.put(token, opNumber);
if (!tokenizer.hasNext())
continue;
token = tokenizer.next();
}
// IL position 4
if (isInstruction(token)) {
token = token.toUpperCase();
if (!token.equals(COMP_STOP) && !token.equals(COMP_READ) &&
!token.equals(COMP_WRITE)) {
if (!tokenizer.hasNext())
throw new Comp360ParsingException("Unexpected end of line on line
"+lineNumber);
token = tokenizer.next();
if (!isNumber(token)) {
token = token.toUpperCase();
if (!variables.containsKey(token))
variables.put(token, nextVariable++);
} else {
int address = Integer.parseInt(token);
if (address > 99 || address < 0)
throw new Comp360ParsingException("Instruction addresses must be at most
two digits");
}
}
opNumber++;
if (!tokenizer.hasNext())
continue;
token = tokenizer.next();
}
throw new Comp360ParsingException("Expected end of line on line "+lineNumber);
}
if (opNumber > 49)
throw new Comp360ParsingException("Too many operations. Programs must have
fewer than 50 instructions");
if (nextVariable >= 98)
throw new Comp360ParsingException("Too many variables in program. Must be less
than 48");
variables.putAll(lineLabels); // line labels should have precedent over variables
return variables;
}
public static boolean isNumber(String token) {
try {
Integer.parseInt(token);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static boolean isInstruction(String token) {
return token != null && getAssemblyTable().containsKey(token.toUpperCase());
}
public static boolean isLineLabel(String token) {
return token != null && token.endsWith(":") && token.length() > 1;
}
public static boolean isSymbol(String token) {
return token.matches("^[a-zA-Z_][w]*$");
}
}
//Comp360Assembler class is in same package of Collection
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;
/**
*
* @author Rashmi Tiwari
*/
public interface Comp360Assembler extends Runnable{
public static final int DEFAULT_DATA = 0;
public static final int MIN_INT = -999;
public static final int MAX_INT = 999;
public static final int MEMORY_SIZE = 100;
public static final int PROGRAM_INSERTION_POINT = 0;
public static final int INSTRUCTION_SIZE = 1;
public static final int COMMAND = 0;
public static final int ADDRESS = 1;
public static final int COMP_STOP = 0;
public static final int COMP_ADD = 1;
public static final int COMP_SUB = 2;
public static final int COMP_LOAD = 3;
public static final int COMP_STORE = 4;
public static final int COMP_GOTO = 5;
public static final int COMP_GOTOZ = 6;
public static final int COMP_GOTOP = 7;
public static final int COMP_READ = 8;
public static final int COMP_WRITE = 9;
public void reset();
public void flushIO();
public String getBadStateReason();
public void fetch();
public int[] decode();
public void execute(int command, int address);
public void step();
public void run();
public boolean isRunning();
public int readOutput();
public void addInput(int data);
public int[] memDump();
public void loadMemDump(int memDump[]);
public int getIr();
public int getDr();
public int getPc();
}
//Custom Exception Class
package collection;
/**
*
* @author Rashmi Tiwari
*/
public class Comp360ParsingException extends Exception{
private static final long serialVersionUID = 1L;
public Comp360ParsingException(String message) {
super(message);
}
}
//Runner Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
*
* @author Rashmi Tiwari
*/
public class Runner {
/**
* Special output returned if a program timed out
*/
public static final int[] TIMED_OUT = {1000};
private static final long RUNNER_TIMEOUT = 10000;
public static int[] runProgram(Comp360Assembler comp360, int[] program, int[] input) {
program = RunTools.loadProgram(program);
comp360.reset();
comp360.flushIO();
comp360.loadMemDump(program);
for (int data : input)
comp360.addInput(data);
// run the program
Future programRunner = Executors.newSingleThreadExecutor().submit(comp360);
try {
programRunner.get(RUNNER_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (ExecutionException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (TimeoutException e) {
programRunner.cancel(true);
return TIMED_OUT.clone();
}
return RunTools.extractOutput(comp360);
}
}
//RunTool Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
/**
*
* @author Rashmi Tiwari
*/
public class RunTools {
private static int[] blankMemory;
public static int[] loadProgram(int[] program) {
if (program == null)
return getBlankMemory().clone();
if (program.length == Comp360Assembler.MEMORY_SIZE)
return program; // no need to load it into a memory dump
if (program.length > Comp360Assembler.MEMORY_SIZE)
return null;
int[] newProgram = getBlankMemory().clone();
System.arraycopy(program, 0, newProgram, 0, program.length);
return newProgram;
}
public static int[] extractOutput(Comp360Assembler comp360) {
List outputList = new LinkedList();
while (true) {
try {
outputList.add(comp360.readOutput());
} catch (NoSuchElementException e) {
// finished reading output
break;
}
}
return deboxInteger(outputList);
}
private static int[] getBlankMemory() {
if (blankMemory == null) {
blankMemory = new int[Comp360Assembler.MEMORY_SIZE];
Arrays.fill(blankMemory, 0);
blankMemory[98] = 0; // write-protected constant
blankMemory[99] = 1; // write-protected constant
}
return blankMemory;
}
static int[] deboxInteger(List a) {
if (a == null) return null;
int[] result = new int[a.size()];
Iterator iter = a.iterator();
for (int i = 0; i < result.length; i++) {
result[i] = iter.next();
}
return result;
}
}
//Core Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;
import java.util.Arrays;
import java.util.LinkedList;
/**
*
* @author Rashmi Tiwari
*/
public class Core {
private static final int DIGITS = 3;
private int[] mem;
private int ir;
private int dr;
private int pc;
private LinkedList stdin;
private LinkedList stdout;
private String badState;
private boolean running;
/**
* Constructs a new VIC object with default state
*/
public Core() {
stdin = new LinkedList();
stdout = new LinkedList();
mem = new int[Comp360Assembler.MEMORY_SIZE];
reset();
resetMemory();
flushIO();
}
public void reset() {
pc = Comp360Assembler.PROGRAM_INSERTION_POINT;
ir = Comp360Assembler.DEFAULT_DATA;
dr = Comp360Assembler.DEFAULT_DATA;
badState = null;
running = false;
}
private void resetMemory() {
Arrays.fill(mem, 0);
mem[98] = 0; // write protected constants ZERO & ONE
mem[99] = 1;
}
private boolean inBadState() {
return badState != null;
}
public void fetch() {
if (inBadState())
return;
ir = mem[pc];
pc += Comp360Assembler.INSTRUCTION_SIZE;
}
public int[] decode() {
if (inBadState())
return null;
if (!isProperInstruction(ir)) {
badState("Malformed Instruction at " + pc + ":" + ir);
return null;
}
// extract the nth digit of the IR, and dispatch it with the rest of the digits
int decode[] = new int[2];
decode[Comp360Assembler.COMMAND] = extractDigitN(ir, DIGITS);
decode[Comp360Assembler.ADDRESS] = ir % (int) (Math.pow(10, DIGITS - 1));
return decode;
}
private boolean isProperInstruction(int ir) {
return Comp360Assembler.MAX_INT >= ir && ir >= 0;
}
private int extractDigitN(int data, int n) {
return (data / (int) (Math.pow(10, n - 1))) % 10;
}
public void execute(int command, int address) {
if (inBadState())
return;
switch (command) {
case Comp360Assembler.COMP_STOP:
running = false;
break;
case Comp360Assembler.COMP_ADD:
if (!isValidData(dr + mem[address]))
badState("Integer Overflow at " + pc + ":" + ir);
dr += mem[address];
break;
case Comp360Assembler.COMP_SUB:
if (!isValidData(dr - mem[address]))
badState("Integer underflow at " + pc + ":" + ir);
dr -= mem[address];
break;
case Comp360Assembler.COMP_LOAD:
dr = mem[address];
break;
case Comp360Assembler.COMP_STORE:
if (address == 98 || address == 99)
badState("Memory address "+address+" is write protected at "+pc+":"+ir);
mem[address] = dr;
break;
case Comp360Assembler.COMP_GOTO:
pc = address;
break;
case Comp360Assembler.COMP_GOTOZ:
if (dr == 0) {
pc = address;
}
break;
case Comp360Assembler.COMP_GOTOP:
if (dr > 0) {
pc = address;
}
break;
case Comp360Assembler.COMP_READ:
dr = readInput();
break;
case Comp360Assembler.COMP_WRITE:
stdout.add(dr);
break;
default:
badState("Bad VIC instruction: " + command + address);
}
}
private int readInput() {
if (!stdin.isEmpty())
return stdin.remove();
else {
badState("No more input at " + pc);
return Comp360Assembler.DEFAULT_DATA;
}
}
private void badState(String reason) {
running = false;
badState = reason;
}
public String getBadStateReason() {
return this.badState;
}
public void step() {
if (inBadState())
return;
fetch();
int decode[] = decode();
execute(decode[Comp360Assembler.COMMAND],
decode[Comp360Assembler.ADDRESS]);
}
public void run() {
running = true;
while (running && !inBadState()) {
step();
}
}
public void addInput(int data) {
if (isValidData(data))
stdin.add(data);
}
private boolean isValidData(int data) {
return Comp360Assembler.MAX_INT >= data && data >=
Comp360Assembler.MIN_INT;
}
public int readOutput() {
return stdout.remove();
}
public int[] memDump() {
return mem.clone();
}
public void loadMemDump(int memDump[]) {
if (memDump.length != Comp360Assembler.MEMORY_SIZE)
return; // not a proper memdump
for (int i = 0; i < Comp360Assembler.MEMORY_SIZE; i++)
if (!isValidData(memDump[i])) {
return; // not a proper memdump
}
// NOTE: ignores the last 2 cells in the mem-dump since they are
// supposed to be constant, write protected cells.
System.arraycopy(memDump, 0, mem, 0, Comp360Assembler.MEMORY_SIZE - 2);
}
public int getIr() {
return ir;
}
public int getDr() {
return dr;
}
public int getPc() {
return pc;
}
public void flushIO() {
stdin.clear();
stdout.clear();
}
public boolean isRunning() {
return running;
}
protected void setDr(int dr) {
this.dr = dr;
}
}

More Related Content

Similar to COMP360 Assembler Write an assembler that reads the source code of an.pdf

C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
NetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionNetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionPaulo Morgado
 
Intel JIT Talk
Intel JIT TalkIntel JIT Talk
Intel JIT Talkiamdvander
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdfWere writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdffsenterprises
 
Shell to be modified#include stdlib.h #include unistd.h .pdf
Shell to be modified#include stdlib.h #include unistd.h .pdfShell to be modified#include stdlib.h #include unistd.h .pdf
Shell to be modified#include stdlib.h #include unistd.h .pdfclarityvision
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Analysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsAnalysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsPVS-Studio
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Max Kleiner
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonRoss McDonald
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
Compiler Design and Construction COSC 5353Project Instructions -
Compiler Design and Construction COSC 5353Project Instructions -Compiler Design and Construction COSC 5353Project Instructions -
Compiler Design and Construction COSC 5353Project Instructions -LynellBull52
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxssuser454af01
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineMatt Provost
 

Similar to COMP360 Assembler Write an assembler that reads the source code of an.pdf (20)

C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
NetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionNetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf Edition
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
Intel JIT Talk
Intel JIT TalkIntel JIT Talk
Intel JIT Talk
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdfWere writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdf
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
Shell to be modified#include stdlib.h #include unistd.h .pdf
Shell to be modified#include stdlib.h #include unistd.h .pdfShell to be modified#include stdlib.h #include unistd.h .pdf
Shell to be modified#include stdlib.h #include unistd.h .pdf
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Analysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsAnalysis of Microsoft Code Contracts
Analysis of Microsoft Code Contracts
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPython
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Compiler Design and Construction COSC 5353Project Instructions -
Compiler Design and Construction COSC 5353Project Instructions -Compiler Design and Construction COSC 5353Project Instructions -
Compiler Design and Construction COSC 5353Project Instructions -
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 

More from fazalenterprises

Brett Donovan was the manager at Waltons Diner. He planned to promo.pdf
Brett Donovan was the manager at Waltons Diner. He planned to promo.pdfBrett Donovan was the manager at Waltons Diner. He planned to promo.pdf
Brett Donovan was the manager at Waltons Diner. He planned to promo.pdffazalenterprises
 
Being larger than the right ventricle, the left ventricle pumps more .pdf
Being larger than the right ventricle, the left ventricle pumps more .pdfBeing larger than the right ventricle, the left ventricle pumps more .pdf
Being larger than the right ventricle, the left ventricle pumps more .pdffazalenterprises
 
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdfC# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdffazalenterprises
 
A number of philosophers held various versions of the view that lang.pdf
A number of philosophers held various versions of the view that lang.pdfA number of philosophers held various versions of the view that lang.pdf
A number of philosophers held various versions of the view that lang.pdffazalenterprises
 
Why phelogyny has to be this way not other way aroundExaplain bas.pdf
Why phelogyny has to be this way not other way aroundExaplain bas.pdfWhy phelogyny has to be this way not other way aroundExaplain bas.pdf
Why phelogyny has to be this way not other way aroundExaplain bas.pdffazalenterprises
 
Which of the following are tertiary activities A. Lead mining.pdf
Which of the following are tertiary activities A. Lead mining.pdfWhich of the following are tertiary activities A. Lead mining.pdf
Which of the following are tertiary activities A. Lead mining.pdffazalenterprises
 
What is John Tukeys relationship to the S language and to the R la.pdf
What is John Tukeys relationship to the S language and to the R la.pdfWhat is John Tukeys relationship to the S language and to the R la.pdf
What is John Tukeys relationship to the S language and to the R la.pdffazalenterprises
 
What do health care professionals need to remember when caring for p.pdf
What do health care professionals need to remember when caring for p.pdfWhat do health care professionals need to remember when caring for p.pdf
What do health care professionals need to remember when caring for p.pdffazalenterprises
 
What is the difference between a neurogenic and myogenic heart Plea.pdf
What is the difference between a neurogenic and myogenic heart Plea.pdfWhat is the difference between a neurogenic and myogenic heart Plea.pdf
What is the difference between a neurogenic and myogenic heart Plea.pdffazalenterprises
 
This is sociology. Content Assessments Communications Resources He.pdf
This is sociology. Content Assessments Communications Resources He.pdfThis is sociology. Content Assessments Communications Resources He.pdf
This is sociology. Content Assessments Communications Resources He.pdffazalenterprises
 
There is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdfThere is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdffazalenterprises
 
The problem is that I have to jump to the administrative function u.pdf
The problem is that I have to jump to the administrative function u.pdfThe problem is that I have to jump to the administrative function u.pdf
The problem is that I have to jump to the administrative function u.pdffazalenterprises
 
The IRS has the right to revoke an installment agreement for any of .pdf
The IRS has the right to revoke an installment agreement for any of .pdfThe IRS has the right to revoke an installment agreement for any of .pdf
The IRS has the right to revoke an installment agreement for any of .pdffazalenterprises
 
The FASB has developed specific guidelines for what to include in in.pdf
The FASB has developed specific guidelines for what to include in in.pdfThe FASB has developed specific guidelines for what to include in in.pdf
The FASB has developed specific guidelines for what to include in in.pdffazalenterprises
 
A difference between bacterial and eukaryotic transcriptionMultipl.pdf
A difference between bacterial and eukaryotic transcriptionMultipl.pdfA difference between bacterial and eukaryotic transcriptionMultipl.pdf
A difference between bacterial and eukaryotic transcriptionMultipl.pdffazalenterprises
 
Ten independent observations were made of the time to load a pallet..pdf
Ten independent observations were made of the time to load a pallet..pdfTen independent observations were made of the time to load a pallet..pdf
Ten independent observations were made of the time to load a pallet..pdffazalenterprises
 
Read and reflect on the case study involving the CEO of Xerox on pag.pdf
Read and reflect on the case study involving the CEO of Xerox on pag.pdfRead and reflect on the case study involving the CEO of Xerox on pag.pdf
Read and reflect on the case study involving the CEO of Xerox on pag.pdffazalenterprises
 
QUESTION 2 The current account is the record of O a. foreign investme.pdf
QUESTION 2 The current account is the record of O a. foreign investme.pdfQUESTION 2 The current account is the record of O a. foreign investme.pdf
QUESTION 2 The current account is the record of O a. foreign investme.pdffazalenterprises
 
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdf
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdfMULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdf
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdffazalenterprises
 
Mendel’s law of dominance supports thatOne person dominates over .pdf
Mendel’s law of dominance supports thatOne person dominates over .pdfMendel’s law of dominance supports thatOne person dominates over .pdf
Mendel’s law of dominance supports thatOne person dominates over .pdffazalenterprises
 

More from fazalenterprises (20)

Brett Donovan was the manager at Waltons Diner. He planned to promo.pdf
Brett Donovan was the manager at Waltons Diner. He planned to promo.pdfBrett Donovan was the manager at Waltons Diner. He planned to promo.pdf
Brett Donovan was the manager at Waltons Diner. He planned to promo.pdf
 
Being larger than the right ventricle, the left ventricle pumps more .pdf
Being larger than the right ventricle, the left ventricle pumps more .pdfBeing larger than the right ventricle, the left ventricle pumps more .pdf
Being larger than the right ventricle, the left ventricle pumps more .pdf
 
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdfC# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
 
A number of philosophers held various versions of the view that lang.pdf
A number of philosophers held various versions of the view that lang.pdfA number of philosophers held various versions of the view that lang.pdf
A number of philosophers held various versions of the view that lang.pdf
 
Why phelogyny has to be this way not other way aroundExaplain bas.pdf
Why phelogyny has to be this way not other way aroundExaplain bas.pdfWhy phelogyny has to be this way not other way aroundExaplain bas.pdf
Why phelogyny has to be this way not other way aroundExaplain bas.pdf
 
Which of the following are tertiary activities A. Lead mining.pdf
Which of the following are tertiary activities A. Lead mining.pdfWhich of the following are tertiary activities A. Lead mining.pdf
Which of the following are tertiary activities A. Lead mining.pdf
 
What is John Tukeys relationship to the S language and to the R la.pdf
What is John Tukeys relationship to the S language and to the R la.pdfWhat is John Tukeys relationship to the S language and to the R la.pdf
What is John Tukeys relationship to the S language and to the R la.pdf
 
What do health care professionals need to remember when caring for p.pdf
What do health care professionals need to remember when caring for p.pdfWhat do health care professionals need to remember when caring for p.pdf
What do health care professionals need to remember when caring for p.pdf
 
What is the difference between a neurogenic and myogenic heart Plea.pdf
What is the difference between a neurogenic and myogenic heart Plea.pdfWhat is the difference between a neurogenic and myogenic heart Plea.pdf
What is the difference between a neurogenic and myogenic heart Plea.pdf
 
This is sociology. Content Assessments Communications Resources He.pdf
This is sociology. Content Assessments Communications Resources He.pdfThis is sociology. Content Assessments Communications Resources He.pdf
This is sociology. Content Assessments Communications Resources He.pdf
 
There is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdfThere is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdf
 
The problem is that I have to jump to the administrative function u.pdf
The problem is that I have to jump to the administrative function u.pdfThe problem is that I have to jump to the administrative function u.pdf
The problem is that I have to jump to the administrative function u.pdf
 
The IRS has the right to revoke an installment agreement for any of .pdf
The IRS has the right to revoke an installment agreement for any of .pdfThe IRS has the right to revoke an installment agreement for any of .pdf
The IRS has the right to revoke an installment agreement for any of .pdf
 
The FASB has developed specific guidelines for what to include in in.pdf
The FASB has developed specific guidelines for what to include in in.pdfThe FASB has developed specific guidelines for what to include in in.pdf
The FASB has developed specific guidelines for what to include in in.pdf
 
A difference between bacterial and eukaryotic transcriptionMultipl.pdf
A difference between bacterial and eukaryotic transcriptionMultipl.pdfA difference between bacterial and eukaryotic transcriptionMultipl.pdf
A difference between bacterial and eukaryotic transcriptionMultipl.pdf
 
Ten independent observations were made of the time to load a pallet..pdf
Ten independent observations were made of the time to load a pallet..pdfTen independent observations were made of the time to load a pallet..pdf
Ten independent observations were made of the time to load a pallet..pdf
 
Read and reflect on the case study involving the CEO of Xerox on pag.pdf
Read and reflect on the case study involving the CEO of Xerox on pag.pdfRead and reflect on the case study involving the CEO of Xerox on pag.pdf
Read and reflect on the case study involving the CEO of Xerox on pag.pdf
 
QUESTION 2 The current account is the record of O a. foreign investme.pdf
QUESTION 2 The current account is the record of O a. foreign investme.pdfQUESTION 2 The current account is the record of O a. foreign investme.pdf
QUESTION 2 The current account is the record of O a. foreign investme.pdf
 
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdf
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdfMULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdf
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdf
 
Mendel’s law of dominance supports thatOne person dominates over .pdf
Mendel’s law of dominance supports thatOne person dominates over .pdfMendel’s law of dominance supports thatOne person dominates over .pdf
Mendel’s law of dominance supports thatOne person dominates over .pdf
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

COMP360 Assembler Write an assembler that reads the source code of an.pdf

  • 1. COMP360 Assembler Write an assembler that reads the source code of an assembly program (for an imaginary machine) and displays the machine language for that program. The following assembler instructions must be supported. Instruction opcode Length Format Example add add R1, R2, R3 sub R1, R2, R3 mult mult RI, R2, R3 I ld R1, addr R7) la R1, addr (RTT st R1, addr R71 jump addr R71 R1, addr R71 3 push R1 Machine language instructions have the following formats: 0: arithmetic Lopcode Reg I Reg 2 Reg3 4bits 4 bits 4 bits 1: Load, store. conditional jump Dopcode eg index ad 4 bits 4 bits 4 bits 20 bits 2: call, jump de unused index addr 4 bits 4 bits 4 bits 20 bits 3: push, pop, lpsw. spsw opcode Reg 4 bits 4 bits 4: rtn opcode unused. 4 bits 4 bits Solution //First Create Package Collection then create all classes mentioned below /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package collection; /** * * @author Rashmi Tiwari */ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Assembler { public static final String COMP_ADD = "ADD"; public static final String COMP_SUB = "SUB"; public static final String COMP_LOAD = "LOAD"; public static final String COMP_STORE = "STORE"; public static final String COMP_GOTO = "GOTO"; public static final String COMP_GOTOZ = "GOTOZ";
  • 2. public static final String COMP_GOTOP = "GOTOP"; public static final String COMP_READ = "READ"; public static final String COMP_WRITE = "WRITE"; public static final String COMP_STOP = "STOP"; private static Map assemblyTable; private static Map getAssemblyTable() { if (assemblyTable == null) { assemblyTable = new HashMap(); assemblyTable.put(COMP_ADD, Comp360Assembler.COMP_ADD); assemblyTable.put(COMP_SUB, Comp360Assembler.COMP_SUB); assemblyTable.put(COMP_LOAD, Comp360Assembler.COMP_LOAD); assemblyTable.put(COMP_STORE, Comp360Assembler.COMP_STORE); assemblyTable.put(COMP_GOTO, Comp360Assembler.COMP_GOTO); assemblyTable.put(COMP_GOTOZ, Comp360Assembler.COMP_GOTOZ); assemblyTable.put(COMP_GOTOP, Comp360Assembler.COMP_GOTOP); assemblyTable.put(COMP_READ, Comp360Assembler.COMP_READ); assemblyTable.put(COMP_WRITE, Comp360Assembler.COMP_WRITE); assemblyTable.put(COMP_STOP, Comp360Assembler.COMP_STOP); } return assemblyTable; } public static int[] assemble(String program) throws Comp360ParsingException { if (program == null) return null; program = preProcess(program); Map symTable = buildSymTable(program); return translateCommands(program, symTable); } public static String preProcess(String text) throws Comp360ParsingException { text = text.replaceAll("(?ms)/*.*?*/", ""); text = text.replaceAll("(?m)//.*$", ""); text = text.replaceAll("(?m)#.*$", ""); text = text.replaceAll("(?m)'.*$", ""); text = text.replaceAll("(?m)`.*$", "");
  • 3. return text; } private static int[] translateCommands(String program, Map symTable) throws Comp360ParsingException { ArrayList code = new ArrayList(); Scanner scan = new Scanner(program); int lineNumber = 0; int opNumber = 0; while (scan.hasNextLine()) { lineNumber++; String line = scan.nextLine(); Scanner tokenizer = new Scanner(line); if (!tokenizer.hasNext()) continue; // blank line String token = tokenizer.next(); // IL position 2 if (isLineLabel(token)) { if (!tokenizer.hasNext()) continue; token = tokenizer.next(); } // IL position 4 if (isInstruction(token)) { token = token.toUpperCase(); Integer opCode = getAssemblyTable().get(token); Integer address = 0; if (!token.equals(COMP_STOP) && !token.equals(COMP_READ) && !token.equals(COMP_WRITE)) { token = tokenizer.next(); if (!isNumber(token)) { token = token.toUpperCase(); address = symTable.get(token); } else { address = Integer.parseInt(token);
  • 4. } } code.add(opCode * 100 + address); opNumber++; } } return RunTools.deboxInteger(code); } public static Map buildSymbols(String program) throws Comp360ParsingException { return buildSymTable(preProcess(program)); } private static Map buildSymTable(String program) throws Comp360ParsingException { Map lineLabels = new HashMap(); Map variables = new HashMap(); variables.put("ZERO", 98); variables.put("ONE", 99); Scanner scan = new Scanner(program); int nextVariable = 50; int lineNumber = 0; int opNumber = 0; while (scan.hasNextLine()) { lineNumber++; String line = scan.nextLine(); Scanner tokenizer = new Scanner(line); if (!tokenizer.hasNext()) continue; String token = tokenizer.next(); // IL position 2 if (isLineLabel(token)) { token = token.substring(0, token.length() - 1); if (lineLabels.get(token) != null) throw new Comp360ParsingException("Duplicate line label "+token+" encountered on line "+lineNumber);
  • 5. lineLabels.put(token, opNumber); if (!tokenizer.hasNext()) continue; token = tokenizer.next(); } // IL position 4 if (isInstruction(token)) { token = token.toUpperCase(); if (!token.equals(COMP_STOP) && !token.equals(COMP_READ) && !token.equals(COMP_WRITE)) { if (!tokenizer.hasNext()) throw new Comp360ParsingException("Unexpected end of line on line "+lineNumber); token = tokenizer.next(); if (!isNumber(token)) { token = token.toUpperCase(); if (!variables.containsKey(token)) variables.put(token, nextVariable++); } else { int address = Integer.parseInt(token); if (address > 99 || address < 0) throw new Comp360ParsingException("Instruction addresses must be at most two digits"); } } opNumber++; if (!tokenizer.hasNext()) continue; token = tokenizer.next(); } throw new Comp360ParsingException("Expected end of line on line "+lineNumber); } if (opNumber > 49) throw new Comp360ParsingException("Too many operations. Programs must have fewer than 50 instructions"); if (nextVariable >= 98)
  • 6. throw new Comp360ParsingException("Too many variables in program. Must be less than 48"); variables.putAll(lineLabels); // line labels should have precedent over variables return variables; } public static boolean isNumber(String token) { try { Integer.parseInt(token); return true; } catch (NumberFormatException e) { return false; } } public static boolean isInstruction(String token) { return token != null && getAssemblyTable().containsKey(token.toUpperCase()); } public static boolean isLineLabel(String token) { return token != null && token.endsWith(":") && token.length() > 1; } public static boolean isSymbol(String token) { return token.matches("^[a-zA-Z_][w]*$"); } } //Comp360Assembler class is in same package of Collection /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor.
  • 7. */ package collection; /** * * @author Rashmi Tiwari */ public interface Comp360Assembler extends Runnable{ public static final int DEFAULT_DATA = 0; public static final int MIN_INT = -999; public static final int MAX_INT = 999; public static final int MEMORY_SIZE = 100; public static final int PROGRAM_INSERTION_POINT = 0; public static final int INSTRUCTION_SIZE = 1; public static final int COMMAND = 0; public static final int ADDRESS = 1; public static final int COMP_STOP = 0; public static final int COMP_ADD = 1; public static final int COMP_SUB = 2; public static final int COMP_LOAD = 3; public static final int COMP_STORE = 4; public static final int COMP_GOTO = 5; public static final int COMP_GOTOZ = 6; public static final int COMP_GOTOP = 7; public static final int COMP_READ = 8; public static final int COMP_WRITE = 9; public void reset(); public void flushIO(); public String getBadStateReason(); public void fetch(); public int[] decode();
  • 8. public void execute(int command, int address); public void step(); public void run(); public boolean isRunning(); public int readOutput(); public void addInput(int data); public int[] memDump(); public void loadMemDump(int memDump[]); public int getIr(); public int getDr(); public int getPc(); } //Custom Exception Class package collection; /** * * @author Rashmi Tiwari */ public class Comp360ParsingException extends Exception{ private static final long serialVersionUID = 1L; public Comp360ParsingException(String message) { super(message); } } //Runner Class /*
  • 9. * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package collection; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /** * * @author Rashmi Tiwari */ public class Runner { /** * Special output returned if a program timed out */ public static final int[] TIMED_OUT = {1000}; private static final long RUNNER_TIMEOUT = 10000; public static int[] runProgram(Comp360Assembler comp360, int[] program, int[] input) { program = RunTools.loadProgram(program); comp360.reset(); comp360.flushIO(); comp360.loadMemDump(program); for (int data : input) comp360.addInput(data); // run the program Future programRunner = Executors.newSingleThreadExecutor().submit(comp360); try { programRunner.get(RUNNER_TIMEOUT, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new IllegalStateException(e.getMessage(), e);
  • 10. } catch (ExecutionException e) { throw new IllegalStateException(e.getMessage(), e); } catch (TimeoutException e) { programRunner.cancel(true); return TIMED_OUT.clone(); } return RunTools.extractOutput(comp360); } } //RunTool Class /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package collection; import java.util.Arrays; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.NoSuchElementException; /** * * @author Rashmi Tiwari */ public class RunTools { private static int[] blankMemory; public static int[] loadProgram(int[] program) { if (program == null) return getBlankMemory().clone(); if (program.length == Comp360Assembler.MEMORY_SIZE) return program; // no need to load it into a memory dump if (program.length > Comp360Assembler.MEMORY_SIZE)
  • 11. return null; int[] newProgram = getBlankMemory().clone(); System.arraycopy(program, 0, newProgram, 0, program.length); return newProgram; } public static int[] extractOutput(Comp360Assembler comp360) { List outputList = new LinkedList(); while (true) { try { outputList.add(comp360.readOutput()); } catch (NoSuchElementException e) { // finished reading output break; } } return deboxInteger(outputList); } private static int[] getBlankMemory() { if (blankMemory == null) { blankMemory = new int[Comp360Assembler.MEMORY_SIZE]; Arrays.fill(blankMemory, 0); blankMemory[98] = 0; // write-protected constant blankMemory[99] = 1; // write-protected constant } return blankMemory; } static int[] deboxInteger(List a) { if (a == null) return null; int[] result = new int[a.size()]; Iterator iter = a.iterator(); for (int i = 0; i < result.length; i++) {
  • 12. result[i] = iter.next(); } return result; } } //Core Class /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package collection; import java.util.Arrays; import java.util.LinkedList; /** * * @author Rashmi Tiwari */ public class Core { private static final int DIGITS = 3; private int[] mem; private int ir; private int dr; private int pc; private LinkedList stdin; private LinkedList stdout; private String badState; private boolean running; /** * Constructs a new VIC object with default state */ public Core() { stdin = new LinkedList(); stdout = new LinkedList(); mem = new int[Comp360Assembler.MEMORY_SIZE];
  • 13. reset(); resetMemory(); flushIO(); } public void reset() { pc = Comp360Assembler.PROGRAM_INSERTION_POINT; ir = Comp360Assembler.DEFAULT_DATA; dr = Comp360Assembler.DEFAULT_DATA; badState = null; running = false; } private void resetMemory() { Arrays.fill(mem, 0); mem[98] = 0; // write protected constants ZERO & ONE mem[99] = 1; } private boolean inBadState() { return badState != null; } public void fetch() { if (inBadState()) return; ir = mem[pc]; pc += Comp360Assembler.INSTRUCTION_SIZE; } public int[] decode() { if (inBadState()) return null; if (!isProperInstruction(ir)) { badState("Malformed Instruction at " + pc + ":" + ir); return null; } // extract the nth digit of the IR, and dispatch it with the rest of the digits
  • 14. int decode[] = new int[2]; decode[Comp360Assembler.COMMAND] = extractDigitN(ir, DIGITS); decode[Comp360Assembler.ADDRESS] = ir % (int) (Math.pow(10, DIGITS - 1)); return decode; } private boolean isProperInstruction(int ir) { return Comp360Assembler.MAX_INT >= ir && ir >= 0; } private int extractDigitN(int data, int n) { return (data / (int) (Math.pow(10, n - 1))) % 10; } public void execute(int command, int address) { if (inBadState()) return; switch (command) { case Comp360Assembler.COMP_STOP: running = false; break; case Comp360Assembler.COMP_ADD: if (!isValidData(dr + mem[address])) badState("Integer Overflow at " + pc + ":" + ir); dr += mem[address]; break; case Comp360Assembler.COMP_SUB: if (!isValidData(dr - mem[address])) badState("Integer underflow at " + pc + ":" + ir); dr -= mem[address]; break; case Comp360Assembler.COMP_LOAD: dr = mem[address]; break; case Comp360Assembler.COMP_STORE: if (address == 98 || address == 99) badState("Memory address "+address+" is write protected at "+pc+":"+ir); mem[address] = dr;
  • 15. break; case Comp360Assembler.COMP_GOTO: pc = address; break; case Comp360Assembler.COMP_GOTOZ: if (dr == 0) { pc = address; } break; case Comp360Assembler.COMP_GOTOP: if (dr > 0) { pc = address; } break; case Comp360Assembler.COMP_READ: dr = readInput(); break; case Comp360Assembler.COMP_WRITE: stdout.add(dr); break; default: badState("Bad VIC instruction: " + command + address); } } private int readInput() { if (!stdin.isEmpty()) return stdin.remove(); else { badState("No more input at " + pc); return Comp360Assembler.DEFAULT_DATA; } } private void badState(String reason) { running = false; badState = reason;
  • 16. } public String getBadStateReason() { return this.badState; } public void step() { if (inBadState()) return; fetch(); int decode[] = decode(); execute(decode[Comp360Assembler.COMMAND], decode[Comp360Assembler.ADDRESS]); } public void run() { running = true; while (running && !inBadState()) { step(); } } public void addInput(int data) { if (isValidData(data)) stdin.add(data); } private boolean isValidData(int data) { return Comp360Assembler.MAX_INT >= data && data >= Comp360Assembler.MIN_INT; } public int readOutput() { return stdout.remove(); } public int[] memDump() { return mem.clone();
  • 17. } public void loadMemDump(int memDump[]) { if (memDump.length != Comp360Assembler.MEMORY_SIZE) return; // not a proper memdump for (int i = 0; i < Comp360Assembler.MEMORY_SIZE; i++) if (!isValidData(memDump[i])) { return; // not a proper memdump } // NOTE: ignores the last 2 cells in the mem-dump since they are // supposed to be constant, write protected cells. System.arraycopy(memDump, 0, mem, 0, Comp360Assembler.MEMORY_SIZE - 2); } public int getIr() { return ir; } public int getDr() { return dr; } public int getPc() { return pc; } public void flushIO() { stdin.clear(); stdout.clear(); } public boolean isRunning() { return running; } protected void setDr(int dr) { this.dr = dr; } }