Question
Help with creating a virtual machine. I dont understand how to print the stack trace. This is the input: 6 0 0 6 1 0
Help with creating a virtual machine. I dont understand how to print the stack trace.
This is the input:
6 0 0 6 1 0 0 3 4 0 0 4 1 0 0 1 4 0 0 5 5 0 0 7 7 0 0 19 6 0 0 4 3 0 1 4 3 1 1 5 13 1 0 1 4 1 1 5 1 1 0 1 12 0 0 1 4 0 1 4 18 0 0 1 8 0 0 18 5 0 1 7 2 0 0 0 3 0 0 5 9 0 0 1 9 0 0 3
This is the output:
Factorial Op Printout:
0 inc 0 0 6
1 lit 0 0 3
2 sto 0 0 4
3 lit 0 0 1
4 sto 0 0 5
5 cal 0 0 7
6 jmp 0 0 19
7 inc 0 0 4
8 lod 0 1 4
9 lod 1 1 5
10 mul 1 0 1
11 sto 1 1 5
12 lit 1 0 1
13 sub 0 0 1
14 sto 0 1 4
15 neq 0 0 1
16 jpc 0 0 18
17 cal 0 1 7
18 rtn 0 0 0
19 lod 0 0 5
20 sio 0 0 1
21 sio 0 0 3
Factorial Stack Trace:
Inital Values pc bp sp
0 inc 0 0 6 1 1 6 0 1 0 0 0 0
1 lit 0 0 3 2 1 6 0 1 0 0 0 0
2 sto 0 0 4 3 1 6 0 1 0 0 3 0
3 lit 0 0 1 4 1 6 0 1 0 0 3 0
4 sto 0 0 5 5 1 6 0 1 0 0 3 1
5 cal 0 0 7 7 7 6 0 1 0 0 3 1
7 inc 0 0 4 8 7 10 0 1 0 0 3 1 | 0 1 1 6
8 lod 0 1 4 9 7 10 0 1 0 0 3 1 | 0 1 1 6
9 lod 1 1 5 10 7 10 0 1 0 0 3 1 | 0 1 1 6
10 mul 1 0 1 11 7 10 0 1 0 0 3 1 | 0 1 1 6
11 sto 1 1 5 12 7 10 0 1 0 0 3 3 | 0 1 1 6
12 lit 1 0 1 13 7 10 0 1 0 0 3 3 | 0 1 1 6
13 sub 0 0 1 14 7 10 0 1 0 0 3 3 | 0 1 1 6
14 sto 0 1 4 15 7 10 0 1 0 0 2 3 | 0 1 1 6
15 neq 0 0 1 16 7 10 0 1 0 0 2 3 | 0 1 1 6
16 jpc 0 0 18 17 7 10 0 1 0 0 2 3 | 0 1 1 6
17 cal 0 1 7 7 11 10 0 1 0 0 2 3 | 0 1 1 6
7 inc 0 0 4 8 11 14 0 1 0 0 2 3 | 0 1 1 6 | 0 1 7 18
8 lod 0 1 4 9 11 14 0 1 0 0 2 3 | 0 1 1 6 | 0 1 7 18
9 lod 1 1 5 10 11 14 0 1 0 0 2 3 | 0 1 1 6 | 0 1 7 18
10 mul 1 0 1 11 11 14 0 1 0 0 2 3 | 0 1 1 6 | 0 1 7 18
11 sto 1 1 5 12 11 14 0 1 0 0 2 6 | 0 1 1 6 | 0 1 7 18
12 lit 1 0 1 13 11 14 0 1 0 0 2 6 | 0 1 1 6 | 0 1 7 18
13 sub 0 0 1 14 11 14 0 1 0 0 2 6 | 0 1 1 6 | 0 1 7 18
14 sto 0 1 4 15 11 14 0 1 0 0 1 6 | 0 1 1 6 | 0 1 7 18
15 neq 0 0 1 16 11 14 0 1 0 0 1 6 | 0 1 1 6 | 0 1 7 18
16 jpc 0 0 18 18 11 14 0 1 0 0 1 6 | 0 1 1 6 | 0 1 7 18
18 rtn 0 0 0 18 7 10 0 1 0 0 1 6 | 0 1 1 6
18 rtn 0 0 0 6 1 6 0 1 0 0 1 6
6 jmp 0 0 19 19 1 6 0 1 0 0 1 6
19 lod 0 0 5 20 1 6 0 1 0 0 1 6
20 sio 0 0 1 21 1 6 0 1 0 0 1 6
21 sio 0 0 3 0 1 0 0
Factorial Output:
6
Here are the operation instructions:
The code (curious to know if I have an error for case 9):
#include
#include
#define MAX_STACK_HEIGHT 2000
#define MAX_CODE_LENGTH 500
#define MAX_LEXI_LEVELS 3
int base(int l, int base);
void fetch(void);
void execute(void);
void print(int count);
void printStack(void);
typedef struct instruction
{
int op; //opcode
int r; // reg
int l; // L
int m; // M
} instruction;
int stack[MAX_STACK_HEIGHT] = {0};
int sp = 0, bp = 1, pc = 0;
int r[16] = {0};
instruction code[MAX_CODE_LENGTH];
instruction ir;
FILE *ifp;
int halt = 0;
int main(void)
{
ifp = fopen("code.txt", "r");
if (ifp == NULL)
{
printf("File could not open. Program halted. ");
}
//Keep track of number of instructions the input file.
int count = 0;
//Read in the lines from the input file.
while(!feof(ifp))
{
fscanf(ifp, "%d", &code[count].op);
fscanf(ifp, "%d", &code[count].r);
fscanf(ifp, "%d", &code[count].l);
fscanf(ifp, "%d", &code[count].m);
count++;
}
fprintf(ifp, "%d", code[count].op);
print(count);
count = 0;
printf(" Factorial Stack Trace: ");
printf("Initial Values\t\tpc\tbp\tsp ");
while (halt == 0)
{
//Start the fetch part of the fetch-execute cycle.
fetch();
//Start the execution process of the
execute();
printf("%d\t%d\t%d\t", ir.r, ir.l, ir.m);
printf("%d\t%d\t%d\t", pc, bp, sp);
printStack();
printf(" ");
if ((pc == 0) && (bp == 0) && (sp == 0))
halt = 1;
}
printf(" Factorial Output: %d ", r[ir.r]);
fclose(ifp);
return 0;
}
void print(int count)
{
int i;
printf("Factorial OP Printout:");
for (i = 0; i
{
int op = code[i].op;
//Interpret the operation.
switch(op)
{
case 1: printf(" %d\t", i); printf("lit\t"); break;
case 2: printf(" %d\t", i); printf("rtn\t"); break;
case 3: printf(" %d\t", i); printf("lod\t"); break;
case 4: printf(" %d\t", i); printf("sto\t"); break;
case 5: printf(" %d\t", i); printf("cal\t"); break;
case 6: printf(" %d\t", i); printf("inc\t"); break;
case 7: printf(" %d\t", i); printf("jmp\t"); break;
case 8: printf(" %d\t", i); printf("jpc\t"); break;
case 9: printf(" %d\t", i); printf("sio\t"); break;
case 10:printf(" %d\t", i); printf("sio\t"); break;
case 11:printf(" %d\t", i); printf("sio\t"); break;
case 12:printf(" %d\t", i); printf("neg\t"); break;
case 13:printf(" %d\t", i); printf("add\t"); break;
case 14:printf(" %d\t", i); printf("sub\t"); break;
case 15:printf(" %d\t", i); printf("mul\t"); break;
case 16:printf(" %d\t", i); printf("div\t"); break;
case 17:printf(" %d\t", i); printf("odd\t"); break;
case 18:printf(" %d\t", i); printf("mod\t"); break;
case 19:printf(" %d\t", i); printf("eql\t"); break;
case 20:printf(" %d\t", i); printf("neq\t"); break;
case 21:printf(" %d\t", i); printf("lss\t"); break;
case 22:printf(" %d\t", i); printf("leq\t"); break;
}
printf("%d\t", code[i].r);
printf("%d\t", code[i].l);
printf("%d\t", code[i].m);
}
return;
}
void fetch()
{
ir = code[pc];
pc++;
return;
}
void execute()
{
switch(ir.op)
{
//LIT
case 1:
printf("lit\t");
r[ir.r] = ir.m;
break;
//RTN
case 2:
printf("rtn\t");
sp = bp - 1;
bp = stack[sp+3];
pc = stack[sp+4];
break;
//LOD
case 3:
printf("lod\t");
r[ir.r] = stack[base(ir.l, bp) + ir.m];
break;
//STO
case 4:
printf("sto\t");
stack[base(ir.l, bp) + ir.m] = r[ir.r];
break;
//CAL
case 5:
printf("cal\t");
stack[sp + 1] = 0;
stack[sp + 2] = base(ir.l, bp);
stack[sp + 3] = bp;
stack[sp + 4] = pc;
bp = sp + 1;
pc = ir.m;
break;
//INC
case 6:
printf("inc\t");
sp = sp + ir.m;
break;
//JMP
case 7:
printf("jmp\t");
pc = ir.m;
break;
//JPC
case 8:
printf("jpc\t");
if (r[ir.r] == 0)
pc = ir.m;
break;
//SIO
case 9:
printf("sio\t");
if(ir.m == 1)
printf(" R[%d] = %d ", ir.r, r[ir.r]);
else if (ir.m == 2)
{
printf(" Enter number to store in register. ");
scanf("%d", &r[ir.r]);
}
else if (ir.m == 3)
halt = 1;
break;
//NEG
case 10:
printf("neg\t");
r[ir.r] = 0 - r[ir.l];
break;
//ADD
case 11:
printf("add\t");
r[ir.r] = r[ir.l] + r[ir.m];
break;
//SUB
case 12:
printf("sub\t");
r[ir.r] = r[ir.l] - r[ir.m];
break;
//MUL
case 13:
printf("mul\t");
r[ir.r] = r[ir.l] * r[ir.m];
break;
//DIV
case 14:
printf("div\t");
r[ir.r] = r[ir.l] / (r[ir.m]);
break;
//ODD
case 15:
printf("odd\t");
r[ir.r] = r[ir.r] % 2;
break;
//MOD
case 16:
printf("mod\t");
r[ir.r] = r[ir.l] % r[ir.m];
break;
//EQL
case 17:
printf("eql\t");
if (r[ir.l] == r[ir.m])
r[ir.r] = 1;
else
r[ir.r] = 0;
break;
//NEQ
case 18:
printf("neq\t");
if (r[ir.l] != r[ir.m])
r[ir.r] = 1;
else
r[ir.r] = 0;
break;
//LSS
case 19:
printf("lss\t");
if (r[ir.l]
r[ir.r] = 1;
else
r[ir.r] = 0;
break;
//LEQ
case 20:
printf("leq\t");
if (r[ir.l]
r[ir.r] = 1;
else
r[ir.r] = 0;
break;
//GTR
case 21:
printf("gtr\t");
if (r[ir.l] > r[ir.m])
r[ir.r] = 1;
else
r[ir.r] = 0;
break;
//GEQ
case 22:
printf("geq\t");
if (r[ir.l] >= r[ir.m])
r[ir.r] = 1;
else
r[ir.r] = 0;
break;
default:
printf("err\t");
return;
}
return;
}
void printStack()
{
if (bp == 0)
return;
else
{
int i;
for(i = 0; i
printf("%d\t", stack[i]);
return;
}
}
int base(int l, int base) // l stand for L in the instruction format
{
int b1; //find base L levels down
b1 = base;
while (l > 0)
{
b1 = stack[b1 + 1];
l--;
}
return b1;
}
Appendix B ISA Pseudo CodStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started