-
Notifications
You must be signed in to change notification settings - Fork 2
/
asm.c
179 lines (154 loc) · 3.95 KB
/
asm.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "asm.h"
#include "code.h"
#include "error.h"
#include "parse.h"
#include "symbol.h"
#define MAXOUTBUFF 10000
struct settings settings;
void usage(void)
{
printf("\n<options> <file>\n");
printf("<file> Filename to assemble.\n");
printf(" Filename must end with .asm.\n");
printf("-C Print code output to stdout - with comments.\n");
printf("-c Print code output to stdout - without comments.\n");
printf("-t Print final hash table to stdout.\n");
printf("-v Verbose output to stdout.\n");
printf("-x Print commands to stdout.\n\n");
}
void settings_init(void)
{
settings.verbose = 0;
settings.verbose = 0;
settings.hash = 0;
settings.code = 0;
settings.comments = 0;
settings.commands = 0;
}
int main(int argc, char *argv[])
{
char FilenameBuff[80];
register int i = 0;
int address = 0;
int c;
settings_init();
if(argc <2) { usage(); return 0;}
while(-1 != (c = getopt(argc, argv,
"-v" /* verbose output to stdout */
"-t" /* print final hash table to stdout */
"-c" /* print code output to stdout - without comments */
"-C" /* print code output to stdout - with comments */
"-x" /* print commands to stdout */
"--help" /*print out usage statement */
))) {
switch (c)
{
case 'v':
settings.verbose = 1;
settings.hash = 1;
settings.code = 1;
settings.comments = 1;
settings.commands = 1;
break;
case 't':
settings.hash = 1;
break;
case 'c':
settings.code = 1;
break;
case 'C':
settings.code = 1;
settings.comments = 1;
break;
case 'x':
settings.commands = 1;
break;
}
}
/* find path location */
if(argv[1][0] == '-') { c = 2; } else { c = 1; }
if(argc < 2) { exit_error(1, "No Input Files."); usage(); }
/* TODO: future versions will accept more than one file */
if(argc > 3) { exit_error(2, "Too Many Files Listed."); usage(); }
strcpy(FilenameBuff, argv[c]);
/* verify filename extension */
i = strlen(argv[c]) - 1;
if( (argv[c][i-2] != 'a') ||
(argv[c][i-1] != 's') ||
(argv[c][i] != 'm' ) ) { exit_error(5, "Filename Extension Not Correct."); usage(); }
init_parser(FilenameBuff);
/* modify filename to output filename and then open file */
FilenameBuff[i-2] = 'h';
FilenameBuff[i-1] = 'a';
FilenameBuff[i] = 'c';
FilenameBuff[i+1] = 'k';
FilenameBuff[i+2] = '\0';
/* load symbol table with pre-defined symbols */
add_entry("SP", 0);
add_entry("LCL", 1);
add_entry("ARG", 2);
add_entry("THIS", 3);
add_entry("THAT", 4);
add_entry("SCREEN", 16384);
add_entry("KBD", 24576);
add_entry("R0", 0);
add_entry("R1", 1);
add_entry("R2", 2);
add_entry("R3", 3);
add_entry("R4", 4);
add_entry("R5", 5);
add_entry("R6", 6);
add_entry("R7", 7);
add_entry("R8", 8);
add_entry("R9", 9);
add_entry("R10", 10);
add_entry("R11", 11);
add_entry("R12", 12);
add_entry("R13", 13);
add_entry("R14", 14);
add_entry("R15", 15);
while(has_more_commands())
{
advance();
if(command_type() == A_COMMAND || command_type() == C_COMMAND)
inc_rom_address();
if(command_type() == L_COMMAND)
symbol_load();
}
init_coder(FilenameBuff);
reset_buffer();
while(has_more_commands())
{
char sym[MAXCOMMAND];
advance();
if(command_type() == A_COMMAND || command_type() == L_COMMAND)
{
address = symbol(sym);
if(command_type() == A_COMMAND) { enc_symbol(address); }
}
if(command_type() == C_COMMAND)
{
if(comp(sym) != 0) { enc_comp(sym); }
if(dest(sym) != 0) { enc_dest(sym); }
if(jump(sym) != 0) { enc_jump(sym); }
}
if(command_type() != L_COMMAND)
{
advance_ouptut_file();
if(settings.code != 0 || settings.comments != 0 ) { printf(" "); }
} else {
if(settings.code != 0 || settings.comments != 0)
{
printf(" ");
}
}
if(settings.commands != 0) { print_current_command(); }
if((settings.code == 0 || settings.comments == 0) && argc > 2) { printf("\n"); }
}
if(settings.hash != 0) { print_hash(); }
return 0;
}