-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquattro_istruzioni_aritmetiche.c
227 lines (193 loc) · 7.49 KB
/
quattro_istruzioni_aritmetiche.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//@+leo-ver=4-thin
//@+node:leonardoce.20090629082550.51:@thin quattro_istruzioni_aritmetiche.c
//@@language c
//@@tabwidth -4
//@+others
//@+node:leonardoce.20090629082550.52:Instruction_Add
#include <stdio.h>
#include <string.h>
#include "quattro_istruzioni.h"
#include "quattro_debug.h"
/**
* Istruzioni aritmetiche
*/
void Instruction_Add(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2]+=self->dataStack[self->dataStackPtr-1];
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.52:Instruction_Add
//@+node:leonardoce.20090629082550.53:Instruction_Sub
void Instruction_Sub(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2]-=self->dataStack[self->dataStackPtr-1];
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.53:Instruction_Sub
//@+node:leonardoce.20090629082550.54:Instruction_Mul
void Instruction_Mul(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2]*=self->dataStack[self->dataStackPtr-1];
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.54:Instruction_Mul
//@+node:leonardoce.20090629082550.55:Instruction_Div
void Instruction_Div(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
if(self->dataStack[self->dataStackPtr-1] == 0 ) {
self->dataStack[self->dataStackPtr-2]=0xFFFF;
} else {
self->dataStack[self->dataStackPtr-2]/=self->dataStack[self->dataStackPtr-1];
}
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.55:Instruction_Div
//@+node:leonardoce.20090629082550.56:Instruction_SlashMod
void Instruction_SlashMod(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
if(self->dataStack[self->dataStackPtr-1] == 0 ) {
self->dataStack[self->dataStackPtr-2]=0xFFFF;
} else {
int q = self->dataStack[self->dataStackPtr-2] / self->dataStack[self->dataStackPtr-1];
int r = self->dataStack[self->dataStackPtr-2] % self->dataStack[self->dataStackPtr-1];
self->dataStack[self->dataStackPtr-1]=q;
self->dataStack[self->dataStackPtr-2]=r;
}
}
//@-node:leonardoce.20090629082550.56:Instruction_SlashMod
//@+node:leonardoce.20090629082550.57:Instruction_Less
void Instruction_Less(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2] = (self->dataStack[self->dataStackPtr-2] < self->dataStack[self->dataStackPtr-1]);
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.57:Instruction_Less
//@+node:leonardoce.20090629082550.58:Instruction_More
void Instruction_More(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2] = (self->dataStack[self->dataStackPtr-2] > self->dataStack[self->dataStackPtr-1]);
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.58:Instruction_More
//@+node:leonardoce.20090629082550.59:Instruction_LessEqual
void Instruction_LessEqual(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2] = (self->dataStack[self->dataStackPtr-2] <= self->dataStack[self->dataStackPtr-1]);
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.59:Instruction_LessEqual
//@+node:leonardoce.20090629082550.60:Instruction_MoreEqual
void Instruction_MoreEqual(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2] = (self->dataStack[self->dataStackPtr-2] >= self->dataStack[self->dataStackPtr-1]);
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.60:Instruction_MoreEqual
//@+node:leonardoce.20090629082550.61:Instruction_Equal
void Instruction_Equal(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2] = (self->dataStack[self->dataStackPtr-2] == self->dataStack[self->dataStackPtr-1]);
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.61:Instruction_Equal
//@+node:leonardoce.20090629082550.62:Instruction_And
void Instruction_And(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2] = (self->dataStack[self->dataStackPtr-2] & self->dataStack[self->dataStackPtr-1]);
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.62:Instruction_And
//@+node:leonardoce.20090629082550.63:Instruction_Or
void Instruction_Or(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2] = (self->dataStack[self->dataStackPtr-2] | self->dataStack[self->dataStackPtr-1]);
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.63:Instruction_Or
//@+node:leonardoce.20090629082550.64:Instruction_Invert
void Instruction_Invert(ForthVm *self) {
if(self->dataStackPtr<1) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-1] = (~self->dataStack[self->dataStackPtr-1]);
}
//@-node:leonardoce.20090629082550.64:Instruction_Invert
//@+node:leonardoce.20090629082550.65:Instruction_LShift
void Instruction_LShift(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2]=self->dataStack[self->dataStackPtr-2] << self->dataStack[self->dataStackPtr-1];
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.65:Instruction_LShift
//@+node:leonardoce.20090629082550.66:Instruction_RShift
void Instruction_RShift(ForthVm *self) {
if(self->dataStackPtr<2) {
ForthVm_Error(self, "data stack underrun");
return;
}
self->dataStack[self->dataStackPtr-2]=self->dataStack[self->dataStackPtr-2] >> self->dataStack[self->dataStackPtr-1];
self->dataStackPtr--;
}
//@-node:leonardoce.20090629082550.66:Instruction_RShift
//@+node:leonardoce.20090629082550.67:ForthVm_RegistraIstruzioniAritmetiche
void ForthVm_RegistraIstruzioniAritmetiche(ForthVm *self) {
ForthVm_RegisterNativeInstruction(self, "+", Instruction_Add);
ForthVm_RegisterNativeInstruction(self, "-", Instruction_Sub);
ForthVm_RegisterNativeInstruction(self, "*", Instruction_Mul);
ForthVm_RegisterNativeInstruction(self, "/", Instruction_Div);
ForthVm_RegisterNativeInstruction(self, "<", Instruction_Less);
ForthVm_RegisterNativeInstruction(self, ">", Instruction_More);
ForthVm_RegisterNativeInstruction(self, "<=", Instruction_LessEqual);
ForthVm_RegisterNativeInstruction(self, ">=", Instruction_MoreEqual);
ForthVm_RegisterNativeInstruction(self, "=", Instruction_Equal);
ForthVm_RegisterNativeInstruction(self, "and", Instruction_And);
ForthVm_RegisterNativeInstruction(self, "or", Instruction_Or);
ForthVm_RegisterNativeInstruction(self, "invert", Instruction_Invert);
ForthVm_RegisterNativeInstruction(self, "/mod", Instruction_SlashMod);
ForthVm_RegisterNativeInstruction(self, "lshift", Instruction_LShift);
ForthVm_RegisterNativeInstruction(self, "rshift", Instruction_RShift);
}
//@-node:leonardoce.20090629082550.67:ForthVm_RegistraIstruzioniAritmetiche
//@-others
//@-node:leonardoce.20090629082550.51:@thin quattro_istruzioni_aritmetiche.c
//@-leo