-
Notifications
You must be signed in to change notification settings - Fork 0
/
translating.c
207 lines (191 loc) · 5.61 KB
/
translating.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
#include "translating.h"
#define print(a) printf(#a "\n");f/*for debugging*/
#define error() fprintf(stderr,"In file '%s' line %d: \n\t ",cur_file, line_num);
/*translate_instruction(line_ptr instruction)*/
/*return: a pointer to machine code translation of the principal instruction*/
/*param: line_ptr, pointer to line of type instruction (i.e. with opcode) in the line list*/
Principal_Instruction_Machine_Code * translate_instruction(line_ptr instruction)
{
/*allocate space*/
Principal_Instruction_Machine_Code * translation = (Principal_Instruction_Machine_Code *) malloc(sizeof(Principal_Instruction_Machine_Code));
if(!translation)
{
fprintf(stderr, "Error: Memory Allocation Failed\n");
exit(EXIT_FAILURE);
}
/*fill in the fields of the translation according to the parameters*/
translation->are = ABSOLUTE;
translation->opcode = instruction->opcode;
if((opcodes_arr[instruction->opcode]).req_parametes==0)
{/*no parameters so set all to immediate*/
translation->addressing_mode_dest=IMMEDIATE;
translation->addressing_mode_src=IMMEDIATE;
translation->parm1=IMMEDIATE;
translation->parm2=IMMEDIATE;
}
if((opcodes_arr[instruction->opcode]).req_parametes==1)
{
if(instruction->jump_syntax==FALSE)
{
translation->addressing_mode_src=IMMEDIATE;
translation->parm1=IMMEDIATE;
translation->parm2=IMMEDIATE;
if(is_register(instruction->operand2)==TRUE)
{
translation->addressing_mode_dest=REG_DIRECT;
}
else if((instruction->operand2)[0]=='#')
{
translation->addressing_mode_dest=IMMEDIATE;
}
else translation->addressing_mode_dest=DIRECT;
}
else/*jump_syntax==TRUE*/
{
char temp[MAX_SYMBOL_NAME];
translation->addressing_mode_src=IMMEDIATE;
if(is_register(instruction->operand2)==TRUE)
{
translation->addressing_mode_dest=REG_DIRECT;
}
else
{
if( (strcmp((opcodes_arr[instruction->opcode]).name,"jmp")==0) ||(strcmp((opcodes_arr[instruction->opcode]).name,"bne")==0) || (strcmp((opcodes_arr[instruction->opcode]).name,"jsr")==0))
{
translation->addressing_mode_dest=JUMP;
}
else
{
translation->addressing_mode_dest=DIRECT;
}
}
strcpy(temp,instruction->jump_op1);
if(is_register(temp)==TRUE)
{
translation->parm1=REG_DIRECT;
}
else if(temp[0]=='#')
{
translation->parm1=IMMEDIATE;
}
else
{
translation->parm1=DIRECT;
}
strcpy(temp,instruction->jump_op2);
if(is_register(temp)==TRUE)
{
translation->parm2=REG_DIRECT;
}
else if(temp[0]=='#')
{
translation->parm2=IMMEDIATE;
}
else
{
translation->parm2=DIRECT;
}
}
}
if((opcodes_arr[instruction->opcode]).req_parametes==2)
{
char temp[MAX_SYMBOL_NAME];
translation->parm1=IMMEDIATE;
translation->parm2=IMMEDIATE;
strcpy(temp,instruction->operand1);
if(is_register(temp)==TRUE)
{
translation->addressing_mode_src=REG_DIRECT;
}
else if(temp[0]=='#')
{
translation->addressing_mode_src=IMMEDIATE;
}
else
{
translation->addressing_mode_src=DIRECT;
}
strcpy(temp,instruction->operand2);
if(is_register(temp)==TRUE)
{
translation->addressing_mode_dest=REG_DIRECT;
}
else
{
translation->addressing_mode_dest=DIRECT;
}
}
return translation;
}
/*translate_register_param(int n_first_reg, int n_second_reg)*/
/*return a pointer to machine code translation of the regular instruction*/
/*param: int n_first_reg the number of the first register - 0 if none*/
/*param: int n_second_reg the number of the second register - 0 if none*/
Register_Param_Machine_Code * translate_register_param(int n_first_reg, int n_second_reg)
{
Register_Param_Machine_Code * translation =(Register_Param_Machine_Code *) malloc(sizeof(Register_Param_Machine_Code));
if(!translation)
{
fprintf(stderr, "Error: Memory Allocation Failed\n");
exit(EXIT_FAILURE);
}
translation->are=ABSOLUTE;
translation->src = n_first_reg;
translation->dest = n_second_reg;
return translation;
}
/*translate_regular_param(char * parm)*/
/*return a pointer to machine code translation of the regular instruction*/
/*param: char parm[] a regular parameter , either a symbol name or an immediate parameter*/
Regular_Param_Machine_Code * translate_regular_param(char * parm)
{
Boolean is_immediate = (parm[0]=='#')? TRUE : FALSE;
Boolean sign = ((is_immediate == TRUE) && (!isdigit(parm[1])))? TRUE : FALSE;
Boolean is_negative = ((sign == TRUE) && (parm[1]=='-'))? TRUE : FALSE;
Regular_Param_Machine_Code * translation = (Regular_Param_Machine_Code *) malloc (sizeof(Regular_Param_Machine_Code ));
if(!translation)
{
fprintf(stderr, "Error: Memory Allocation Failed\n");
exit(EXIT_FAILURE);
}
if(is_immediate == FALSE)/*the it should be a symbol in the symbol table*/
{
int loc;
symbol_ptr symbol = get_symbol(symbol_list_h, parm);
if(symbol==NULL)
{
error()
fprintf(stderr, "Error: Symbol %s was not declared\n",parm);
can_be_assembled=FALSE;
free(translation);
translation=NULL;
return NULL;
}
translation->are = ((symbol->external)==TRUE)? EXTERNAL : RELOCATABLE;
if((symbol->external)==TRUE)
{
write_external(parm);
}
loc = symbol->location;
translation->param =loc;/*assuming it fits*/
return translation;
}
/*if it is immediate*/
translation->are=ABSOLUTE;
if(sign==FALSE)
{
translation->param = atoi(parm+1);/*atoi the string after the '#'*/
return translation;
}
if(sign==TRUE)
{
if(is_negative==FALSE)
{
translation->param = atoi(parm+2);/*atoi the string after'#+'*/
return translation;
}
}
/*is negative == true*/
translation->param = - atoi(parm+2);/*atoi the string after'#-'*/
return translation;
}