-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembler.c
209 lines (177 loc) · 5.2 KB
/
assembler.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <limits.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "symbol_table.h"
#include "parser.h"
/*
* Extract the filename without the extension
*
* @param char *file: the file name with the extension
* @return:
* * the file name without the extension (allocates memory)
* * NULL iff file is not a valid assembly file
*/
char *extractFileName(const char *file) {
char *file_token;
char *ext_token;
char *filename;
char *bname;
char file_cpy[NAME_MAX];
char *path;
if (!file) {
return NULL;
}
if (strlen(file) > NAME_MAX) {
fprintf(stderr, "File name exceeds %d characters\n", NAME_MAX);
return NULL;
}
if ( ! (path = strndup(file, strlen(file))) ) {
return NULL;
}
if ( ! (bname = basename(path)) ) {
free(path);
return NULL;
}
strncpy(file_cpy, bname, strlen(bname));
file_cpy[strlen(file)] = '\0';
free(path);
file_token = strtok(file_cpy, ".");
ext_token = strtok(NULL, ".");
if (file_token && ext_token && strncmp(ext_token, "asm", 3) == 0) {
if (!(filename = (char *)malloc(sizeof(char) * strlen(file_token) + 1))) {
return NULL;
}
if (!strncpy(filename, file_token, strlen(file_token))) {
perror("strcpy");
free(filename);
return NULL;
}
filename[strlen(file_token)] = '\0';
return filename;
}
fprintf(stderr, "File must have .asm extension\n");
return NULL;
}
/*
* The assembler's first pass through the assembly code to look ahead of all the
* labels that exist in the program and insert them to the symbol table
*
* @param table: the symbol table
* @param fp : thre file pointer to read the source code
*/
void first_pass(struct symbol_entry *table, FILE *fp) {
int i = 0;
int line_num = 0, is_label = 0;
fseek(fp, 0, SEEK_SET);
char line[INSTR_SIZE];
char symbol[SYMBOL_SIZE];
while (fgets(line, sizeof(line), fp)) {
for (i = 0; i < strlen(line); i++) {
if (line[i] == '/' || line[i] == '\r' || line[i] == '\n') {
break;
}
else if (isalnum(line[i]) || line[i] == '@') {
line_num++;
break;
}
else if (line[i] == '(') {
//printf("line: %s\n", line);
parse_address_asm(line, symbol, &is_label);
//printf("label: %s\n", symbol);
get_addr_bin(table, symbol, line_num);
break;
}
}
}
fseek(fp, 0, SEEK_SET);
}
/*
* Write the output of the assembler to the provided file
*
* @param filename : the name of the file without the hack extension to write
* the output of the assembler to
* @param instructions: the list of instructions stored in a data structure that
* contains all the information needed to convert to
* machine code
* @return : 1 iff there is nothing wrong with the function.
* 0 otherwise
*/
int write_binary_instructions(char *filename,
struct instruct_bin_entry *instructions)
{
struct instruct_bin_entry *instruction = instructions;
FILE *fp;
int name_size = strlen(filename) + 5 + 1;
char *fileout_name;
if ( ! (fileout_name = malloc(sizeof(char) * name_size)) ) {
perror("malloc");
return 0;
}
strncpy(fileout_name, filename, strlen(filename));
fileout_name[strlen(filename)] = '\0';
strncat(fileout_name, ".hack", 6);
fileout_name[strlen(filename) + 5] = '\0';
if (! (fp = fopen(fileout_name, "w")) ) {
free(fileout_name);
}
while (instruction) {
fprintf(fp, "%s\n", instruction->instr);
instruction = instruction->next;
}
free(fileout_name);
fclose(fp);
return 1;
}
int main (int argc, char **argv) {
FILE *fp = NULL;
char *filename;
int status = 0;
char line[INSTR_SIZE];
struct symbol_entry *table = NULL;
struct instruct_bin_entry *instructions = NULL;
if (argc < 2) {
fprintf(stderr, "usage: %s file.asm\n", argv[0]);
status = 1;
return 1;
}
if (!(filename = extractFileName(argv[1]))) {
fprintf(stderr, "%s is an invalid filename\n", argv[1]);
status = 1;
goto cleanup;
}
if (!(fp = fopen(argv[1], "r"))) {
fprintf(stderr, "Failed to open file: %s\n", argv[1]);
perror("fopen");
status = 1;
goto cleanup;
}
if (! (table = init_sym_table()) ) {
status = 1;
goto cleanup;
}
first_pass(table, fp);
if ( ! (instructions = parse_instructions(table, fp)) ) {
status = 1;
goto cleanup;
}
fclose(fp);
fp = NULL;
status = write_binary_instructions(filename, instructions);
cleanup:
if (fp) {
fclose(fp);
}
if (filename) {
free(filename);
}
if (table) {
destroy_table(table);
}
if (instructions) {
destroy_instructions(instructions);
}
return status;
}