-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcode.c
120 lines (116 loc) · 2.22 KB
/
opcode.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "opcode.h"
int opcodeDecode(char* instruction,int *aux){
if(!strcmp(instruction,"stop")){
*aux = 0;
return 0;
}
else if(0==strcmp(instruction, "load")){
*aux = 1;
return 2;
}
else if(0==strcmp(instruction, "store")){
*aux = 2;
return 2;
}
else if(0==strcmp(instruction, "read")){
*aux = 3;
return 3;
}
else if(0==strcmp(instruction, "write")){
*aux = 4;
return 3;
}
else if(0==strcmp(instruction, "add")) {
*aux = 5;
return 4;
}
else if(0==strcmp(instruction, "subtract")) {
*aux = 6;
return 4;
}
else if(0==strcmp(instruction, "multiply")) {
*aux = 7;
return 4;
}
else if(0==strcmp(instruction, "divide")) {
*aux = 8;
return 4;
}
else if(0==strcmp(instruction, "jump")) {
*aux = 9;
return 1;
}
else if(0==strcmp(instruction, "jmpz")) {
*aux = 10;
return 2;
}
else if(0==strcmp(instruction, "jmpn")) {
*aux = 11;
return 2;
}
else if(0==strcmp(instruction, "move")) {
*aux = 12;
return 4;
}
else if(0==strcmp(instruction, "push")) {
*aux = 13;
return 3;
}
else if(0==strcmp(instruction, "pop")) {
*aux = 14;
return 3;
}
else if(0==strcmp(instruction, "call")) {
*aux = 15;
return 1;
}
else if(0==strcmp(instruction, "return")) {
*aux = 16;
return 0;
}
else if(0==strcmp(instruction, "load_s")) {
*aux = 17;
return 2;
}
else if(0==strcmp(instruction, "store_s")) {
*aux = 18;
return 2;
}
else if(0==strcmp(instruction, "loadc")) {
*aux = 19;
return 2;
}
else if(0==strcmp(instruction, "loadi")) {
*aux = 20;
return 4;
}
else if(0==strcmp(instruction, "storei")) {
*aux = 21;
return 4;
}
else if(0==strcmp(instruction, "copytop")) {
*aux = 22;
return 3;
}
else return -1;
}
char *IntToBinOP(int *n){
int c, d, count;
char *pointer;
count = 0;
pointer = (char*)malloc(5);
if (pointer == NULL)
exit(EXIT_FAILURE);
for (c = 4 ; c >= 0 ; c--){
d = *n >> c;
if (d & 1)
*(pointer+count) = 1 + '0';
else
*(pointer+count) = 0 + '0';
count++;
}
return pointer;
}