|
@@ -0,0 +1,248 @@
|
|
1
|
+#include "tables.h"
|
|
2
|
+#include <stdio.h>
|
|
3
|
+#include <stdlib.h>
|
|
4
|
+
|
|
5
|
+#define TAILLE_BUFFER_INSTRUCTIONS (1024)
|
|
6
|
+#define TAILLE_MEMOIRE (32)
|
|
7
|
+#define TAILLE_PILE_APPELS (16)
|
|
8
|
+#define TAILLE_BANC_REGISTRES (16)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+/**************************************************/
|
|
12
|
+/**************************************************/
|
|
13
|
+/************ Variables et structures *************/
|
|
14
|
+/**************************************************/
|
|
15
|
+/**************************************************/
|
|
16
|
+
|
|
17
|
+// Structure coding an instruction
|
|
18
|
+struct str_instruction {
|
|
19
|
+ enum instruction_t instruction;
|
|
20
|
+ int param1;
|
|
21
|
+ int param2;
|
|
22
|
+ int param3;
|
|
23
|
+};
|
|
24
|
+
|
|
25
|
+// Buffer stockant le programme
|
|
26
|
+struct str_instruction programme[TAILLE_BUFFER_INSTRUCTIONS * sizeof(struct str_instruction)];
|
|
27
|
+
|
|
28
|
+// Pointeur d'instruction
|
|
29
|
+int eip = 0;
|
|
30
|
+
|
|
31
|
+// Banc de registre
|
|
32
|
+int registres[TAILLE_BANC_REGISTRES];
|
|
33
|
+
|
|
34
|
+// Memoire du programme
|
|
35
|
+int mem[TAILLE_MEMOIRE];
|
|
36
|
+
|
|
37
|
+// Pointeur de base dans la mémoire
|
|
38
|
+int ebp = 0;
|
|
39
|
+
|
|
40
|
+// Memoire du programme
|
|
41
|
+int pile[TAILLE_PILE_APPELS];
|
|
42
|
+
|
|
43
|
+// Pointeur de sommet dans la pile d'appel
|
|
44
|
+int esp = 0;
|
|
45
|
+
|
|
46
|
+// Flag Z
|
|
47
|
+int Z = 0;
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+/**************************************************/
|
|
62
|
+/**************************************************/
|
|
63
|
+/******* Fonctions internes de verification *******/
|
|
64
|
+/**************************************************/
|
|
65
|
+/**************************************************/
|
|
66
|
+
|
|
67
|
+// Verifie qu'une adresse est bien en mémoire (renvoi l'adresse si oui, leve une exception si non)
|
|
68
|
+int check_adresse(int adresse) {
|
|
69
|
+ if (adresse >= TAILLE_MEMOIRE || adresse < 0) {
|
|
70
|
+ printf("\033[31;01m ERROR : \033[00m Segmentation fault de %d avec eip = %d et ebp = %d\n", adresse, eip, ebp);
|
|
71
|
+ exit(2);
|
|
72
|
+ }
|
|
73
|
+ return adresse;
|
|
74
|
+}
|
|
75
|
+
|
|
76
|
+// Verifie qu'un numéro de registre est valide
|
|
77
|
+int check_registre(int registre) {
|
|
78
|
+ if (registre >= TAILLE_BANC_REGISTRES || registre < 0) {
|
|
79
|
+ printf("\033[31;01m ERROR : \033[00m Register error\n");
|
|
80
|
+ exit(2);
|
|
81
|
+ }
|
|
82
|
+ return registre;
|
|
83
|
+}
|
|
84
|
+
|
|
85
|
+// Verifie que les limites de la mémoire d'appels ne sont pas franchies
|
|
86
|
+int check_adresse_pile(int adresse) {
|
|
87
|
+ if (adresse >= TAILLE_PILE_APPELS || adresse < 0) {
|
|
88
|
+ printf("\033[31;01m ERROR : \033[00m Stack error\n");
|
|
89
|
+ exit(2);
|
|
90
|
+ }
|
|
91
|
+ return adresse;
|
|
92
|
+}
|
|
93
|
+
|
|
94
|
+// Verifie que eip ne depasse pas la taille du buffer
|
|
95
|
+int check_eip(int eip) {
|
|
96
|
+ if (eip >= TAILLE_BUFFER_INSTRUCTIONS) {
|
|
97
|
+ printf("\033[31;01m ERROR : \033[00m Taille du buffer insuffisante pour charger le programme\n");
|
|
98
|
+ exit(2);
|
|
99
|
+ } else if (eip < 0) {
|
|
100
|
+ printf("\033[31;01m ERROR : \033[00m EIP < 0\n");
|
|
101
|
+ exit(2);
|
|
102
|
+ }
|
|
103
|
+ return eip;
|
|
104
|
+}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+/**************************************************/
|
|
117
|
+/**************************************************/
|
|
118
|
+/********** Fonction interne d'affichage **********/
|
|
119
|
+/**************************************************/
|
|
120
|
+/**************************************************/
|
|
121
|
+
|
|
122
|
+// Affiche la mémoire
|
|
123
|
+void print() {
|
|
124
|
+ int i;
|
|
125
|
+ printf("EBP : %d\n", ebp);
|
|
126
|
+ for (i=0; i<TAILLE_MEMOIRE; i++) {
|
|
127
|
+ printf("mem[%d] = %d\n", i, mem[i]);
|
|
128
|
+ }
|
|
129
|
+}
|
|
130
|
+
|
|
131
|
+// Affiche les registres
|
|
132
|
+void print_reg() {
|
|
133
|
+ int i;
|
|
134
|
+ for (i=0; i<TAILLE_BANC_REGISTRES; i++) {
|
|
135
|
+ printf("registres[%d] = %d\n", i, registres[i]);
|
|
136
|
+ }
|
|
137
|
+}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+/**************************************************/
|
|
146
|
+/**************************************************/
|
|
147
|
+/************** Instructions Writing **************/
|
|
148
|
+/**************************************************/
|
|
149
|
+/**************************************************/
|
|
150
|
+
|
|
151
|
+// Add a new instruction
|
|
152
|
+void add_instruction(enum instruction_t inst, int param1, int param2, int param3) {
|
|
153
|
+ struct str_instruction my_instruction = {inst, param1, param2, param3};
|
|
154
|
+ programme[check_eip(eip)] = my_instruction;
|
|
155
|
+ eip++;
|
|
156
|
+}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+/**************************************************/
|
|
167
|
+/**************************************************/
|
|
168
|
+/************* Execution du programme *************/
|
|
169
|
+/**************************************************/
|
|
170
|
+/**************************************************/
|
|
171
|
+
|
|
172
|
+// execute le programme
|
|
173
|
+void execute() {
|
|
174
|
+ printf("Debut de l'interpretation\n");
|
|
175
|
+ eip = 0;
|
|
176
|
+ int continuer = 1;
|
|
177
|
+ while (continuer) {
|
|
178
|
+ if (programme[eip].instruction == ADD) {
|
|
179
|
+ registres[check_registre(programme[eip].param1)] = registres[check_registre(programme[eip].param2)] + registres[check_registre(programme[eip].param3)];
|
|
180
|
+ } else if (programme[eip].instruction == SUB) {
|
|
181
|
+ registres[check_registre(programme[eip].param1)] = registres[check_registre(programme[eip].param2)] - registres[check_registre(programme[eip].param3)];
|
|
182
|
+ Z = !registres[check_registre(programme[eip].param1)];
|
|
183
|
+ } else if (programme[eip].instruction == MUL) {
|
|
184
|
+ registres[check_registre(programme[eip].param1)] = registres[check_registre(programme[eip].param2)] * registres[check_registre(programme[eip].param3)];
|
|
185
|
+ } else if (programme[eip].instruction == DIV) {
|
|
186
|
+ registres[check_registre(programme[eip].param1)] = registres[check_registre(programme[eip].param2)] / registres[check_registre(programme[eip].param3)];
|
|
187
|
+ } else if (programme[eip].instruction == INF) {
|
|
188
|
+ registres[check_registre(programme[eip].param1)] = registres[check_registre(programme[eip].param2)] < registres[check_registre(programme[eip].param3)];
|
|
189
|
+ } else if (programme[eip].instruction == SUP) {
|
|
190
|
+ registres[check_registre(programme[eip].param1)] = registres[check_registre(programme[eip].param2)] > registres[check_registre(programme[eip].param3)];
|
|
191
|
+ } else if (programme[eip].instruction == EQU) {
|
|
192
|
+ registres[check_registre(programme[eip].param1)] = registres[check_registre(programme[eip].param2)] == registres[check_registre(programme[eip].param3)];
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+ } else if (programme[eip].instruction == AFC) {
|
|
196
|
+ registres[check_registre(programme[eip].param1)] = programme[eip].param2;
|
|
197
|
+ } else if (programme[eip].instruction == CPY) {
|
|
198
|
+ registres[check_registre(programme[eip].param1)] = registres[check_registre(programme[eip].param2)];
|
|
199
|
+
|
|
200
|
+ } else if (programme[eip].instruction == LOAD) {
|
|
201
|
+ registres[check_registre(programme[eip].param1)] = mem[check_adresse(ebp + programme[eip].param2)];
|
|
202
|
+ } else if (programme[eip].instruction == STORE) {
|
|
203
|
+ mem[check_adresse(ebp + programme[eip].param1)] = registres[check_registre(programme[eip].param2)];
|
|
204
|
+ } else if (programme[eip].instruction == LOADI) {
|
|
205
|
+ registres[check_registre(programme[eip].param1)] = mem[check_adresse(registres[check_registre(programme[eip].param2)])];
|
|
206
|
+ } else if (programme[eip].instruction == STOREI) {
|
|
207
|
+ mem[check_adresse(registres[check_registre(programme[eip].param1)])] = registres[check_registre(programme[eip].param2)];
|
|
208
|
+ } else if (programme[eip].instruction == STOREA) {
|
|
209
|
+ mem[check_adresse(ebp + programme[eip].param1)] = registres[check_registre(programme[eip].param2)] + ebp;
|
|
210
|
+
|
|
211
|
+ } else if (programme[eip].instruction == JMP) {
|
|
212
|
+ eip = programme[eip].param1 - 1;
|
|
213
|
+ } else if (programme[eip].instruction == JMZ) {
|
|
214
|
+ if (Z) {
|
|
215
|
+ eip = programme[eip].param1 - 1;
|
|
216
|
+ }
|
|
217
|
+
|
|
218
|
+ } else if (programme[eip].instruction == GET) {
|
|
219
|
+ printf("Veuillez saisir un nombre : \n");
|
|
220
|
+ scanf("%d", &(registres[check_registre(programme[eip].param1)]));
|
|
221
|
+ } else if (programme[eip].instruction == PRI) {
|
|
222
|
+ printf("%d@%d\n", registres[check_registre(programme[eip].param1)], programme[eip].param1);
|
|
223
|
+
|
|
224
|
+ } else if (programme[eip].instruction == CALL) {
|
|
225
|
+ pile[check_adresse_pile(esp)] = ebp;
|
|
226
|
+ esp++;
|
|
227
|
+ pile[check_adresse_pile(esp)] = eip + 1;
|
|
228
|
+ esp++;
|
|
229
|
+ ebp = ebp + programme[eip].param2;
|
|
230
|
+ eip = programme[eip].param1 - 1;
|
|
231
|
+
|
|
232
|
+ } else if (programme[eip].instruction == RET) {
|
|
233
|
+ esp--;
|
|
234
|
+ eip = pile[check_adresse_pile(esp)] - 1;
|
|
235
|
+ esp--;
|
|
236
|
+ ebp = pile[check_adresse_pile(esp)];
|
|
237
|
+
|
|
238
|
+ } else if (programme[eip].instruction == STOP) {
|
|
239
|
+ if (programme[eip].param1 == 0) {
|
|
240
|
+ continuer = 0;
|
|
241
|
+ print();
|
|
242
|
+ print_reg();
|
|
243
|
+ }
|
|
244
|
+ }
|
|
245
|
+ eip = (eip + 1) % TAILLE_BUFFER_INSTRUCTIONS;
|
|
246
|
+ //printf("EIP : %d ; Z : %d\n", eip, Z);
|
|
247
|
+ }
|
|
248
|
+}
|