-
Notifications
You must be signed in to change notification settings - Fork 0
/
masm.d
94 lines (76 loc) · 2.62 KB
/
masm.d
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/********************************************************************************
* masm.d: main file for mips assembler/disassembler *
* 2016 - Ben Perlin *
*******************************************************************************/
import std.exception;
import std.getopt;
import std.stdio;
import program;
import util;
int main(string[] args) {
uint startAddress = 0x0040_0000;
string startAddressString;
string outputFilename;
bool disassemblyMode;
bool printSymbolTable;
bool strictCompatibility;
auto helpInformation = getopt(args,
std.getopt.config.passThrough,
"start|s", &startAddressString,
"disassemble|d", &disassemblyMode,
"output-file|o", &outputFilename,
"strict", &strictCompatibility,
"print-symbol-table", &printSymbolTable);
if (helpInformation.helpWanted) {
defaultGetoptPrinter("Usage masm [filename]", helpInformation.options);
return 0;
}
if (startAddressString != "") {
try {
startAddress = parseStartAddress(startAddressString);
enforce(startAddress%4 == 0, "Error: Start address must be word aligned");
} catch (Exception exception) {
stderr.writeln(exception.msg);
return 1;
}
}
File codeFile = stdin; /* note does not work from console yet */
if (args.length == 2) {
string inputFilename = args[1];
if (inputFilename != "") {
try {
codeFile = File(inputFilename, "r");
} catch (Throwable o) {
stderr.writeln("Failed to open file: ", inputFilename);
return 1;
}
}
}
Program program;
File outputFile = stdout;
if (outputFilename != "") {
try {
outputFile = File(outputFilename, "w");
} catch (Throwable o) {
stderr.writeln("Failed to open file: ", outputFilename);
return 1;
}
}
try {
if (disassemblyMode) {
program.disassemble(codeFile);
program.printDisassembled(outputFile);
} else {
program.assemble(codeFile, startAddress, strictCompatibility);
if (printSymbolTable) {
writeln();
program.printSymbolTable(outputFile);
}
program.printAssembled(outputFile);
}
} catch (Exception exception) {
/* print exception message without stacktrace */
stderr.writeln(exception.msg);
}
return 0;
}