Developing a Java Assembler for CS227 Assignment: Implementation

Verified

Added on  2023/01/13

|6
|805
|84
Homework Assignment
AI Summary
This assignment requires the design and implementation of an assembler program in Java, specifically for the simple computer from Miniassignment 2. The assembler translates assembly language into machine code, utilizing symbolic names for opcodes, data locations, and jump targets. The solution involves the creation of two primary classes: Assembler.java, which handles the core assembly process, and potentially an AsmFileUtil class (as suggested in the assignment brief), for file handling. The assembler reads an assembly language file, parses its content, builds a symbol table, and generates the corresponding machine code. The implementation includes handling different command types (A-commands, C-commands, and L-commands), managing memory addresses, and converting symbolic addresses to numerical values. The code demonstrates file reading, parsing, symbol table usage, and machine code generation. The program reads the assembly file line by line, and during the first pass, it populates a symbol table with labels and their corresponding memory addresses. In the second pass, the code converts assembly instructions into binary machine code, addressing variables and jump targets using the symbol table. The solution includes handling different command types, managing memory addresses, and converting symbolic addresses to numerical values, and writing the resulting machine code to an output file.
Document Page
Introduction
The objective of this assignment design and develop an assembler .The An assembler is a
program that translates assembly language into machine code. We design and implement
symbolic names for opcodes, data locations (variables), and jump targets.
The assignment start with given instruction as follows
1. Download the zip file.
2. In Eclipse, go to File -> Import -> General -> Existing Projects into Workspace,
click Next.
3. Click the radio button for “Select archive file”.
4. Browse to the zip file you downloaded and click Finish.
The assembly language
In this program we working the file-reading i so that the actual input to the assembler is an
ArrayList of strings, one for each line of the file.
We develop and design the project in hw directory.
In this an Assembler class we define the variable given below
public static int counter=0;
public static int nextRam = 16;
public static String compT,destT,jumpT; // temp's
String name = args[0].substring(0, args[0].indexOf('.'));
copies name of existing file without the file type
String outFileName = name+".hack"; //out file name
We create the object of SymbolTable that
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
SymbolTable st = new SymbolTable(); //init's symbol table
We create Parser object
Parser newParser = new Parser(args[0]); //new parser object
We create file object for storing information of generate the output
File out = new File(outFileName); //output, .hack file
Next we will work on write the data into file
FileWriter fw = null;
try {
fw = new FileWriter(out.getAbsoluteFile());
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(fw);
Ready to write on file
first pass for add the entry this will work for adds new symbol to symbol table
while(newParser.hasMoreCommand()) {
if(newParser.commandType()== Parser.commandType.L_COMMAND) {
st.addEntry(newParser.symbol(),Integer.toString(counter)) ;
Resets counter for starts from first line
//second pass
Document Page
while(newParser.hasMoreCommand())
{
if(newParser.commandType()== Parser.commandType.A_COMMAND) //@xxx
{
if(newParser.strFileArr[newParser.lineCount].startsWith("@"))
{
String tmp = newParser.symbol(); //returns xxx
if(newParser.isNum(tmp)) //checks if xxx is number
{
int xxx = Integer.parseInt(tmp);
tmp = Parser.dexToBin(xxx); // return bin value of xxx
tmp = newParser.addZero(tmp);
try {
bw.write(tmp + '\n');//write to hack
} catch (IOException e) {
e.printStackTrace();
}
}
else //if not number
{
if(!st.containKey(tmp)) // not exists in Symbol Table
{
Document Page
st.addEntry(tmp,Integer.toString(nextRam)); //Adds to Symbol Table
nextRam++;
}
if(st.containKey(tmp)) // already exists in Symbol Table
{
String tmp2 = st.getValue(tmp);
int xxx = Integer.parseInt(tmp2);
tmp2 = Parser.dexToBin(xxx);
tmp2 = newParser.addZero(tmp2);
try {
bw.write(tmp2+'\n'); //write to hack
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}//if command type A_COMMAND
if(newParser.commandType()== Parser.commandType.C_COMMAND)
{
if(newParser.strFileArr[newParser.lineCount].contains("="))//dest=comp
{
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
destT = ct.getDest(newParser.dest());
compT = ct.getComp(newParser.comp());
jumpT = ct.getJump("NULL"); //no need jump
try {
bw.write("111" + compT + destT + jumpT +'\n');
} catch (IOException e) {
e.printStackTrace();
}
}
else if(newParser.strFileArr[newParser.lineCount].contains(";")) //jump
{
destT = ct.getDest("NULL"); // no need dest
compT = ct.getComp(newParser.comp());
jumpT = ct.getJump(newParser.jump());
try {
bw.write("111" + compT + destT + jumpT +'\n');
} catch (IOException e) {
e.printStackTrace();
}
}
Document Page
}//if command type C_COMMAND
newParser.advance();
}//end while
This assembler.java is main file the above complete description is for this assembler.
chevron_up_icon
1 out of 6
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]