Ask a question from expert

Ask now

Design and Develop an Assembler

Implement an assembler for a simple computer from Miniassignment 2, involving writing loops, using arrays and lists, and creating interacting Java classes.

6 Pages805 Words84 Views
   

Added on  2023-01-13

About This Document

This assignment focuses on designing and developing an assembler program that translates assembly language into machine code. It covers the steps involved in creating the assembler, including file reading, symbol table creation, and generating the output file. The main file for this assignment is Assembler.java.

Design and Develop an Assembler

Implement an assembler for a simple computer from Miniassignment 2, involving writing loops, using arrays and lists, and creating interacting Java classes.

   Added on 2023-01-13

BookmarkShareRelated Documents
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
Design and Develop an Assembler_1
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
Design and Develop an Assembler_2
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
{
Design and Develop an Assembler_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Code for Proxy Cache Assignment - Desklib
|7
|1720
|105

R programming for analyzing flight dataset and flight scheduling
|26
|1439
|140

CS4348 - Operating Systems Concepts
|5
|1324
|177

Shared Memory Segment Creation and Destruction in C Programming
|29
|2258
|453