-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevm2.c
311 lines (257 loc) · 4.21 KB
/
evm2.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "vm_codops.h"
#define MAX_CODESEGMENT_SIZE 1000
#define MAX_STACK_SIZE 100
// Les registres de la machine virtuelle
int pc;
int sp;
int bp;
// Declaration du segment de code
int codeSegment[MAX_CODESEGMENT_SIZE];
// Declaration de la pile d'execution (tableau de float)
float pile[MAX_STACK_SIZE];
// Toutes les fonctions representant les instructions de la MV
void inst_add()
{
pile[sp-1]=pile[sp-1]+pile[sp];
sp-- ; pc++ ;
printf("add\n");
}
void inst_sub()
{
pile[sp-1]=pile[sp-1]-pile[sp];
sp-- ; pc++ ;
}
void inst_mult()
{
pile[sp-1]=pile[sp-1]*pile[sp];
sp-- ; pc++ ;
printf("mult\n");
}
void inst_div()
{
pile[sp-1]=pile[sp-1]/pile[sp];
sp-- ; pc++ ;
}
void inst_divi()
{
pile[sp-1]=(float)((int)pile[sp-1]/(int)pile[sp]);
sp-- ; pc++ ;
}
void inst_neg()
{
pile[sp]=-pile[sp];
pc++ ;
}
void inst_and()
{
pile[sp-1]=(float)((int)pile[sp-1] & (int)pile[sp]);
sp-- ; pc++ ;
}
void inst_or()
{
pile[sp-1]=(float)((int)pile[sp-1] | (int)pile[sp]);
sp-- ; pc++ ;
}
void inst_not()
{
pile[sp]=(float)(1-(int)pile[sp]);
pc++ ;
}
void inst_eq()
{
pile[sp-1]=(pile[sp-1]==pile[sp]) ? 1.0 : 0.0;
sp-- ; pc++ ;
}
void inst_ls()
{
pile[sp-1]=(pile[sp-1]<pile[sp]) ? 1.0 : 0.0;
sp-- ; pc++ ;
}
void inst_gt()
{
pile[sp-1]=(pile[sp-1]>pile[sp]) ? 1.0 : 0.0;
sp-- ; pc++ ;
}
void inst_inc()
{
sp=sp+codeSegment[pc+1];
pc+=2 ;
}
void inst_dec()
{
sp=sp-codeSegment[pc+1];
pc+=2 ;
}
void inst_push()
{
sp++;
pile[sp]=codeSegment[pc+1];
pc=pc+2;
}
void inst_pushr()
{
char strReal[256];
int strRealLength,pos;
float fv;
strRealLength=0;
pos=pc+1;
while (codeSegment[pos]!=0)
{
strReal[strRealLength]=(char)codeSegment[pos];
strRealLength++;
pos++;
}
strReal[strRealLength]='\0';
sscanf(strReal,"%f",&fv);
sp++;
pile[sp]=fv;
printf("pushr %f\n",fv);
pc=pos+1;
}
void inst_stm()
{
float fv;
int dest;
fv=pile[sp];
dest=(int)pile[sp-1];
pile[dest]=fv;
pc++; sp-=2;
}
void inst_mts()
{
int dest, orig;
dest=sp;
orig=(int)pile[sp];
pile[dest]=pile[orig];
pc++;
}
void inst_jp()
{
pc=codeSegment[pc+1];
}
void inst_jf()
{
if (pile[sp]==0.0)
pc=codeSegment[pc+1];
else
pc=pc+2;
sp--;
}
void inst_input()
{
float fv;
scanf("%f",&fv);
pile[(int)pile[sp]]=fv;
sp--;
pc++;
}
void inst_output()
{
printf("%f\n",pile[sp]);
sp--;
pc++;
}
void inst_outchar()
{
char strReal[256];
int strRealLength,pos;
strRealLength=0;
pos=pc+1;
while (codeSegment[pos]!=0)
{
strReal[strRealLength]=(char)codeSegment[pos];
strRealLength++;
pos++;
}
strReal[strRealLength]='\0';
printf("%s\n",strReal);
pc=pos+1;
}
void (*instructions[])()=
{
NULL,
inst_add,
inst_sub,
inst_mult,
inst_div,
inst_divi,
inst_neg,
inst_and,
inst_or,
inst_not,
inst_eq,
inst_ls,
inst_gt,
inst_inc,
inst_dec,
inst_push,
inst_pushr,
inst_stm,
inst_mts,
inst_jp,
inst_jf,
inst_input,
inst_output,
inst_outchar,
};
// Fonction de lecture d'un fichier langage machine
// ouvert et dont le pointeur de fichier est fin
void readAssembly(FILE *fin)
{
int nbinst;
char line[100];
int i;
int pc,v;
fgets(line,100,fin);
sscanf(line,"%d",&nbinst);
for (i=0;i<nbinst;i++)
{
fgets(line,100,fin);
sscanf(line,"%d:%d",&pc,&v);
codeSegment[i]=v;
}
}
// Boucle principale d'exécution de la machine virtuelle
// On sort de cette fonction si l'instruction pointee
// par pc est OP_HALT. On traitera les erreurs et on
// pensera a ajouter des traces et des moyens de mise
// au point
void run()
{
int inst;
inst=codeSegment[pc];
while (inst!=OP_HALT)
{
(*instructions[inst])();
inst=codeSegment[pc];
}
}
// Programme principal. Apres les tests de nombre et de qualite
// des arguments, ouvrir le fichier langage machine, appeler
// la fonction readAssembly, et fermer le fichier ouvert. Ensuite
// initialiser pc a 0, sp et bp a -1. Enfin, lancer la machine
// virtuelle avec la fonction run().
int main(int argc, char **argv)
{
//int i;
if (argc!=2)
{
printf("Usage : vm infile.bin\n");
}
FILE *fin=fopen(argv[1],"r");
if (fin==NULL)
{
printf("Error opening read file %s\n",argv[1]);
exit(1);
}
readAssembly(fin);
fclose(fin);
pc=0;
sp=-1;
bp=0;
run();
return 0;
}