interpreteur instructions.c avec les fonctions
This commit is contained in:
parent
7734aea2d2
commit
2733800f4d
1 changed files with 39 additions and 0 deletions
|
@ -13,8 +13,31 @@ int current_line;
|
||||||
int has_error;
|
int has_error;
|
||||||
|
|
||||||
int memory[MAX_MEMORY_SIZE];
|
int memory[MAX_MEMORY_SIZE];
|
||||||
|
int memory_fonc[MAX_MEMORY_SIZE];
|
||||||
|
|
||||||
int EBP=0;
|
int EBP=0;
|
||||||
|
int INDEX_memory_fonc=0;
|
||||||
|
|
||||||
|
int pop(){
|
||||||
|
int res;
|
||||||
|
if(INDEX_memory_fonc >0){
|
||||||
|
res=memory_fonc[INDEX_memory_fonc];
|
||||||
|
memory_fonc[INDEX_memory_fonc]=-1;
|
||||||
|
INDEX_memory_fonc --;
|
||||||
|
}else{
|
||||||
|
printf("erreur pop dans la pile des adresses de retour et sauvegardes de contexte, pile déjà vide\n");
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void push(int arg){
|
||||||
|
if (INDEX_memory_fonc < MAX_MEMORY_SIZE){
|
||||||
|
INDEX_memory_fonc ++;
|
||||||
|
memory_fonc[INDEX_memory_fonc]=arg;
|
||||||
|
}else{
|
||||||
|
printf("erreur push dans la pile des adresses de retour et sauvegardes de contexte, taille excedée\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int exec(int ip);
|
int exec(int ip);
|
||||||
int valid_memory_addr(int address);
|
int valid_memory_addr(int address);
|
||||||
|
@ -154,6 +177,22 @@ int exec(int ip) {
|
||||||
printf("READ @%d[%d] = @%d\n", arg1, memory[arg1], memory[memory[arg1+EBP]]);
|
printf("READ @%d[%d] = @%d\n", arg1, memory[arg1], memory[memory[arg1+EBP]]);
|
||||||
memory[arg1]=memory[memory[arg1+EBP]];
|
memory[arg1]=memory[memory[arg1+EBP]];
|
||||||
break;
|
break;
|
||||||
|
case COPR:
|
||||||
|
printf("COPR @ebp-%d = @%d[%d]\n", arg2, arg1, memory[arg1]);
|
||||||
|
memory[EBP-arg2]=memory[arg1];
|
||||||
|
break;
|
||||||
|
case CALL:
|
||||||
|
printf("CALL memory_fonc: adr_ret=%d; context (ebp)=%d \njump to function to line %d\n", arg3, EBP, arg1);
|
||||||
|
push(arg3);
|
||||||
|
push(EBP);
|
||||||
|
EBP+=arg2;
|
||||||
|
next_ip=arg1;
|
||||||
|
break;
|
||||||
|
case RET:
|
||||||
|
EBP=pop();
|
||||||
|
next_ip=pop();
|
||||||
|
printf("RET return to line %d \nand update ebp to %d\n", next_ip, EBP);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "ERROR run : unknown inst.\n");
|
fprintf(stderr, "ERROR run : unknown inst.\n");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue