Commit 086defff50f43fa6099d05cf888c921a56e0a246

Authored by Imanol-Mikel Barba Sabariego
1 parent 5e06910c

Fixing errors

Showing 1 changed file with 80 additions and 34 deletions
... ... @@ -37,7 +37,7 @@ void pop(short dst)
37 37 fprintf(stderr,"CRITICAL: STACK VIOLATION\n");
38 38 core_dump();
39 39 }
40   - regs[dst] = value;
  40 + regs[dst % (MAX_INT+1)] = value;
41 41 }
42 42  
43 43 void teq(short dst, short a, short b)
... ... @@ -80,6 +80,10 @@ void jmp(short dst)
80 80  
81 81 void jnz(short cond, short dst)
82 82 {
  83 + if(cond > MAX_INT)
  84 + {
  85 + cond = regs[dst % (MAX_INT+1)];
  86 + }
83 87 if(cond)
84 88 {
85 89 jmp(dst);
... ... @@ -88,6 +92,10 @@ void jnz(short cond, short dst)
88 92  
89 93 void jz(short cond, short dst)
90 94 {
  95 + if(cond > MAX_INT)
  96 + {
  97 + cond = regs[dst % (MAX_INT+1)];
  98 + }
91 99 if(!cond)
92 100 {
93 101 jmp(dst);
... ... @@ -221,78 +229,115 @@ void nop()
221 229 return;
222 230 }
223 231  
  232 +short fetch()
  233 +{
  234 + short value = mem[pc++];
  235 + if(value < 0)
  236 + {
  237 + value *= -1;
  238 + }
  239 + return value;
  240 +}
  241 +
224 242 void start_execution()
225 243 {
  244 + short arg1;
  245 + short arg2;
  246 + short arg3;
226 247 for(;;)
227 248 {
228   - short opcode = mem[pc++];
  249 + short opcode = fetch();
229 250 switch(opcode)
230 251 {
231 252 case HALT:
232 253 return;
233 254 case MOV:
234   - mov(mem[pc],mem[pc+1]);
235   - pc += 2;
  255 + arg1 = fetch();
  256 + arg2 = fetch();
  257 + mov(arg1,arg2);
236 258 break;
237 259 case PUSH:
238   - push(mem[pc++]);
  260 + arg1 = fetch();
  261 + push(arg1);
239 262 break;
240 263 case POP:
241   - pop(mem[pc++]);
  264 + arg1 = fetch();
  265 + pop(arg1);
242 266 break;
243 267 case TEQ:
244   - teq(mem[pc],mem[pc+1],mem[pc+2]);
245   - pc += 3;
  268 + arg1 = fetch();
  269 + arg2 = fetch();
  270 + arg3 = fetch();
  271 + teq(arg1,arg2,arg3);
246 272 break;
247 273 case TGT:
248   - tgt(mem[pc],mem[pc+1],mem[pc+2]);
249   - pc += 3;
  274 + arg1 = fetch();
  275 + arg2 = fetch();
  276 + arg3 = fetch();
  277 + tgt(arg1,arg2,arg3);
250 278 break;
251 279 case JMP:
252   - jmp(mem[pc++]);
  280 + arg1 = fetch();
  281 + jmp(arg1);
253 282 break;
254 283 case JNZ:
255   - jnz(mem[pc],mem[pc+1]);
256   - pc += 2;
  284 + arg1 = fetch();
  285 + arg2 = fetch();
  286 + jnz(arg1,arg2);
257 287 break;
258 288 case JZ:
259   - jz(mem[pc],mem[pc+1]);
260   - pc += 2;
  289 + arg1 = fetch();
  290 + arg2 = fetch();
  291 + jz(arg1,arg2);
261 292 break;
262 293 case ADD:
263   - add(mem[pc],mem[pc+1],mem[pc+2]);
264   - pc += 3;
  294 + arg1 = fetch();
  295 + arg2 = fetch();
  296 + arg3 = fetch();
  297 + add(arg1,arg2,arg3);
265 298 break;
266 299 case MULT:
267   - mult(mem[pc],mem[pc+1],mem[pc+2]);
268   - pc += 3;
  300 + arg1 = fetch();
  301 + arg2 = fetch();
  302 + arg3 = fetch();
  303 + mult(arg1,arg2,arg3);
269 304 break;
270 305 case MOD:
271   - mod(mem[pc],mem[pc+1],mem[pc+2]);
272   - pc += 3;
  306 + arg1 = fetch();
  307 + arg2 = fetch();
  308 + arg3 = fetch();
  309 + mod(arg1,arg2,arg3);
273 310 break;
274 311 case AND:
275   - and(mem[pc],mem[pc+1],mem[pc+2]);
276   - pc += 3;
  312 + arg1 = fetch();
  313 + arg2 = fetch();
  314 + arg3 = fetch();
  315 + and(arg1,arg2,arg3);
277 316 break;
278 317 case OR:
279   - or(mem[pc],mem[pc+1],mem[pc+2]);
280   - pc += 3;
  318 + arg1 = fetch();
  319 + arg2 = fetch();
  320 + arg3 = fetch();
  321 + or(arg1,arg2,arg3);
281 322 break;
282 323 case NOT:
283   - not(mem[pc],mem[pc+1]);
284   - pc += 2;
  324 + arg1 = fetch();
  325 + arg2 = fetch();
  326 + not(arg1,arg2);
285 327 break;
286 328 case LOAD:
287   - load(mem[pc],mem[pc+1]);
288   - pc += 2;
  329 + arg1 = fetch();
  330 + arg2 = fetch();
  331 + load(arg1,arg2);
289 332 break;
290 333 case STOR:
291   - stor(mem[pc],mem[pc+1]);
292   - pc += 2;
  334 + arg1 = fetch();
  335 + arg2 = fetch();
  336 + stor(arg1,arg2);
293 337 break;
294 338 case CALL:
295   - call(mem[pc++]);
  339 + arg1 = fetch();
  340 + call(arg1);
296 341 break;
297 342 case RET:
298 343 if(ret())
... ... @@ -301,10 +346,11 @@ void start_execution()
301 346 }
302 347 break;
303 348 case OUT:
304   - out((unsigned char)mem[pc++]);
  349 + out((unsigned char)fetch());
305 350 break;
306 351 case IN:
307   - in(mem[pc++]);
  352 + arg1 = fetch();
  353 + in(arg1);
308 354 break;
309 355 case NOP:
310 356 nop();
... ...