Repository navigation
Expand file tree
/
Copy pathsecondpass.c
More file actions
209 lines (183 loc) · 7.23 KB
/
Copy pathsecondpass.c
File metadata and controls
209 lines (183 loc) · 7.23 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
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 "secondpass.h"
/* the second pass over the file adds the addresses of the operands in the command lines
* after each command line.
* the algorithm is as follows:
* check the first word after the label (if there is one). if it's not an instruction line, go to the next line.
* otherwise, check how many operands the command accepts. get that number of operands and get their directing method.
* send those arguments to preCode to get the number of instructions to advance the instruction counter.
* return an indicator whether an error had been found during the second run over the file.
*/
int make_second_pass(FILE *fil, short memory[], LabelList *labelList, ExternalList *extList) {
char line[MAX_LINE], command[MAX_LINE], opSource[MAX_LINE], opTarget[MAX_LINE];
int IC, errorFound, index, numOps, sourceAddressing, targetAddress, lineNumber, answer;
IC = 0;
index = 0;
lineNumber = 0;
errorFound = FALSE;
while (fgets(line, MAX_LINE + 2, fil)) {
memset(opSource, '\0', MAX_LINE);
memset(opTarget, '\0', MAX_LINE);
targetAddress = REACHED_END;
sourceAddressing = REACHED_END;
lineNumber++;
index = move_to_not_white(line, MAX_LINE, 0);
if (index == MAX_LINE || line[index] == ';' || line[index] == '\n')
continue;
insertWord(line, command, &index);
if (!is_label(command))
index = 0;
insertWord(line, command, &index);
if (!is_instruction(command))
continue;
numOps = how_many_ops(command);
IC++;
if (!numOps)
continue;
index = move_to_not_white(line, MAX_LINE, index);
insertWord(line, opSource, &index);
/*get first operand addressing*/
sourceAddressing = source_directing(opSource, lineNumber);
if (numOps == 2) { /*if there are 2 operands get the second operand */
index = move_to_not_white(line, MAX_LINE, index);
if (line[index] == ',') /*skipping comma*/
index = move_to_not_white(line, MAX_LINE, index + 1);
insertWord(line, opTarget, &index);
targetAddress = source_directing(opTarget, index);
}
/* send the two operands to process their binary code*/
answer = preCode(memory, IC, lineNumber, opSource, opTarget, sourceAddressing, targetAddress, labelList,
extList);
if (answer == ERROR)
errorFound = TRUE;
else
IC = answer;
}
freeLabelList(labelList);
if (errorFound)
return ERROR;
else
return IC;
}
/* check if each operand exist. if the two of them exist, check if both are registers, if so create one binary code
* representing the two registers numbers, add that code to the memory and return IC+1. otherwise
* check for each operand if it's a register, if not check for addressing 0-2.
* if the addressing is 0-3 add the operand code to the memory and return the number of instructions advanced.
* if not, return an error
*/
int preCode(short memory[], int IC, int lineNumber, char opSource[], char opTarget[], int sourceAddressing,
int targetAddressing, LabelList *lblList, ExternalList *extList) {
int answer;
if (sourceAddressing == 3 && targetAddressing == 3) {
memory[IC] = (short) (codeSourceRegister(opSource) | codeTargetRegister(opTarget));
return IC + 1;
}
if (sourceAddressing != REACHED_END) {
if (sourceAddressing == 3) {
answer = codeSourceRegister(opSource);
memory[IC++] = (short) answer;
} else {
answer = handelAddressing0and1and2(opSource, sourceAddressing, memory, IC, lblList, extList);
if (answer == FALSE || answer == ERROR)
return printERROR(lineNumber, "operand is not allowed in current context");
else
IC = answer;
}
if (targetAddressing != REACHED_END) {
if (targetAddressing == 3) {
answer = codeTargetRegister(opTarget);
memory[IC++] = (short) answer;
} else {
answer = handelAddressing0and1and2(opTarget, targetAddressing, memory, IC, lblList, extList);
if (answer == FALSE || answer == ERROR)
return printERROR(lineNumber, "operand is not allowed in current context");
else
IC = answer;
}
}
} else
return ERROR;
return IC;
}
/* handel operand addressing methode 0,1,2 and add if the operand's addressing methode matches 0-2
* then add its necessary addresses to the memory. and return the number of instructions added
*/
int handelAddressing0and1and2(char *operand, int addressing, short memory[], int currentIC, LabelList *lblList,
ExternalList *extList) {
short address;
if (addressing == 0) {
int num = getNumberCode(operand);
memory[currentIC] = (short) num;
return currentIC + 1;
}
if (addressing == 1) {
address = (short) labelAddressCode(operand, lblList, extList, currentIC);
memory[currentIC] = address;
return currentIC + 1;
}
/* structure operand return 2 addresses.
one for the operand's address, and one for the first or second field*/
if (addressing == 2) {
int field = 0;
char name[MAX_LINE];
separateStructToNameAndField(operand, name, &field);
address = (short) labelAddressCode(name, lblList, extList, currentIC);
memory[currentIC] = address;
memory[++currentIC] = (short) (field << 2);
return currentIC + 1;
}
return FALSE;
}
/* create a code from the label address.
* if the label is an external, the address is 0 and addressing is ARE field is 1
*/
int labelAddressCode(char *operand, LabelList *lblList, ExternalList *extList, int currentIC) {
Label *labelTemp;
External *extTemp;
int address;
if ((labelTemp = isLabelExist(lblList, operand)) != NULL) {
address = (labelTemp->address << 2) | 2;
return address;
}
if ((extTemp = isExternalExist(extList, operand)) != NULL) {
extTemp->addressList = addAddress(extTemp->addressList, currentIC + START_OF_MEMORY);
return 1;
}
return FALSE;
}
/* separate a structure operand to the structure name and field
*/
void separateStructToNameAndField(char *operand, char name[], int *field) {
int j = 0, length = (int) strlen(operand);
while (j < length && operand[j] != '.') {
name[j] = operand[j];
j++;
}
name[j] = '\0';
*field = operand[++j] - '0';
}
/* create a binary representing the source register number
*/
unsigned short codeSourceRegister(char const *reg) {
unsigned short num = reg[1] - '0';
num <<= 6;
return num;
}
/* create a binary representing the target register number
*/
unsigned short codeTargetRegister(char const *reg) {
unsigned short num = reg[1] - '0';
num <<= 2;
return num;
}
/* return a binary of the number
*/
short getNumberCode(char *word) {
char temp[MAX_LINE];
int index;
short num;
index = move_to_not_white(word, MAX_LINE, 1);
insertWord(word, temp, &index);
num = atoi(temp);
num = num << 2;
return num;
}