logo

Design and Develop an Assembler

   

Added on  2023-01-13

6 Pages805 Words84 Views
 | 
 | 
 | 
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