From e8b406233104467894f51564db2eeb8d9f66810d Mon Sep 17 00:00:00 2001 From: nilspolek Date: Tue, 23 Jan 2024 16:58:57 +0100 Subject: [PATCH] added some sht --- njvm.c | 14 +++++ record.c | 25 +++++++++ test.asm | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 record.c create mode 100644 test.asm diff --git a/njvm.c b/njvm.c index c09b891..1fffc07 100644 --- a/njvm.c +++ b/njvm.c @@ -8,6 +8,7 @@ #include "SDA.c" #include "debugMenu.c" #include "bigint.h" +#include "record.c" // Debug int debug = 0; @@ -240,6 +241,19 @@ void execute(struct program program) { if (debug == 1) printf("pushr"); push(stack, pop(reg)); break; + case NEW: + if (debug == 1) printf("new"); + push(stack, newRecord(SIGN_EXTEND(IMMEDIATE(program.program[i])))) + break; + case GETF: + if (debug == 1) printf("getf"); + push(stack, getField(pop(stack), SIGN_EXTEND(IMMEDIATE(program.program[i])))) + break; + case PUTF: + if (debug == 1) printf("putf"); + tempObj = pop(stack) + setField(pop(stack), SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj) + break; } } end: diff --git a/record.c b/record.c new file mode 100644 index 0000000..eb49bec --- /dev/null +++ b/record.c @@ -0,0 +1,25 @@ +// +// Created by Nils Polek on 23.01.24. +// +#ifndef RECORD +#define RECORD +#include "stackslot.c" +#include "instruktion.c" +ObjRef newRecord(int size){ + ObjRef record; + if((record = malloc(*record + size * sizeof(void *)))== NULL){ + perror("malloc") + } + record->size = MSB & size; + for(int i = 0; i < size; i++) { + GET_REFS_PTR(record)[i] = malloc(8) + } +} +ObjRef getField(ObjRef array, int point){ + return *(ObjRef *)GET_REFS_PTR(array)[point]->data +} +setField(ObjRef array, int point, ObjRef value){ + (ObjRef *)GET_REFS_PTR(array)[point]->data = value +} + +#endif \ No newline at end of file diff --git a/test.asm b/test.asm new file mode 100644 index 0000000..cd43d4a --- /dev/null +++ b/test.asm @@ -0,0 +1,155 @@ +// +// version +// + .vers 7 + +// +// execution framework +// +__start: + call _main + call _exit +__stop: + jmp __stop + +// +// Integer readInteger() +// +_readInteger: + asf 0 + rdint + popr + rsf + ret + +// +// void writeInteger(Integer) +// +_writeInteger: + asf 0 + pushl -3 + wrint + rsf + ret + +// +// Character readCharacter() +// +_readCharacter: + asf 0 + rdchr + popr + rsf + ret + +// +// void writeCharacter(Character) +// +_writeCharacter: + asf 0 + pushl -3 + wrchr + rsf + ret + +// +// Integer char2int(Character) +// +_char2int: + asf 0 + pushl -3 + popr + rsf + ret + +// +// Character int2char(Integer) +// +_int2char: + asf 0 + pushl -3 + popr + rsf + ret + +// +// void exit() +// +_exit: + asf 0 + halt + rsf + ret + +// +// void writeString(String) +// +_writeString: + asf 1 + pushc 0 + popl 0 + jmp _writeString_L2 +_writeString_L1: + pushl -3 + pushl 0 + getfa + call _writeCharacter + drop 1 + pushl 0 + pushc 1 + add + popl 0 +_writeString_L2: + pushl 0 + pushl -3 + getsz + lt + brt _writeString_L1 + rsf + ret + +// +// void main() +// +_main: + asf 2 + new 2 + popl 1 + pushl 1 + pushc 5 + putf 0 + pushl 1 + getf 0 + popl 0 + pushl 1 + pushc 2 + pushl 0 + mul + putf 1 + pushl 1 + getf 0 + call _writeInteger + drop 1 + pushc 1 + newa + dup + pushc 0 + pushc 10 + putfa + call _writeString + drop 1 + pushl 1 + getf 1 + call _writeInteger + drop 1 + pushc 1 + newa + dup + pushc 0 + pushc 10 + putfa + call _writeString + drop 1 +__0: + rsf + ret \ No newline at end of file