-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions.c
156 lines (122 loc) · 3.65 KB
/
instructions.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "instructions.h"
#include "memory.h"
void populate_instruction_table(instruction_table t)
{
t[MOV] = instr_mov;
t[PUSH] = instr_push;
t[POP] = instr_pop;
t[INC] = instr_inc;
t[DEC] = instr_dec;
t[ADD] = instr_add;
t[SUB] = instr_sub;
t[MUL] = instr_mul;
t[DIV] = instr_div;
t[REM] = instr_rem;
t[NOT] = instr_not;
t[XOR] = instr_xor;
t[OR] = instr_or;
t[AND] = instr_and;
t[CMP] = instr_cmp;
t[JMP] = instr_jmp;
t[JE] = instr_je;
t[JNE] = instr_jne;
t[JG] = instr_jg;
t[JGE] = instr_jge;
t[JL] = instr_jl;
t[JLE] = instr_jle;
}
void instr_mov(program* p, memory* m, stack* s, int* instr_idx)
{
*arg_to_addr(m, &p->args[*instr_idx][0]) = *arg_to_addr(m, &p->args[*instr_idx][1]);
}
void instr_push(program* p, memory* m, stack* s, int* instr_idx)
{
stack_push(s, arg_to_addr(m, &p->args[*instr_idx][0]));
}
void instr_pop(program* p, memory* m, stack* s, int* instr_idx)
{
stack_pop(s, arg_to_addr(m, &p->args[*instr_idx][0]));
}
void instr_inc(program* p, memory* m, stack* s, int* instr_idx)
{
++(*arg_to_addr(m, &p->args[*instr_idx][0]));
}
void instr_dec(program* p, memory* m, stack* s, int* instr_idx)
{
--(*arg_to_addr(m, &p->args[*instr_idx][0]));
}
void instr_add(program* p, memory* m, stack* s, int* instr_idx)
{
*arg_to_addr(m, &p->args[*instr_idx][0]) += *arg_to_addr(m, &p->args[*instr_idx][1]);
}
void instr_sub(program* p, memory* m, stack* s, int* instr_idx)
{
*arg_to_addr(m, &p->args[*instr_idx][0]) -= *arg_to_addr(m, &p->args[*instr_idx][1]);
}
void instr_mul(program* p, memory* m, stack* s, int* instr_idx)
{
*arg_to_addr(m, &p->args[*instr_idx][0]) *= *arg_to_addr(m, &p->args[*instr_idx][1]);
}
void instr_div(program* p, memory* m, stack* s, int* instr_idx)
{
int* arg0 = arg_to_addr(m, &p->args[*instr_idx][0]);
int* arg1 = arg_to_addr(m, &p->args[*instr_idx][1]);
m->remainder = *arg0 % *arg1;
*arg0 /= *arg1;
}
void instr_rem(program* p, memory* m, stack* s, int* instr_idx)
{
*arg_to_addr(m, &p->args[*instr_idx][0]) = m->remainder;
}
void instr_not(program* p, memory* m, stack* s, int* instr_idx)
{
int* arg = arg_to_addr(m, &p->args[*instr_idx][0]);
*arg = ~(*arg);
}
void instr_xor(program* p, memory* m, stack* s, int* instr_idx)
{
*arg_to_addr(m, &p->args[*instr_idx][0]) ^= *arg_to_addr(m, &p->args[*instr_idx][1]);
}
void instr_or(program* p, memory* m, stack* s, int* instr_idx)
{
*arg_to_addr(m, &p->args[*instr_idx][0]) |= *arg_to_addr(m, &p->args[*instr_idx][1]);
}
void instr_and(program* p, memory* m, stack* s, int* instr_idx)
{
*arg_to_addr(m, &p->args[*instr_idx][0]) &= *arg_to_addr(m, &p->args[*instr_idx][1]);
}
void instr_cmp(program* p, memory* m, stack* s, int* instr_idx)
{
int* arg0 = arg_to_addr(m, &p->args[*instr_idx][0]);
int* arg1 = arg_to_addr(m, &p->args[*instr_idx][1]);
m->equal = (*arg0 == *arg1);
m->greater = (*arg0 > *arg1);
}
void instr_jmp(program* p, memory* m, stack* s, int* instr_idx)
{
*instr_idx = *arg_to_addr(m, &p->args[*instr_idx][0]) - 1;
}
void instr_je(program* p, memory* m, stack* s, int* instr_idx)
{
if (m->equal) instr_jmp(p, m, s, instr_idx);
}
void instr_jne(program* p, memory* m, stack* s, int* instr_idx)
{
if (!(m->equal)) instr_jmp(p, m, s, instr_idx);
}
void instr_jg(program* p, memory* m, stack* s, int* instr_idx)
{
if (m->greater) instr_jmp(p, m, s, instr_idx);
}
void instr_jge(program* p, memory* m, stack* s, int* instr_idx)
{
if (m->greater || m->equal) instr_jmp(p, m, s, instr_idx);
}
void instr_jl(program* p, memory* m, stack* s, int* instr_idx)
{
if (!(m->greater) && !(m->equal)) instr_jmp(p, m, s, instr_idx);
}
void instr_jle(program* p, memory* m, stack* s, int* instr_idx)
{
if (!(m->greater)) instr_jmp(p, m, s, instr_idx);
}