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