hi all, i have this code that takes in some instructions like
READ a // reads value of a from stdinput
READ b //read value of b from stdinput
LOAD a r2 //load value of variable into register (r2)
LOADC #6 r3 // load constant(6) into register r3
STORE c r1 // stores value of register (which is 1) into variable
ADD r1 r2 r3 .. .. //adds contents of r2 and r3 and store it into r3.
The program is supposed to interpret each instruction as i have noted above.
all the variables will go in the mem[5] array and registers in reg[1000] array.
this is what i have done so far, but i think i am no mapping the variables into the array right.
Code:
#include <stdio.h>
#include "CodeGen.h"
#include "InstrUtils.h"
int main(int argc, char *argv[]) {
FILE *infile;
Instruction *instr;
int instrCounter = 0;
int reg[1000]; // array of registers
char a, b, c , d, e;
int mem[5]; //array for variables
int x, y, z;
Instruction *current;
instr = ReadInstructionList(infile); //file contains instructions
current = instr;
while(current!=NULL){
if(current->field1 == a) // not sure??
mem[0] = current->field1;
else if
(current->field1 == b)
mem[1] = current->field1;
else if
(current->field1== c)
mem[2] = current->field1;
else if
(current->field1== d)
mem[3] = current->field1;
else
mem[4] = current->field1;
switch (current->opcode) {
case READ:
{
instrCounter++;
printf("tinyL>> enter value for \"%c\": ", current->field1);
scanf("%d",mem); //did i pass right address here??
break;
}
case WRITE:
{
instrCounter++;
printf("tinyL>> %c = %d\n", current->field1, mem);
break;
}
default:
printf("ILLEGAL INSTRUCTION\n");
case LOAD:
{
x = current->field1;
y = current->field2;
reg[y] =x;
instrCounter++;
break;
}
case LOADC:
{
x = current->field1;
y = current->field2;
reg[y] = x;
instrCounter++;
break;
}
case STORE:
x=current->field1;
y=current->field2;
if(x == a)
mem[0] = y;
else if
(x == b)
mem[1] = y;
else if
(x== c)
mem[2] = y;
else if
(x== d)
mem[3] = y;
else
mem[4] = y;
instrCounter++;
break;
case ADD:
x=current->field1;
y=current->field2;
z=current->field3;
reg[x] = reg[y]+reg[z];
instrCounter++;
break;
}
current = current->next;
}
printf("Executed %d instructions\n", instrCounter);
return 0;
}
i would really appreciate if someone can give me some hints about this,
thanks a lot.