-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssembler.c
330 lines (314 loc) · 10.8 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// main.c
// SIMP Assembler
//
// Created by Dan Malka on 28/12/2020.
//
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 256
#define MAXLABELSIZE 50
#define MAXSIZE 500
typedef struct LABEL
{
char LABELNAME[MAXLABELSIZE];
int labeladdress;
struct LABEL* next;
}LABEL;
LABEL* create_label(char label_name[MAXLABELSIZE], int label_address) { //creat new label
LABEL *new_label;
char word[5] = "word";
if (strcmp(word, label_name) == 0)
return NULL;
new_label = (LABEL*)malloc(sizeof(LABEL)); //allocate memmory for new label
if (new_label == NULL) {
printf("Fail to allocate memmory");
return NULL;
}
strcpy(new_label->LABELNAME, label_name); //give label name
new_label->labeladdress = label_address; //number of row
new_label->next = NULL;
return(new_label);
}
LABEL* first_read(FILE* rfile) {
char LINE[MAXSIZE]; // a place to put the line and labels
char strs[6][MAXLABELSIZE]; // a list for the registers name
int i, l, pc_counter, elements;
LABEL* label_list = NULL;
LABEL *current = NULL, *next_label;
char *ptr, *token;
const char clean[10] = " :,.-\t\n\r";
pc_counter = 0;
while (feof(rfile) == 0) { //read next line untill the end of the file
fgets(LINE, MAXSIZE, rfile);
ptr = strchr(LINE, '#'); //clear all string after '#'
if (ptr != NULL) {
*ptr = '\0';
}
token = strtok(LINE, clean); //break the string into tokens
elements = 0;
for (i = 0; i < 6; i++) { // insert the tokens into the list, if there is nothing left insert NULL
if (token == NULL) {
strs[i][0] = NULL;
token = strtok(NULL, clean);
}
else {
strcpy(strs[i], token);
token = strtok(NULL, clean);
elements++;
}
}
if (elements == 1) {//there is only label in this row
if (label_list == NULL) { //insert the first label
label_list = create_label(strs[0], pc_counter);
current = label_list;
}
else {
next_label = create_label(strs[0], pc_counter);//insert new label
current->next = next_label;
current = current->next;
}
}
else if (elements == 6) { //this is a row with label and command
if (label_list == NULL) { //insert the first label
label_list = create_label(strs[0], pc_counter);
current = label_list;
}
else {
next_label = create_label(strs[0], pc_counter);//insert new label
current->next = next_label;
current = current->next;
}
pc_counter += 2; //progress the pc counter by 2
}
else if (elements == 0)
continue;
else {
l = 0;
for (i = 0; i < 6; i++) {
if (strcmp("$imm", strs[i]) == 0) {
pc_counter += 2;
l = 1;
break;
}
else if (strcmp("word", strs[i]) == 0){ // handles .word - not counting pc
l=-1;
break;
}
}
if (l == 0)
pc_counter += 1;
}
}
return (label_list);
}
int searchforlabel(LABEL *label_list, char *label_name) // searches for label_name in label_list
{
int result = 0, found;
LABEL *current;
current = label_list;
found = 0;
while (current != NULL) // iterates until the end of the list
{
if (strcmp(current->LABELNAME, label_name) == 0) { // compares the string to the labelname in current label
found = 1;
result = current->labeladdress; // takes the label address
break;
}
else {
current = current->next; // next label
}
}
if (found == 0) // not found
return (-1);
else
return(result);
}
/* an auxilary struct to be used as a dictionary */
typedef struct { char* str; char* hexa; }dict;
dict register_to_hexa_dict [] =
{
{ "$zero", "0"},
{ "$imm", "1"},
{ "$v0", "2"},
{ "$a0", "3"},
{ "$a1", "4" },
{ "$t0", "5" },
{ "$t1", "6" },
{ "$t2", "7" },
{ "$t3", "8" },
{ "$s0", "9" },
{ "$s1", "A" },
{ "$s2", "B" },
{ "$gp", "C" },
{ "$sp", "D" },
{ "$fp", "E"},
{ "$ra", "F" },
};
/* creating a 'dictionary' for a commands to its hexa lower case only! */
dict command_to_hexa_dict [] =
{
{ "add", "00"},
{ "sub", "01"},
{ "and", "02"},
{ "or", "03"},
{ "xor", "04" },
{ "mul", "05" },
{ "sll", "06" },
{ "sra", "07" },
{ "srl", "08" },
{ "beq", "09" },
{ "bne", "0A" },
{ "blt", "0B" },
{ "bgt", "0C" },
{ "ble", "0D" },
{ "bge", "0E"},
{ "jal", "0F" },
{ "lw", "10" },
{ "sw", "11" },
{ "reti", "12" },
{ "in", "13" },
{ "out", "14" },
{ "halt", "15" },
};
char* getValue(char * str, int mode)
{
dict* pSearch;
char * selected = NULL; //
if (str != NULL)
{
switch (mode)
{
case 1: // command search
/* runing on the dictionary to get the command selected */
for(pSearch = command_to_hexa_dict; pSearch != command_to_hexa_dict + sizeof(command_to_hexa_dict) / sizeof(command_to_hexa_dict[0]); pSearch++ )
{
if(!strcmp( pSearch->str, str))
selected = (pSearch->hexa);
}
if (selected == NULL)
printf("%s is not a valid command name\n", str);
break;
case 2: // register search
/* runing on the dictionary to get the register selected */
for(pSearch = register_to_hexa_dict; pSearch != register_to_hexa_dict + sizeof(register_to_hexa_dict) / sizeof(register_to_hexa_dict[0]); pSearch++ )
{
if(!strcmp( pSearch->str, str))
selected = (pSearch->hexa);
}
if (selected == NULL)
printf("%s is not a valid register name\n", str);
break;
}
}
else
printf("expected command/register name, got NULL\n");
return selected;
}
int main(void)
{
int datamemmory[4096] = { 0 };
FILE *fp, *fw_imemin, *dmemin; // check if needed as "a" mode or "w" mode
fp = fopen("/Users/danmalka/Documents/לימודים/שנה\ ג/סמסטר\ א/מבנה\ המחשב/פרויקט/SIMP\ Assembler/fib.asm", "r");
if (fp == NULL) {
perror("Failed: ");
return 1;
}
LABEL *label_list = first_read(fp);
rewind(fp);
char buffer[MAXSIZE];
// -1 to allow room for NULL terminator for really long string
fw_imemin = fopen("/Users/danmalka/Documents/לימודים/שנה\ ג/סמסטר\ א/מבנה\ המחשב/פרויקט/SIMP\ Assembler/imemin.txt", "w");
while (fgets(buffer, MAXSIZE - 1, fp))
{
int is_imm=0, is_word=0, add_hexa=0, val_hexa=0;
// Remove trailing newline
buffer[strcspn(buffer, "\n")] = 0; // sets the buffer to 0 after the index for end of line
char * pch;
int k = 0;
char* parameters[5]; // init the 5 commands array in the line
pch = strtok (buffer," ,\t:");
if (pch == NULL)
continue;
while (pch != NULL)
{
if (*pch == '#') // reached comments
{
pch = NULL; // to get out of loop
continue;
}
parameters[k++] = pch;//assigns the command in the 5 commands 1-line array
if (strcmp("$imm", pch) == 0)
is_imm = 1;
if (strcmp(".word", pch) == 0)
is_word = 1;
pch = strtok (NULL, " ,\t"); // gets next token
if (is_word){ // handles hexa values for .word command
if (k == 2)
if (parameters[1][1] == 'x' || parameters[1][1] == 'X')
add_hexa = 1; // address hexa flag
if (k == 3)
if (parameters[2][1] == 'x' || parameters[2][1] == 'X')
val_hexa = 1; // value hexa flag
}
}
int imm,label_line,address=0,value=0;
unsigned int imm_5;
label_line = searchforlabel(label_list, parameters[0]);
if (label_line!=-1) // check if it is a label line
continue;
imm = searchforlabel(label_list, parameters[4]); // check if there is a label in the last token
if (imm ==-1) // not label
{
if (parameters[4][1] == 'x' || parameters[4][1] == 'X') // handles immediate hexa values
imm = strtol(parameters[4], NULL, 16);
else
imm = strtol(parameters[4], NULL, 10); // decimal values
imm_5 = (unsigned int)(imm & 0xFFFFF);
}
else // gets label line from imm
{
imm_5 = (unsigned int)(imm & 0xFFFFF);
}
if (is_word){ // a word command
if (add_hexa && val_hexa){ // both value and address are hexa
address = strtol(parameters[1], NULL, 16);
value = strtol(parameters[2], NULL, 16);
}
else if (add_hexa){ // only address is hexa
address = strtol(parameters[1], NULL, 16);
value = strtol(parameters[2], NULL, 10);
}
else if (val_hexa){ // only value is hexa
value = strtol(parameters[2], NULL, 16);
address = strtol(parameters[1], NULL, 10);
}
else{ // none is hexa
address = strtol(parameters[1], NULL, 10);
value = strtol(parameters[2], NULL, 10);
}
}
char* opcode;
char *rd,*rs,*rt;
if (!is_word) { // not a .word command
opcode = getValue(parameters[0], 1); //gets opcode
rd = getValue(parameters[1], 2); // gets current rd register hexa
rs = getValue(parameters[2], 2);// gets current rs register hexa
rt = getValue(parameters[3], 2);// gets current rt register hexa
fprintf(fw_imemin,"%s%s%s%s\n",opcode,rd,rs,rt); // prints current line to file
if (is_imm) // print one more line if uses immediate
fprintf(fw_imemin,"%05X\n",imm_5);
}
else
datamemmory[address] = value;
}
dmemin = fopen("/Users/danmalka/Documents/לימודים/שנה\ ג/סמסטר\ א/מבנה\ המחשב/פרויקט/SIMP\ Assembler/dmemin.txt", "w");
for (int i = 0; i<4096; ++i) // iterates over datamemmory and prints to file its values
fprintf(dmemin,"%08X\n",datamemmory[i]);
fclose(fp);
fclose(dmemin);
fclose(fw_imemin);
return 0;
}