diff --git a/Makefile b/Makefile index 894b86a..1d0990c 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ # Compiler CC = gcc +# program to Run +F = prog.bin + # Compiler flags CFLAGS = -g -Wall -std=c99 -pedantic @@ -19,8 +22,11 @@ all: clean: rm -f $(OBJ) $(TARGET) +debug: all + ./$(TARGET) --debug $(F) + run: all - ./$(TARGET) + ./$(TARGET) $(F) nja: ./nja/nja$(V) ./nja/nja$(V) $(IN) $(OUT) diff --git a/SDA.c b/SDA.c index 2ea0509..08a7db2 100644 --- a/SDA.c +++ b/SDA.c @@ -20,7 +20,7 @@ void setSDA(int point, ObjRef val, struct sda s) { void printSDA(struct sda s) { for (int i = 0; i < *s.size; i++) { - printf("%i\n", getSDA(i, s)); + printf("%i\n", *(int *)getSDA(i, s)->data); } } diff --git a/nja/nja4 b/nja/nja4 index 3af774f..c831120 100755 Binary files a/nja/nja4 and b/nja/nja4 differ diff --git a/nja/nja6 b/nja/nja6 index a8fa548..15d2e6e 100755 Binary files a/nja/nja6 and b/nja/nja6 differ diff --git a/nja/nja7 b/nja/nja7 index bf024b9..b88b698 100755 Binary files a/nja/nja7 and b/nja/nja7 differ diff --git a/nja/nja8 b/nja/nja8 index f9555dc..ab90a0e 100755 Binary files a/nja/nja8 and b/nja/nja8 differ diff --git a/njvm.c b/njvm.c index 51b9bab..feab9cb 100644 --- a/njvm.c +++ b/njvm.c @@ -10,7 +10,7 @@ #include "reg.c" // Debug -int debug = 1; +int debug = 0; // Stack struct stack stack; @@ -54,7 +54,7 @@ void execute(struct program program) { push(stack, stackSlotWithObjRef(getIntObj(SIGN_EXTEND(IMMEDIATE(program.program[i]))))); break; case ADD: - if (debug == 1) printf("add: %i + %i\n", * (int *)peek(stack, 2).u.objRef->data, * (int *)peek(stack, 1).u.objRef); + if (debug == 1) printf("add: %i + %i\n",peek(stack, 2),peek(stack, 1)); push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) + getIntValfromStackSlot(pop(stack))))); break; case SUB: @@ -194,19 +194,24 @@ void execute(struct program program) { if (debug == 1) printf("new i: %i\n", i); break; case DROP: + if(debug ==1) printf("drop\n"); *stack.current = *stack.current - SIGN_EXTEND(IMMEDIATE(program.program[i])); break; case DUP: + if (debug==1) printf("dup\n"); temp = getIntValfromStackSlot(pop(stack)); push(stack, stackSlotWitchNumber(temp)); push(stack, stackSlotWitchNumber(temp)); break; - case PUSHR: - //TODO - pushR(reg, SIGN_EXTEND(IMMEDIATE(program.program[i]))); - break; case POPR: - push(stack, stackSlotWitchNumber(popR(reg))); + if (debug==1) printf("popr") ; + pushR(reg, getIntValfromStackSlot(pop(stack))); + if(debug == 1) printStackR(reg); + break; + case PUSHR: + if(debug == 1) printf("pushr"); + push(stack, stackSlotWitchNumber(popR(reg))); + if(debug == 1) printStackR(reg); break; } @@ -226,7 +231,7 @@ int main(int argc, char *argv[]) { // Initialize the Stack int size = SIZE; int current = 0; - unsigned int s[SIZE]; + StackSlot s[SIZE]; stack.size = &size; stack.current = ¤t; stack.stack = s; diff --git a/test.bin b/prog.bin similarity index 100% rename from test.bin rename to prog.bin diff --git a/programs/prog1.asm?inline=false b/programs/prog1.asm?inline=false new file mode 100644 index 0000000..470fd7a --- /dev/null +++ b/programs/prog1.asm?inline=false @@ -0,0 +1,31 @@ +// +// prog1.asm -- an assembler example with global variables +// + +// global Integer x; +// global Integer y; +// x = 2; +// y = x + 3; +// x = 7 * y + x; +// writeInteger(x + -33); +// writeCharacter('\n'); + + pushc 2 + popg 0 + pushg 0 + pushc 3 + add + popg 1 + pushc 7 + pushg 1 + mul + pushg 0 + add + popg 0 + pushg 0 + pushc -33 + add + wrint + pushc '\n' + wrchr + halt diff --git a/reg.c b/reg.c index 65b3ce5..20b71dc 100755 --- a/reg.c +++ b/reg.c @@ -13,11 +13,10 @@ struct reg { unsigned int *stack; }; -void printStackR(struct reg stack, int fp) { - printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); - for (int i = 0; i < *stack.current; ++i) { - if(fp == i) printf("|FP|\n"); - printf("|%i|\n", stack.stack[i]); +void printStackR(struct reg stack) { + printf("Regurn reg\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); + for (int i = *stack.current-1; i > 0; --i) { + printf("||%i||\n", stack.stack[i]); } } diff --git a/stack.c b/stack.c index f481085..1aa0cfb 100755 --- a/stack.c +++ b/stack.c @@ -16,9 +16,18 @@ struct stack { void printStack(struct stack stack, int fp) { printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); - for (int i = 0; i < *stack.current; ++i) { - if(fp == i) printf("|FP|\n"); - printf("|%i|\n", stack.stack[i]); + for (int i = *stack.current -1; i >= 0; --i) { + printf("%i\t",i); + if(stack.stack[i].isObjRef){ + printf("|%p|", stack.stack[i].u.objRef); + if(stack.stack[i].u.objRef->size == sizeof(int)) + printf("(%i)",*(int *)stack.stack[i].u.objRef->data); + }else { + printf("|%i|", getIntValfromStackSlot(stack.stack[i])); + } + if(fp == i) printf("<-\tFP\n"); + else if(*stack.current == i) printf("<-\tSP\n"); + else printf("\n"); } } @@ -40,11 +49,11 @@ StackSlot pop(struct stack s) { return s.stack[*s.current]; } -StackSlot peek(struct stack s, int steps) { +int peek(struct stack s, int steps) { if (*s.current - steps < 0) { printf("Stack Underflow\n"); exit(EXIT_FAILURE); } - return s.stack[*s.current - steps]; + return getIntValfromStackSlot(s.stack[*s.current - steps]); } #endif diff --git a/stackslot.c b/stackslot.c index 5cbd04a..95380b2 100644 --- a/stackslot.c +++ b/stackslot.c @@ -24,6 +24,7 @@ ObjRef getIntObj(int val){ perror("malloc"); } *(int *)intObject->data=val; + intObject->size=sizeof(int); return intObject; } int getValFromIntObj(ObjRef iref){ @@ -36,6 +37,7 @@ int getIntValfromStackSlot(StackSlot s){ return s.u.number; } void setValIntObj(ObjRef iref, int val){ + iref->size=sizeof(int); *(int *)iref->data=val; } StackSlot stackSlotWithObjRef(ObjRef val){