From 086defff50f43fa6099d05cf888c921a56e0a246 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sun, 25 Dec 2016 13:31:47 +0100 Subject: [PATCH] Fixing errors --- cpu.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 80 insertions(+), 34 deletions(-) diff --git a/cpu.c b/cpu.c index 78e35e8..b23ba55 100644 --- a/cpu.c +++ b/cpu.c @@ -37,7 +37,7 @@ void pop(short dst) fprintf(stderr,"CRITICAL: STACK VIOLATION\n"); core_dump(); } - regs[dst] = value; + regs[dst % (MAX_INT+1)] = value; } void teq(short dst, short a, short b) @@ -80,6 +80,10 @@ void jmp(short dst) void jnz(short cond, short dst) { + if(cond > MAX_INT) + { + cond = regs[dst % (MAX_INT+1)]; + } if(cond) { jmp(dst); @@ -88,6 +92,10 @@ void jnz(short cond, short dst) void jz(short cond, short dst) { + if(cond > MAX_INT) + { + cond = regs[dst % (MAX_INT+1)]; + } if(!cond) { jmp(dst); @@ -221,78 +229,115 @@ void nop() return; } +short fetch() +{ + short value = mem[pc++]; + if(value < 0) + { + value *= -1; + } + return value; +} + void start_execution() { + short arg1; + short arg2; + short arg3; for(;;) { - short opcode = mem[pc++]; + short opcode = fetch(); switch(opcode) { case HALT: return; case MOV: - mov(mem[pc],mem[pc+1]); - pc += 2; + arg1 = fetch(); + arg2 = fetch(); + mov(arg1,arg2); break; case PUSH: - push(mem[pc++]); + arg1 = fetch(); + push(arg1); break; case POP: - pop(mem[pc++]); + arg1 = fetch(); + pop(arg1); break; case TEQ: - teq(mem[pc],mem[pc+1],mem[pc+2]); - pc += 3; + arg1 = fetch(); + arg2 = fetch(); + arg3 = fetch(); + teq(arg1,arg2,arg3); break; case TGT: - tgt(mem[pc],mem[pc+1],mem[pc+2]); - pc += 3; + arg1 = fetch(); + arg2 = fetch(); + arg3 = fetch(); + tgt(arg1,arg2,arg3); break; case JMP: - jmp(mem[pc++]); + arg1 = fetch(); + jmp(arg1); break; case JNZ: - jnz(mem[pc],mem[pc+1]); - pc += 2; + arg1 = fetch(); + arg2 = fetch(); + jnz(arg1,arg2); break; case JZ: - jz(mem[pc],mem[pc+1]); - pc += 2; + arg1 = fetch(); + arg2 = fetch(); + jz(arg1,arg2); break; case ADD: - add(mem[pc],mem[pc+1],mem[pc+2]); - pc += 3; + arg1 = fetch(); + arg2 = fetch(); + arg3 = fetch(); + add(arg1,arg2,arg3); break; case MULT: - mult(mem[pc],mem[pc+1],mem[pc+2]); - pc += 3; + arg1 = fetch(); + arg2 = fetch(); + arg3 = fetch(); + mult(arg1,arg2,arg3); break; case MOD: - mod(mem[pc],mem[pc+1],mem[pc+2]); - pc += 3; + arg1 = fetch(); + arg2 = fetch(); + arg3 = fetch(); + mod(arg1,arg2,arg3); break; case AND: - and(mem[pc],mem[pc+1],mem[pc+2]); - pc += 3; + arg1 = fetch(); + arg2 = fetch(); + arg3 = fetch(); + and(arg1,arg2,arg3); break; case OR: - or(mem[pc],mem[pc+1],mem[pc+2]); - pc += 3; + arg1 = fetch(); + arg2 = fetch(); + arg3 = fetch(); + or(arg1,arg2,arg3); break; case NOT: - not(mem[pc],mem[pc+1]); - pc += 2; + arg1 = fetch(); + arg2 = fetch(); + not(arg1,arg2); break; case LOAD: - load(mem[pc],mem[pc+1]); - pc += 2; + arg1 = fetch(); + arg2 = fetch(); + load(arg1,arg2); break; case STOR: - stor(mem[pc],mem[pc+1]); - pc += 2; + arg1 = fetch(); + arg2 = fetch(); + stor(arg1,arg2); break; case CALL: - call(mem[pc++]); + arg1 = fetch(); + call(arg1); break; case RET: if(ret()) @@ -301,10 +346,11 @@ void start_execution() } break; case OUT: - out((unsigned char)mem[pc++]); + out((unsigned char)fetch()); break; case IN: - in(mem[pc++]); + arg1 = fetch(); + in(arg1); break; case NOP: nop(); -- libgit2 0.22.2