-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementLine.c
More file actions
145 lines (134 loc) · 6.49 KB
/
implementLine.c
File metadata and controls
145 lines (134 loc) · 6.49 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "implementLine.h"
#include "globals.h"
#define OPCODE_LOCATION 5
#define SOURCE_OP_LOCATION 2
#define FIRST_REGISTER_LOCATION 2
#define SECOND_REGISETR_LOCATION 7
#define DEST_OP_LOCATION 9
#define DIRECT_OP_LOCATION 2
#define REGISETR_DEST_LOCATION 2
short instruction_first_word(lineInfo line, int lineNum, label **secondPassTable);
short instruction_second_word(lineInfo line, int lineNum, label **secondPassTable);
short instruction_third_word(lineInfo line, int lineNum, label **secondPassTable);
void implementLine(lineInfo line, label **instrSymbolTableLast, label **directiveSymbolTableLast,
label **secondPassTable, label **externName, label **entryName, short instructionMap[][2], short directiveMap[][2], int lineNum){
int i;
switch(line.subType){
case group1:
if (*line.labelName != '\0')/*if it starts with a label*/
*instrSymbolTableLast = add_label(line.labelName,STARTING_LINE + ic, 0, *instrSymbolTableLast);
/*write the first word into instructionMap and update ic*/
instructionMap[ic][0] = STARTING_LINE + ic;
instructionMap[ic][1] = instruction_first_word(line, lineNum, secondPassTable);
ic++;
/*write the second word into instructionMap and update ic*/
instructionMap[ic][0] = STARTING_LINE + ic;
instructionMap[ic][1] = instruction_second_word(line, lineNum, secondPassTable);
ic++;
/*unless both operand are registers, group1 instruction gets a third word*/
if (!(line.sourceOpMethod == REG_OP_METHOD && line.destOpMethod == REG_OP_METHOD)){
instructionMap[ic][0] = STARTING_LINE + ic;
instructionMap[ic][1] = instruction_third_word(line, lineNum, secondPassTable);
ic++;
}
break;
case group2:
if (*line.labelName != '\0')/*if it starts with a label*/
*instrSymbolTableLast = add_label(line.labelName,STARTING_LINE + ic, 0, *instrSymbolTableLast);
/*write the first word into instructionMap and update ic*/
instructionMap[ic][0] = STARTING_LINE + ic;
instructionMap[ic][1] = instruction_first_word(line, lineNum, secondPassTable);
ic++;
/*write the second word into instructionMap and update ic*/
instructionMap[ic][0] = STARTING_LINE + ic;
instructionMap[ic][1] = instruction_second_word(line, lineNum, secondPassTable);
ic++;
break;
case group3:
if (*line.labelName != '\0')/*if it starts with a label*/
*instrSymbolTableLast = add_label(line.labelName,STARTING_LINE + ic, 0, *instrSymbolTableLast);
/*write the first word into instructionMap and update ic*/
instructionMap[ic][0] = STARTING_LINE + ic;
instructionMap[ic][1] = instruction_first_word(line, lineNum, secondPassTable);
ic++;
break;
case data:
if (*line.labelName != '\0')/*if it starts with a label*/
*directiveSymbolTableLast = add_label(line.labelName, dc , 0, *directiveSymbolTableLast);
for (i=0; i<line.numOfDataParameters; i++){
directiveMap[dc][0] = dc;
directiveMap[dc][1] = line.dataParameters[i];
dc++;
}
break;
case string:
if (*line.labelName != '\0')/*if it starts with a label*/
*directiveSymbolTableLast = add_label(line.labelName, dc , 0, *directiveSymbolTableLast);
for(i=0; i<=strlen(line.string);i++){
directiveMap[dc][0] = dc;
directiveMap[dc][1] = line.string[i];
dc++;
}
break;
case external:
*externName = add_label(line.labelRef,0, lineNum,*externName);
break;
case entry:
*entryName = add_label(line.labelRef,0, lineNum,*entryName);
}
}
short instruction_first_word(lineInfo line, int lineNum, label **secondPassTable){
short res;
res = 0;
res = res | (line.sourceOpMethod<<DEST_OP_LOCATION);
res = res | (line.opCode<<OPCODE_LOCATION);
res = res | (line.destOpMethod<<SOURCE_OP_LOCATION);
return res;
}
short instruction_second_word(lineInfo line, int lineNum, label **secondPassTable){
short res;
res = 0;
/*an irregular case in which a group1 instruction get only one word for both operand*/
if (line.sourceOpMethod == REG_OP_METHOD && line.destOpMethod == REG_OP_METHOD){
res = res|(line.destOp<<FIRST_REGISTER_LOCATION);
res = res|(line.sourceOp<<SECOND_REGISETR_LOCATION);
}
if (line.subType == group1){ /*in this case the second word represent the source op*/
if (line.sourceOpMethod == IMEDIATE_OP_METHOD)/*if the op is a number*/
res = res | (line.sourceOp<<DIRECT_OP_LOCATION);
else if (line.sourceOpMethod == DIRECT_OP_METHOD)
/*leave the line empty.*/
*secondPassTable = add_label(line.sourceOpLabel,ic + STARTING_LINE, lineNum,*secondPassTable);
else /*source op is a register*/
res = res | (line.sourceOp<<SECOND_REGISETR_LOCATION);
}
else{/*the second word represents the dest op*/
if (line.destOpMethod == IMEDIATE_OP_METHOD)/*if the op is a number*/
res = res | (line.destOp<<DIRECT_OP_LOCATION);
else if (line.destOpMethod == DIRECT_OP_METHOD){ /* op is a label*/
/*leave the line empty.*/
*secondPassTable = add_label(line.destOpLabel,ic + STARTING_LINE,lineNum ,*secondPassTable);
}
else /*source op is a register*/
res = res | (line.destOp<<REGISETR_DEST_LOCATION);
}
return res;
}
/*the third word only apply when its a group1 command that doesnt have 2 registers.*/
short instruction_third_word(lineInfo line, int lineNum, label **secondPassTable){
short res;
res = 0;
/*the third word represent the dest op*/
if (line.destOpMethod == IMEDIATE_OP_METHOD)/*if the dest op is a number*/
res = res | (line.destOp<<DIRECT_OP_LOCATION);
else if (line.destOpMethod == DIRECT_OP_METHOD){ /* dest op is a label*/
/*leave the line empty.*/
*secondPassTable = add_label(line.destOpLabel,ic+ STARTING_LINE, lineNum, *secondPassTable);
}
else /*dest op is a register*/
res = res | (line.destOp<<REGISETR_DEST_LOCATION);
return res;
}