This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
assemble.c
279 lines (258 loc) · 7.26 KB
/
assemble.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* File: assemble.c
* Contains the entry point for the assembly process and functions which conduct the first and second passes
* of the assembly process.
*/
#include "lc3asm.h"
/*
*
* name: assemble
* Invokes firstpass() and secondpass(). This is the function invoked by main()
*
*/
void assemble(files *f)
{
int success;
printf("Assembling \"%s\"\n\n", f->infilename);
printf("First Pass\n");
printf("----------\n");
symbol *symtable = NULL;
firstpass(f, &symtable, &success);
if (!success)
{
printf("First Pass failed\n");
return;
}
printf("First Pass successful\n");
write_symfile(symtable, f);
printf("Symbol table written to file \"%s\"\n", f->symfilename);
printf("\nSecond Pass\n");
printf("-----------\n");
secondpass(f, &symtable, &success);
if (!success)
{
printf("Second Pass failed\n");
return;
}
printf("Second Pass successful\n");
printf("Binary file written to file \"%s\"\n", f->outbinfilename);
printf("Hexadecimal file written to file \"%s\"\n", f->outhexfilename);
destroy_symtable(symtable);
}
/*
*
* name: firstpass
* The first pass of the assembly process. Searches for the ".ORIG" directive, and starts adding labels to the symbol
* table. Then searches for a ".END" directive.
*
*/
void firstpass(files *f, symbol **symtable, int *success)
{
line l;
int LC;
int foundORIG = 0, foundEND = 0;
char op[TOK_LEN];
int opindex;
*success = 0;
while (!feof(f->in))
{
readline(&l, f);
parsetokens(&l);
if (strcmp(l.tokens[0], ".ORIG") == 0)
{
foundORIG = 1;
if (l.tokens[1][0] != '\0')
{
scannumber(l.tokens[1], &LC);
break;
}
else
{
fprintf(stderr, "Invalid .ORIG statement\n");
return;
}
}
}
if (!foundORIG)
{
fprintf(stderr, ".ORIG pseudo-op not found.\n");
return;
}
//.ORIG is found and position indicator is on line below .ORIG statement
while (!feof(f->in))
{
readline(&l, f);
parsetokens(&l);
if (strcmp(l.tokens[0], ".END") == 0)
{
foundEND = 1;
break;
}
if (l.tokens[0][0] != '\0')
{
if (islabel(l.tokens[0]))
{
if (search_table(*symtable, l.tokens[0]) != -1)
{
fprintf(stderr, "Label %s has already appeared in file\n", l.tokens[0]);
return;
}
append_symtable(symtable, l.tokens[0], LC);
if (l.tokens[1][0] != '\0')
{
opindex = 1;
}
else
{
continue;
}
}
else
{
opindex = 0;
}
strcpy(op, l.tokens[opindex]);
if (strcmp(op, ".BLKW") == 0)
{
if (l.tokens[opindex+1][0] == '\0')
{
fprintf(stderr, "Illegal argument to .BLKW directive\n");
return;
}
int block;
scannumber(l.tokens[opindex+1], &block);
LC += block;
}
else if (strcmp(op, ".STRINGZ") == 0)
{
if (l.tokens[opindex+1][0] == '\0')
{
fprintf(stderr, "Illegal argument to .STRINGZ directive\n");
return;
}
LC += strlen(l.tokens[opindex+1]) + 1;
}
else
{
LC++;
}
}
}
if (!foundEND)
{
fprintf(stderr, ".END pseudo-op not found.\n");
return;
}
*success = 1;
}
/*
*
* name: secondpass
* The second pass of the assembly process. Searches for ".ORIG", and starts generating instructions from there.
* Searches the symbol table for addresses of symbols. Writes the LC-3 machine code in binary and hexadecimal to the
* respective files.
*
*/
void secondpass(files *f, symbol **symtable, int *success)
{
line l;
int LC;
int opindex;
char op[TOK_LEN];
*success = 0;
rewind(f->in);
while (!feof(f->in))
{
readline(&l, f);
parsetokens(&l);
if (strcmp(l.tokens[0], ".ORIG") == 0)
{
if (l.tokens[1][0] != '\0')
{
scannumber(l.tokens[1], &LC);
char origlocationbin[17], origlocationhex[17];
int_to_bin(LC, origlocationbin, 16);
fprintf(f->outbin, "%s\n", origlocationbin);
instruction_bintohex(origlocationbin, origlocationhex);
fprintf(f->outhex, "%s\n", origlocationhex);
break;
}
}
}
//position indicator is on line below .ORIG statement
while (!feof(f->in))
{
readline(&l, f);
parsetokens(&l);
//finish reading if .END is encountered
if (strcmp(l.tokens[0], ".END") == 0)
{
break;
}
//continue only if line is not empty
if (l.tokens[0][0] != '\0')
{
/*
fprintf(f->outbin, "%d ", LC);
for (int j=0; l.tokens[j][0] != '\0'; j++)
{
fprintf(f->outbin, "[%s]", l.tokens[j]);
}
fprintf(f->outbin, "\n");
*/
if (islabel(l.tokens[0]))
{
if (l.tokens[1][0] != '\0')
{
opindex = 1;
}
else
{
continue;
}
}
else
{
opindex = 0;
}
int error;
strcpy(op, l.tokens[opindex]);
if (strcmp(op, ".BLKW") == 0)
{
error = handle_BLKW(f, &l, opindex, &LC);
if (error)
{
printf("Generation of Instruction failed\n");
*success = 0;
return;
}
}
else if (strcmp(op, ".STRINGZ") == 0)
{
error = handle_STRINGZ(f, &l, opindex, &LC);
if (error)
{
printf("Generation of Instruction failed\n");
*success = 0;
return;
}
}
else
{
char inst_bin[17], inst_hex[5];
error = generate_instruction(inst_bin, &l, opindex, *symtable, &LC);
if (error)
{
printf("Generation of Instruction failed\n");
*success = 0;
return;
}
fprintf(f->outbin, "%s\n", inst_bin);
instruction_bintohex(inst_bin, inst_hex);
fprintf(f->outhex, "%s\n", inst_hex);
LC++;
}
}
}
*success = 1;
}