-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstPassCmdCoding.c
124 lines (91 loc) · 3.01 KB
/
FirstPassCmdCoding.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
#include "declarationsHeader.h"
size_t memoryWords;
/*
* Calculating the number of memory words the line will take.
*/
void calCmdLine(int *IC) {
analyseParameters();
memoryWords += labelBeforeParam() ? 1 : 0;
if (lineWithLabelDefinition())
updateLabelAddress(lineListHead->str, IC);
incMemoryWordsByOne();
(*IC) += memoryWords;
}
/*
* Counting the number of parameters in the line & inserting them into a list
*/
void analyseParameters() {
char lineCpy[LINE_SIZE], cmd[CMD_LENGTH], tmp[LINE_SIZE];
linePtr ptr = lineListHead;
strcpy(lineCpy, line);
getCommand(cmd);
if (noParameters(cmd))
return;
ptr = movePtrToFirstParameter(ptr, cmd);
moveLineAfterSymbol(lineCpy);
/*tmp pointing to the line since the first string after the command.*/
strcpy(tmp, strstr(lineCpy, ptr->str));
strcpy(lineCpy, tmp);
removeSpaces(lineCpy);
if (twoParameters(lineCpy)) {
codeTwoParameters(lineCpy);
} else {
codeOneParameter(lineCpy);
}
}
void moveLineAfterSymbol(char* lineCpy) {
char lineCpy2[LINE_SIZE];
if (strchr(lineCpy, ISLABEL) != NULL) {/*Moving the lineCpy after the symbol.*/
strcpy(lineCpy2, line);
strcpy(lineCpy, (strchr(lineCpy2, ISLABEL)));
}
}
void codeOneParameter(char *lineCpy) {
char firstParam[STRING_SIZE];
strcpy(firstParam, lineCpy);
addToParamsList(firstParam, NULL);
checkForFloatErrors(firstParam, NULL);
incMemoryWordsByOne();
}
void codeTwoParameters(char *lineCpy) {
char firstParam[STRING_SIZE], secondParam[STRING_SIZE];
size_t i = 0, j = 0;
incMemoryWordsByTwo();
if (strchr(lineCpy, O_BRACKETS) != NULL) {/* Line has brackets.*/
strcpy(lineCpy, strchr(lineCpy, O_BRACKETS) + 1);
while ((firstParam[i] = lineCpy[i]) != COMMA)/*Getting first param.*/
i++;
firstParam[i++] = STR_END;
while ((secondParam[j++] = lineCpy[i]) != C_BRACKETS)/*Getting second param.*/
i++;
secondParam[--j] = STR_END;
addToParamsList(firstParam, secondParam);
} else {/* Just parameters.*/
strcpy(firstParam, strtok(lineCpy, COMMA_STR));
strcpy(secondParam, strtok(NULL, COMMA_STR));
addToParamsList(firstParam, secondParam);
}
memoryWords += bothParametersAreRegisters(firstParam, secondParam) ? -1
: 0;/*Two reg's takes one word.*/
checkForFloatErrors(firstParam, secondParam);
}
void incMemoryWordsByOne() {
memoryWords++;
}
void incMemoryWordsByTwo() {
memoryWords += 2;
}
linePtr movePtrToFirstParameter(linePtr ptr, char *cmd) {
for (ptr = lineListHead; ptr; ptr = ptr->next) {
if (strstr(ptr->str, cmd)) {
ptr = ptr->next;
return ptr;
}
}
}
boolean noParameters(char *cmd) {
return (strcmp(cmd, "stop") == EQUAL || strcmp(cmd, "rts") == EQUAL);
}
boolean twoParameters(char *lineCpy) {
return strchr(lineCpy, COMMA) != NULL;
}