-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessFile.c
More file actions
88 lines (71 loc) · 2.71 KB
/
Copy pathprocessFile.c
File metadata and controls
88 lines (71 loc) · 2.71 KB
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
#include "processFile.h"
void initializeVariables(short data[], short memory[]);
/* process the post macro file.
* send the file for the firs run to process, if any errors been found, don't proceed for the second process.
* otherwise, update the labels and entries addresses and send the file for the second process.
* if during the second process there were no errors, create output files for the file.
*/
int processFile(FILE *amFile, char *fileName) {
int answer, IC, DC;
short data[RAM], memory[RAM];
IC = 0, DC = 0;
LabelList *labelList = initializeLabelList();
EntryList *entryList = initializeEntryList();
ExternalList *externalList = initializeExternalList();
initializeVariables(data, memory);
answer = make_first_pass(amFile, memory, data, &IC, &DC, labelList, entryList, externalList);
if (answer == TRUE)
return printERROR(-1, "an error has been found during process");
else {
updateData(IC, labelList, entryList);
rewind(amFile);
/*start second process*/
IC = make_second_pass(amFile, memory, labelList, externalList);
if (IC != ERROR) {
createFiles(fileName, memory, data, IC, DC, entryList, externalList);
} else
return printERROR(-1, "an Error had been found. can't create output files");
}
return 0;
}
/* set all the data and memory cells to 0
*/
void initializeVariables(short data[], short memory[]) {
memset(data, 0, RAM);
memset(memory, 0, RAM);
}
/* update all data labels addresses to be at their location+IC+start of memory (usually 100)
*/
void updateDataLabelAddresses(LabelList *labelList, int IC) {
Label *labelTemp;
labelTemp = labelList->firstLabel;
while (labelTemp != NULL) {
if (labelTemp->type == DATA)
labelTemp->address += IC + START_OF_MEMORY;
labelTemp = labelTemp->next;
}
}
/* add to each entry the address of the co-responding label defined in the file
*/
void addAddressesToEntries(LabelList *lblList, EntryList *entryList) {
Label *labelTemp;
Entry *entryTemp;
entryTemp = entryList->firstEntry;
while (entryTemp != NULL) {
labelTemp = lblList->firstLabel;
while (labelTemp != NULL) {
if (!strcmp(entryTemp->name, labelTemp->name)) {
entryTemp->address = labelTemp->address;
break;
}
labelTemp = labelTemp->next;
}
entryTemp = entryTemp->next;
}
}
/* update all addresses of the data labels and put all data at the end of memory stack
*/
void updateData(int IC, LabelList *labelList, EntryList *entryList) {
updateDataLabelAddresses(labelList, IC);
addAddressesToEntries(labelList, entryList);
}