patternjavaMinor
Assembler for Hack Assembly Language
Viewed 0 times
languageassemblyassemblerforhack
Problem
I have been reading this great book and I decided to do the Project in Chapter 5: Writing an Assembler for the Hack Assembly Language.
You can find the specification for the Hack Machine Language and the Hack Assembly Language here.
Here is my implementation:
`package biz.tugay.hack.assembler;
/ User: koray.asm@tugay.biz Date: 07/03/15 Time: 10:47 /
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HackAssembler {
private static File machineLanguageFile;
private static File assembled;
private static FileWriter fileWriter;
private static int variableAddressCounter = 16;
private static Map variableAddressMap = new HashMap();
private static Map loopAddressMap = new HashMap();
public static void main(String[] args) throws IOException {
doChecksForSourceFile(args);
checkLabelsAndPopulateLoopAddressMap(machineLanguageFile);
Scanner machineLanguageFileScanner = new Scanner(machineLanguageFile);
fileWriter = new FileWriter(assembled);
while(machineLanguageFileScanner.hasNextLine()){
String nextAssemblyLine = machineLanguageFileScanner.nextLine();
assemble(nextAssemblyLine);
}
fileWriter.flush();
fileWriter.close();
}
private static void assemble(String lineToBeAssembled) throws IOException {
if(lineToBeAssembled.startsWith("(")) {
}
else if(lineToBeAssembled.startsWith("@")) {
assemble_A_Instruction(lineToBeAssembled);
fileWriter.write(System.lineSeparator());
}
else {
assemble_C_Instruction(lineToBeAssembled);
fileWriter.write(System.lineSeparator());
}
}
private static void assemble_C_Instruction(String lineToBeAssembled) throws IOException {
String compMnemonnic;
String destinationMnemonnic = "null";
String jumpMnemonnic = "null";
String compBinaryR
You can find the specification for the Hack Machine Language and the Hack Assembly Language here.
Here is my implementation:
`package biz.tugay.hack.assembler;
/ User: koray.asm@tugay.biz Date: 07/03/15 Time: 10:47 /
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HackAssembler {
private static File machineLanguageFile;
private static File assembled;
private static FileWriter fileWriter;
private static int variableAddressCounter = 16;
private static Map variableAddressMap = new HashMap();
private static Map loopAddressMap = new HashMap();
public static void main(String[] args) throws IOException {
doChecksForSourceFile(args);
checkLabelsAndPopulateLoopAddressMap(machineLanguageFile);
Scanner machineLanguageFileScanner = new Scanner(machineLanguageFile);
fileWriter = new FileWriter(assembled);
while(machineLanguageFileScanner.hasNextLine()){
String nextAssemblyLine = machineLanguageFileScanner.nextLine();
assemble(nextAssemblyLine);
}
fileWriter.flush();
fileWriter.close();
}
private static void assemble(String lineToBeAssembled) throws IOException {
if(lineToBeAssembled.startsWith("(")) {
}
else if(lineToBeAssembled.startsWith("@")) {
assemble_A_Instruction(lineToBeAssembled);
fileWriter.write(System.lineSeparator());
}
else {
assemble_C_Instruction(lineToBeAssembled);
fileWriter.write(System.lineSeparator());
}
}
private static void assemble_C_Instruction(String lineToBeAssembled) throws IOException {
String compMnemonnic;
String destinationMnemonnic = "null";
String jumpMnemonnic = "null";
String compBinaryR
Solution
My two cents:
-
Your lines are long:
"code gets harder to read the wider it becomes"
-
Spaces.
can easily be
-
There are no comments describing what the code does.
"Comments can never be replaced by code alone."
-
Strings are hard-coded all over the place. It's much easier to understand
than
It helps to have these type of things all in one place in anticipation of one of them needing change.
Over all, it looks pretty decent. Just remember: K.I.S.S.
-
Your lines are long:
"code gets harder to read the wider it becomes"
- You pass around ambiguously named variables. At first glance, it's hard to know what
args[0]orsubStringis.
- Some names are very long.
-
Spaces.
for(int i=0;i<zerosToAppend;i++);can easily be
for(int i = 0; i < zerosToAppend; i++);-
There are no comments describing what the code does.
"Comments can never be replaced by code alone."
-
Strings are hard-coded all over the place. It's much easier to understand
assembled = new File(fileName + FILE_SUFFIX);than
assembled = new File(args[0]+".hack");It helps to have these type of things all in one place in anticipation of one of them needing change.
Over all, it looks pretty decent. Just remember: K.I.S.S.
Code Snippets
for(int i=0;i<zerosToAppend;i++);for(int i = 0; i < zerosToAppend; i++);assembled = new File(fileName + FILE_SUFFIX);assembled = new File(args[0]+".hack");Context
StackExchange Code Review Q#83495, answer score: 7
Revisions (0)
No revisions yet.