-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assembly.java
63 lines (53 loc) · 2.11 KB
/
Assembly.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;
import fileOperations.CopyFileToProject;
import fileTools.FileAssembly;
import fileTools.instructions.*;
public class Assembly {
public static void main(String[] args) {
// Copia arquivo para o projeto
CopyFileToProject copyFile = new CopyFileToProject();
copyFile.main(args);
try {
String line;
// FileAssembly f = new FileAssembly("arquivo.asm");
FileAssembly f = new FileAssembly("files/instrucoes.asm");
Map<String, Integer> labels = f.countLinesWithLabels();
f.fileRead();
BufferedReader bf = new BufferedReader(f.getFile());
Translator tr = new Translator(labels);
int i = 1;
ArrayList<String> instructions = new ArrayList<>(); // pegar instruções do arquivos
while ((line = bf.readLine()) != null) {
// Se linha não for vazia e não tiver nada após o label
if (!line.isEmpty() && !line.substring(line.lastIndexOf(":") + 1).trim().isEmpty()) {
System.out.println(tr.translateInstruction(line, i));
instructions.add(tr.translateInstruction(line, i));
}
i++;
}
i = 1;
String fileName = "output.hex";
BinaryConversor binaryConversor = new BinaryConversor();
try (PrintWriter writer = new PrintWriter(new FileWriter(fileName))) {
writer.println("v2.0 raw");
for (String instruction : instructions) {
writer.print(binaryConversor.binaryToHexa(instruction) + " ");
if (i >= 4) {
writer.print("\n");
i = 0;
}
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}