Skip to content

Commit

Permalink
Set debug, activate binary
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoGames4 authored Jun 15, 2024
1 parent 11e9e39 commit 6fc1df6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
9 changes: 3 additions & 6 deletions EPAssembly/src/compiler/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Compiler {
* Whether additional information should be printed.<br>
* Writes the original code line next to the hex translation into the destination file if true. (Separated by an '@' character.)
*/
public static final boolean DEBUG = true;
public static boolean DEBUG = false;

/**
* Stores the original assembly source code.
Expand Down Expand Up @@ -138,19 +138,16 @@ public static Register getRegisterByTitle(String title, int line) throws Compile
* @throws CompileException if something goes wrong
* @see #compileToHex()
*/
public String[] compileToBinary() throws CompileException { // TODO tag same line support, does not support multiple spaces within line
public String[] compileToBinary() throws CompileException { // TODO tag same line support
ArrayList<String> binaries = new ArrayList<>();

System.out.println("Starting compilation process to binary...");

for(int i = 0; i<code.length; i++) {
final String line = code[i].split(";")[0].trim(); // Separate comments
if(line.isEmpty()) continue; // Ignore empty lines
if(line.contains(" ")) { // Source code must not contain multiple spaces within its content
throw new CompileException(i, "Multiple spaces next to each other are only allowed at the beginning or at the end of a line.");
}

final String[] components = line.split(" ", 2); // Seeks for the command title
final String[] components = line.split(" ", 2); // Seeks for the command title and the arguments
final String[] args = components.length > 1 ? components[1].split(",") : new String[0]; // Seeks for the command arguments

for(int j = 0; j<args.length; j++) { // Trims the arguments
Expand Down
17 changes: 13 additions & 4 deletions EPAssembly/src/compiler/Load.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@
* Contains the main method of this compiler, which is responsible for reading the assembly code and writing it to the target file.
* @author Mika Thein
* @see #main(String[])
* @see compiler.Compiler
*/
public class Load {

/**
* The main method. Reads the assembly code and translates it into hex using the {@link compiler.Compiler} class.
* @param args requires two elements: A source file path and a destination file path. (Absolute or relative to the jar location.)
* The main method. Reads the assembly code and translates it into hex using the {@link compiler.Compiler} class.<br>
* <ul>
* <li>The first argument specifies the path of the source file.</li>
* <li>The second argument specifies the path of the destination file.</li>
* <li>If the third argument is set to "binary", the compiler will output binary instead of hex. (Optional.)</li>
* <li>If the fourth argument is set to "debug", the {@link compiler.Compiler#DEBUG} mode will be set to true. (Optional.)</li>
* <lu>
* @param args requires at least two arguments: A source file path and a destination file path. (Absolute or relative to the jar location.)
*/
public static void main(String[] args) {
if(args.length < 2) {
throw new RuntimeException("Not enough arguments. Source and destination file paths required.\nFor example: folder/in.asm another_folder/out.txt.");
}

boolean binary = args.length > 2 && args[2].equals("binary");

Compiler.DEBUG = args.length > 3 && args[3].equals("debug");

try {
File from = new File(args[0]);
File to = new File(args[1]);
Expand All @@ -44,7 +53,7 @@ public static void main(String[] args) {
}

Compiler compiler = new Compiler(code);
String[] result = compiler.compileToHex();
String[] result = !binary ? compiler.compileToHex() : compiler.compileToBinary();

System.out.println("Writing to " + to.getAbsolutePath() + "...");

Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A simple assembly compiler for the Intel 8080 based chip used at the electronics
## What it does
* This compiler translates assembly code into hex code as required by the course. (The compiler prints each command to a new line. Ignore all line breaks when you copy the code into the chip to assure correct line jumping.)
* Informs you if a command or a register is not known (to the compiler) or if they might be used incorrectly.
* Additional it is also possible to print out the binary representation.
* Additional it is also possible to compile to binary (by setting the third argument to `binary` as described below).

## What it does not
* The compiler does not actually *execute* your code. To test the code’s behaviour I recommend https://eliben.org/js8080/ by [Eli Bendersky](https://github.com/eliben/js-8080-sim). (Please note that this online simulator does not support `IN` or `OUT`, but it shows the content of each register.)
Expand All @@ -28,7 +28,7 @@ Please also read limitations and requirements further below.
For this approach the [Java JDK](https://www.oracle.com/java/technologies/downloads/) is required.
1. Download the latest release or clone this repository (to build it yourself).
2. Navigate to the `.jar`-file from your terminal.
3. Run `java -jar [enter release file path here] [assembly source file path] [hex destination file]` (without the brackets),
3. Run `java -jar [enter release file path here] [assembly source file path] [hex destination file] {hex/binary} {debug/standard}` (without the brackets, curly brackets are optional),
for example `java -jar EPAssemblyCompiler.jar aufgabe1/source.asm aufgabe1/source_hex.txt`.
4. The compiled hex code should be written to the destination file.

Expand Down Expand Up @@ -177,8 +177,11 @@ EQUALS:
JMP START
```

### Compile to binary
Per default the compiler compiles to hex. To compile to binary the third argument on execution must be set to `binary`.

### Debug mode
The `Compiler.java`-class contains a boolean named `DEBUG`, which is `true` by default. If true, the compiler will show the following extra information.
The `Compiler.java`-class contains a boolean named `DEBUG` which is `false` by default, but can be set by writing `debug` as the fourth argument. If true, the compiler will show the following extra information.
* The current line (to the console while compiling).
* The corresponding (original) line next to each hex code line (to the destination file, separated by an '@' character).

Expand Down

0 comments on commit 6fc1df6

Please sign in to comment.