Compare commits
11 Commits
35f051b5da
...
old
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
509dc5e947 | ||
|
|
c6ae64250d | ||
|
|
ffc04172ae | ||
|
|
f5cab7c013 | ||
| de248a5943 | |||
| 4994bd6071 | |||
| 0b042fb912 | |||
|
|
77cae7467e | ||
| 6ea2a7e735 | |||
| 8a89c6bf70 | |||
| 88271c91ca |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ cmake-build-debug
|
||||
njvm
|
||||
njvm.dSYM
|
||||
njvm.exe
|
||||
njvm2
|
||||
|
||||
@@ -5,19 +5,5 @@ set(CMAKE_C_STANDARD 99)
|
||||
|
||||
add_compile_options(-g -Wall -pedantic)
|
||||
|
||||
add_executable(njvm njvm.c)
|
||||
|
||||
# Include directories for njvm executable
|
||||
target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
||||
|
||||
# Add the library
|
||||
add_library(bigint STATIC ${CMAKE_SOURCE_DIR}/bigint/src/bigint.c
|
||||
support.c
|
||||
GC.c)
|
||||
|
||||
# Include directories for the bigint library
|
||||
target_include_directories(bigint PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
||||
|
||||
# Link the executable with the library
|
||||
target_link_libraries(njvm PRIVATE bigint)
|
||||
|
||||
add_executable(ninja njvm.c
|
||||
SDA.c)
|
||||
|
||||
105
GC.c
105
GC.c
@@ -1,105 +0,0 @@
|
||||
#ifndef GC
|
||||
#define GC
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "objref.c"
|
||||
#include "instruktion.c"
|
||||
|
||||
bool halbspeicher1IsActiv = true;
|
||||
unsigned char* halbspeicher1;
|
||||
unsigned char* halbspeicher2;
|
||||
unsigned char* freizeiger;
|
||||
unsigned int groesse;
|
||||
|
||||
void initHeap(unsigned int size){
|
||||
groesse = size*1024/2;
|
||||
if((halbspeicher1 = malloc(groesse)) == NULL) perror("malloc");
|
||||
if((halbspeicher2 = malloc(groesse)) == NULL) perror("malloc");
|
||||
freizeiger = halbspeicher1;
|
||||
}
|
||||
ObjRef copyObjToFreeMem(ObjRef obj){
|
||||
ObjRef copy = (ObjRef) halbspeicher2[freizeiger];
|
||||
unsigned int size = (IS_PRIMITIVE(obj))? obj->size : GET_ELEMENT_COUNT(obj)* sizeof(void *);
|
||||
memcpy((void *) halbspeicher2[freizeiger], obj, size);
|
||||
freizeiger += size;
|
||||
return copy;
|
||||
}
|
||||
void setBrokenHeart(ObjRef obj, bool val){
|
||||
//TODO
|
||||
}
|
||||
bool getBrokenHeart(ObjRef obj){
|
||||
//TODO
|
||||
return true;
|
||||
}
|
||||
void setForwardPointer(ObjRef obj, void* ref){
|
||||
//TODO
|
||||
}
|
||||
void *getForwardPointer(ObjRef obj){
|
||||
//TODO
|
||||
return malloc(0);
|
||||
}
|
||||
|
||||
ObjRef reallocate(ObjRef orig){
|
||||
ObjRef copy;
|
||||
if(orig == NULL){
|
||||
copy == NULL;
|
||||
}else{
|
||||
if(getBrokenHeart(orig)){
|
||||
setForwardPointer(copy,orig);
|
||||
}else{
|
||||
copy = copyObjToFreeMem(orig);
|
||||
setBrokenHeart(orig,true);
|
||||
setForwardPointer(orig,copy);
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
unsigned int getSize(ObjRef obj){
|
||||
return (IS_PRIMITIVE(obj))? obj->size : GET_ELEMENT_COUNT(obj)* sizeof(void *);
|
||||
}
|
||||
void scan(void){
|
||||
void *scan = halbspeicher1;
|
||||
while (scan < freizeiger){
|
||||
if(!IS_PRIMITIVE((ObjRef)scan)){
|
||||
for (unsigned int i = 0; i < GET_ELEMENT_COUNT((ObjRef)scan); i += getSize(GET_REFS_PTR((ObjRef)scan)[i])) {
|
||||
GET_REFS_PTR((ObjRef)scan)[i] = reallocate(GET_REFS_PTR((ObjRef)scan)[i]);
|
||||
}
|
||||
}
|
||||
scan += getSize((ObjRef)scan);
|
||||
}
|
||||
}
|
||||
void *alloc(unsigned int size){
|
||||
if((&freizeiger - &halbspeicher1)/8 + size > groesse) {
|
||||
printf("Heap Overflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Set broken heart to false
|
||||
setBrokenHeart((ObjRef)freizeiger,false);
|
||||
freizeiger += sizeof(bool) + sizeof(void *);
|
||||
void *ptr = &freizeiger;
|
||||
freizeiger += size;
|
||||
return ptr;
|
||||
}
|
||||
void swap(void){
|
||||
unsigned char *tmp = halbspeicher1;
|
||||
halbspeicher1 = halbspeicher2;
|
||||
halbspeicher2 = tmp;
|
||||
freizeiger = halbspeicher1;
|
||||
halbspeicher1IsActiv = !halbspeicher1IsActiv;
|
||||
}
|
||||
void triggerGC(void){
|
||||
printf("GC triggered\n");
|
||||
//TODO
|
||||
}
|
||||
void copy(){
|
||||
ObjRef *test;
|
||||
unsigned int objResSize = 2;
|
||||
if((test = malloc(2*sizeof(test)))==NULL) perror("malloc");
|
||||
for (int i = 0; i < objResSize; ++i) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
35
Makefile
35
Makefile
@@ -1,12 +1,29 @@
|
||||
# Makefile for a simple C program
|
||||
build:
|
||||
cd ./bigint; make; cd ..
|
||||
gcc -g -Wall -Ibigint/build/include -o support.o -c support.c
|
||||
gcc -g -Wall -Ibigint/build/include -o njvm.o -c njvm.c
|
||||
gcc -g -Wall -Lbigint/build/lib -o njvm njvm.o support.o -lbigint
|
||||
|
||||
run: build
|
||||
./njvm prog.bin
|
||||
# Compiler
|
||||
CC = gcc
|
||||
|
||||
debug: build
|
||||
./njvm --debug prog.bin
|
||||
# Compiler flags
|
||||
CFLAGS = -g -Wall -std=c99 -pedantic
|
||||
|
||||
# Source file
|
||||
SRC = njvm.c
|
||||
|
||||
# Executable name
|
||||
TARGET = njvm
|
||||
|
||||
# Default target
|
||||
all:
|
||||
$(CC) $(CFLAGS) -o $(TARGET) $(SRC)
|
||||
# Clean up
|
||||
clean:
|
||||
rm -f $(OBJ) $(TARGET)
|
||||
|
||||
run: all
|
||||
./$(TARGET)
|
||||
|
||||
nja: ./nja/nja$(V)
|
||||
./nja/nja$(V) $(IN) $(OUT)
|
||||
|
||||
njc: ./njc/njc$(V)
|
||||
./njc/njc$(V) $(IN) $(OUT)
|
||||
|
||||
43
Makefile3
43
Makefile3
@@ -1,43 +0,0 @@
|
||||
# Makefile for a simple C program
|
||||
|
||||
# Compiler
|
||||
CC = gcc
|
||||
|
||||
# program to Run
|
||||
F = prog.bin
|
||||
|
||||
# Compiler flags
|
||||
CFLAGS = -g -Wall -Ibigint/build/include
|
||||
|
||||
LDFLAGS = -g -Wall -Lbigint/build/lib
|
||||
|
||||
# Source file
|
||||
SRC = njvm.c
|
||||
|
||||
# Executable name
|
||||
TARGET = njvm
|
||||
|
||||
njvm.o:
|
||||
$(CC) $(CFLAGS) -o njvm.o -c njvm.c
|
||||
|
||||
support.o:
|
||||
$(CC) $(CFLAGS) -o support.o -c support.c
|
||||
|
||||
# Default target
|
||||
all: njvm.o support.o
|
||||
$(CC) $(LDFLAGS) -o $(TARGET) njvm.o support.o -lbigint
|
||||
# Clean up
|
||||
clean:
|
||||
rm -f $(OBJ) $(TARGET)
|
||||
|
||||
debug: all
|
||||
./$(TARGET) --debug $(F)
|
||||
|
||||
run: all
|
||||
./$(TARGET) $(F)
|
||||
|
||||
nja: ./nja/nja$(V)
|
||||
./nja/nja$(V) $(IN) $(OUT)
|
||||
|
||||
njc: ./njc/njc$(V)
|
||||
./njc/njc$(V) $(IN) $(OUT)
|
||||
22
SDA.c
22
SDA.c
@@ -4,24 +4,24 @@
|
||||
#ifndef SDA
|
||||
#define SDA
|
||||
#include <stdio.h>
|
||||
#include "stackslot.c"
|
||||
|
||||
struct sda {
|
||||
int *size;
|
||||
ObjRef *sda;
|
||||
unsigned int *sda;
|
||||
int size;
|
||||
};
|
||||
|
||||
ObjRef getSDA(int i, struct sda s) {
|
||||
return s.sda[i];
|
||||
int getSDA(unsigned int offset, struct sda *sda) {
|
||||
return sda->sda[offset];
|
||||
}
|
||||
|
||||
void setSDA(int point, ObjRef val, struct sda s) {
|
||||
if (val == NULL) perror("Value is null");
|
||||
s.sda[point] = val;
|
||||
void setSDA(unsigned int offset, int value, struct sda *sda) {
|
||||
sda->sda[offset] = value;
|
||||
}
|
||||
|
||||
void printSDA(struct sda s) {
|
||||
for (int i = 0; i < *s.size; i++) {
|
||||
printf("%i\n", *(int *)getSDA(i, s)->data);
|
||||
void printSDA(struct sda *sda) {
|
||||
printf("SDA:\n");
|
||||
for (int i = 0; i < sda->size; ++i) {
|
||||
printf("[%d] = %d\n", i, sda->sda[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
#include <stdlib.h>
|
||||
#include "program.c"
|
||||
|
||||
unsigned int fromFile(char *path, struct program program) {
|
||||
unsigned int fromFile(char *path, struct program *program) {
|
||||
unsigned int countInstructions;
|
||||
unsigned int staticVars;
|
||||
FILE *fptr;
|
||||
fptr = fopen(path, "r");
|
||||
if (fptr == NULL) {
|
||||
printf("Error: cannot open code file %s", path);
|
||||
printf("Error: cannot open code file %s\n", path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
unsigned int buffer[4];
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
-Ibigint/build/include
|
||||
-Lbigint/build/lib
|
||||
-lbigint
|
||||
6
consts.c
6
consts.c
@@ -1,7 +1,5 @@
|
||||
#ifndef CONSTS
|
||||
#define CONSTS
|
||||
#define VERSION 8
|
||||
#define VERSION 4
|
||||
|
||||
#endif /* ifndef CONSTS
|
||||
#define CONSTS
|
||||
#define VERSION 2; */
|
||||
#endif
|
||||
|
||||
55
debugMenu.c
55
debugMenu.c
@@ -1,55 +0,0 @@
|
||||
#ifndef DEBUGMENU
|
||||
#define DEBUGMENU
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "stack.c"
|
||||
#include "stack.c"
|
||||
|
||||
void inspect(struct stack s, int fp){
|
||||
//todo Does not work dont know why
|
||||
char input[20];
|
||||
char ref[20];
|
||||
char refStr[20];
|
||||
printf("DEBUG [inspect]: stack, datam object?");
|
||||
fgets(input,20,stdin);
|
||||
if (input[0] == 's') printStack(s, fp);
|
||||
if (input[0] == 'd'){/* todo */ }
|
||||
if (input[0] == 'o'){
|
||||
scanf("%19s",ref);
|
||||
ObjRefContainer container;
|
||||
for (int i = 0; i<= container.size; i++) {
|
||||
sprintf(refStr, "%p", (void *)&container.refs[i]);
|
||||
if(strcmp(ref, refStr) == 0) printf("Adress exists\n");
|
||||
else printf("Adress doeas not exist\n");
|
||||
printf("%s",refStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
void list(void){
|
||||
//todo
|
||||
}
|
||||
void breakpoint(int *bp){
|
||||
printf("BREAKPOINT: ");
|
||||
char input[20];
|
||||
fgets(input,20,stdin);
|
||||
*bp = atoi(input);
|
||||
}
|
||||
|
||||
void debugMenu(int fp, struct stack stack, int* debug, int point, int* bp){
|
||||
char input[20];
|
||||
while (true) {
|
||||
printf("DEBUG(%i): inspect, list, breakpoint, run, step, quit?",point);
|
||||
fgets(input, 20, stdin);
|
||||
printf("%s",input);
|
||||
if(input[0] == 'i') {inspect(stack,fp);}
|
||||
if(input[0] == 'l') list();
|
||||
if(input[0] == 'b') breakpoint(bp);
|
||||
if(input[0] == 's') break;
|
||||
if(input[0] == 'r') {*debug = 0; break;};
|
||||
if(input[0] == 'q') exit(0);
|
||||
strcpy(input, "");
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* ifndef DEBUGMENU */
|
||||
8301
factor.asm
8301
factor.asm
File diff suppressed because it is too large
Load Diff
BIN
factor.bin
BIN
factor.bin
Binary file not shown.
794
factor.nj
794
factor.nj
@@ -1,794 +0,0 @@
|
||||
//
|
||||
// factor.nj -- factorize the numbers 10^n+1, n = 1..30
|
||||
//
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// handle list of factors
|
||||
|
||||
type List = record {
|
||||
Integer value;
|
||||
List next;
|
||||
};
|
||||
|
||||
List addToList(Integer value, List next) {
|
||||
local List newList;
|
||||
newList = new(List);
|
||||
newList.value = value;
|
||||
newList.next = next;
|
||||
return newList;
|
||||
}
|
||||
|
||||
List sortList(List list) {
|
||||
local List result;
|
||||
local List element;
|
||||
local List p;
|
||||
result = nil;
|
||||
while (list != nil) {
|
||||
element = list;
|
||||
list = list.next;
|
||||
if (result == nil || element.value < result.value) {
|
||||
element.next = result;
|
||||
result = element;
|
||||
} else {
|
||||
p = result;
|
||||
while (p.next != nil && element.value >= p.next.value) {
|
||||
p = p.next;
|
||||
}
|
||||
element.next = p.next;
|
||||
p.next = element;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void showList(List list) {
|
||||
if (list == nil) {
|
||||
writeString("1");
|
||||
} else {
|
||||
while (true) {
|
||||
writeInteger(list.value);
|
||||
list = list.next;
|
||||
if (list == nil) {
|
||||
break;
|
||||
}
|
||||
writeString(" * ");
|
||||
}
|
||||
}
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
Integer evalList(List list) {
|
||||
local Integer result;
|
||||
result = 1;
|
||||
while (list != nil) {
|
||||
result = result * list.value;
|
||||
list = list.next;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
List fuseLists(List list1, List list2) {
|
||||
local List element;
|
||||
while (list1 != nil) {
|
||||
element = list1;
|
||||
list1 = list1.next;
|
||||
element.next = list2;
|
||||
list2 = element;
|
||||
}
|
||||
return list2;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// compute 10^n+1
|
||||
|
||||
Integer computeTarget(Integer n) {
|
||||
local Integer x;
|
||||
local Integer i;
|
||||
x = 1;
|
||||
i = 0;
|
||||
while (i < n) {
|
||||
x = x * 10;
|
||||
i = i + 1;
|
||||
}
|
||||
x = x + 1;
|
||||
return x;
|
||||
}
|
||||
|
||||
void testComputeTarget() {
|
||||
writeString("computeTarget()\n");
|
||||
writeString("---------------\n");
|
||||
writeString("target(1) = ");
|
||||
writeInteger(computeTarget(1));
|
||||
writeString("\n");
|
||||
writeString("target(2) = ");
|
||||
writeInteger(computeTarget(2));
|
||||
writeString("\n");
|
||||
writeString("target(3) = ");
|
||||
writeInteger(computeTarget(3));
|
||||
writeString("\n");
|
||||
writeString("target(4) = ");
|
||||
writeInteger(computeTarget(4));
|
||||
writeString("\n");
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// build a table of small primes
|
||||
|
||||
global Integer smallPrimesLimit;
|
||||
|
||||
global Integer[] primes;
|
||||
global Integer numPrimes;
|
||||
|
||||
void showSmallPrimes() {
|
||||
local Integer i;
|
||||
local Integer k;
|
||||
i = 0;
|
||||
k = 0;
|
||||
while (i < numPrimes) {
|
||||
writeInteger(primes[i]);
|
||||
writeString(" ");
|
||||
k = k + 1;
|
||||
if (k == 8) {
|
||||
k = 0;
|
||||
writeString("\n");
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
if (k != 0) {
|
||||
writeString("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void enterSmallPrime(Integer p) {
|
||||
local Integer n;
|
||||
local Integer[] a;
|
||||
local Integer i;
|
||||
if (sizeof(primes) == numPrimes) {
|
||||
n = 2 * numPrimes;
|
||||
a = new(Integer[n]);
|
||||
i = 0;
|
||||
while (i < numPrimes) {
|
||||
a[i] = primes[i];
|
||||
i = i + 1;
|
||||
}
|
||||
primes = a;
|
||||
}
|
||||
primes[numPrimes] = p;
|
||||
numPrimes = numPrimes + 1;
|
||||
}
|
||||
|
||||
Boolean isPrime(Integer n) {
|
||||
local Integer t;
|
||||
t = 3;
|
||||
while (t * t <= n) {
|
||||
if (n % t == 0) {
|
||||
return false;
|
||||
}
|
||||
t = t + 2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void calcSmallPrimes(Integer limit) {
|
||||
local Integer i;
|
||||
smallPrimesLimit = limit;
|
||||
primes = new(Integer[256]);
|
||||
numPrimes = 0;
|
||||
enterSmallPrime(2);
|
||||
enterSmallPrime(3);
|
||||
i = 5;
|
||||
while (true) {
|
||||
if (i > smallPrimesLimit) {
|
||||
break;
|
||||
}
|
||||
if (isPrime(i)) {
|
||||
enterSmallPrime(i);
|
||||
}
|
||||
i = i + 2;
|
||||
if (i > smallPrimesLimit) {
|
||||
break;
|
||||
}
|
||||
if (isPrime(i)) {
|
||||
enterSmallPrime(i);
|
||||
}
|
||||
i = i + 4;
|
||||
}
|
||||
}
|
||||
|
||||
void testCalcSmallPrimes() {
|
||||
writeString("calcSmallPrimes()\n");
|
||||
writeString("-----------------\n");
|
||||
writeString("primes less than or equal to 100:\n");
|
||||
calcSmallPrimes(100);
|
||||
showSmallPrimes();
|
||||
writeString("number of primes less than or equal to 100: ");
|
||||
writeInteger(numPrimes);
|
||||
writeString("\n");
|
||||
calcSmallPrimes(1000);
|
||||
writeString("number of primes less than or equal to 1000: ");
|
||||
writeInteger(numPrimes);
|
||||
writeString("\n");
|
||||
calcSmallPrimes(10000);
|
||||
writeString("number of primes less than or equal to 10000: ");
|
||||
writeInteger(numPrimes);
|
||||
writeString("\n");
|
||||
calcSmallPrimes(100000);
|
||||
writeString("number of primes less than or equal to 100000: ");
|
||||
writeInteger(numPrimes);
|
||||
writeString("\n");
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// try to find a small prime factor of a given number
|
||||
|
||||
Integer smallPrimeFactor(Integer n) {
|
||||
local Integer i;
|
||||
i = 0;
|
||||
while (i < numPrimes) {
|
||||
if (n % primes[i] == 0) {
|
||||
// prime factor found
|
||||
return primes[i];
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
// no prime factor less than or equal to smallPrimesLimit found
|
||||
return 0;
|
||||
}
|
||||
|
||||
void testSmallPrimeFactor() {
|
||||
calcSmallPrimes(10000);
|
||||
writeString("smallPrimeFactor()\n");
|
||||
writeString("------------------\n");
|
||||
writeString("small prime factor of 2: ");
|
||||
writeInteger(smallPrimeFactor(2));
|
||||
writeString("\n");
|
||||
writeString("small prime factor of 222: ");
|
||||
writeInteger(smallPrimeFactor(222));
|
||||
writeString("\n");
|
||||
writeString("small prime factor of 17*19*23: ");
|
||||
writeInteger(smallPrimeFactor(17*19*23));
|
||||
writeString("\n");
|
||||
writeString("small prime factor of 7919: ");
|
||||
writeInteger(smallPrimeFactor(7919));
|
||||
writeString("\n");
|
||||
writeString("small prime factor of 987654323: ");
|
||||
writeInteger(smallPrimeFactor(987654323));
|
||||
writeString("\n");
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// compute b^n mod m
|
||||
|
||||
Integer powerMod(Integer b, Integer n, Integer m) {
|
||||
local Integer a;
|
||||
a = 1;
|
||||
while (n != 0) {
|
||||
if (n % 2 == 0) {
|
||||
b = (b * b) % m;
|
||||
n = n / 2;
|
||||
} else {
|
||||
a = (a * b) % m;
|
||||
n = n - 1;
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
void testPowerMod() {
|
||||
writeString("powerMod()\n");
|
||||
writeString("----------\n");
|
||||
writeString("2^16 mod 7: ");
|
||||
writeInteger(powerMod(2, 16, 7));
|
||||
writeString("\n");
|
||||
writeString("3^10 mod 19: ");
|
||||
writeInteger(powerMod(3, 10, 19));
|
||||
writeString("\n");
|
||||
writeString("123^987654323 mod 987654323: ");
|
||||
writeInteger(powerMod(123, 987654323, 987654323));
|
||||
writeString("\n");
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// compute greatest common divisor
|
||||
|
||||
Integer GCD(Integer a, Integer b) {
|
||||
local Integer r;
|
||||
if (a < 0) {
|
||||
a = -a;
|
||||
}
|
||||
if (b < 0) {
|
||||
b = -b;
|
||||
}
|
||||
while (b != 0) {
|
||||
r = a % b;
|
||||
a = b;
|
||||
b = r;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
void testGCD() {
|
||||
writeString("GCD()\n");
|
||||
writeString("-----\n");
|
||||
writeString("GCD(3*5*5*11*37, 2*2*5*37*53) = ");
|
||||
writeInteger(GCD(3*5*5*11*37, 2*2*5*37*53));
|
||||
writeString("\n");
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// Rabin-Miller test
|
||||
|
||||
Boolean isComposite(Integer n) {
|
||||
local Integer q;
|
||||
local Integer t;
|
||||
local Integer i;
|
||||
local Integer a;
|
||||
local Integer e;
|
||||
local Integer b;
|
||||
q = n - 1;
|
||||
t = 0;
|
||||
while (q % 2 == 0) {
|
||||
q = q / 2;
|
||||
t = t + 1;
|
||||
}
|
||||
i = 0;
|
||||
while (i < 20) {
|
||||
a = primes[i];
|
||||
if (a >= n) {
|
||||
break;
|
||||
}
|
||||
e = 0;
|
||||
b = powerMod(a, q, n);
|
||||
if (b != 1) {
|
||||
while (b != 1 && b != n - 1 && e <= t - 2) {
|
||||
b = (b * b) % n;
|
||||
e = e + 1;
|
||||
}
|
||||
if (b != n - 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void testIsComposite() {
|
||||
local Integer n;
|
||||
local Integer k;
|
||||
writeString("isComposite()\n");
|
||||
writeString("-------------\n");
|
||||
writeString("odd numbers in [3..99] which are probably prime:\n");
|
||||
n = 3;
|
||||
k = 0;
|
||||
while (n < 100) {
|
||||
if (!isComposite(n)) {
|
||||
writeInteger(n);
|
||||
writeString(" ");
|
||||
k = k + 1;
|
||||
if (k == 8) {
|
||||
k = 0;
|
||||
writeString("\n");
|
||||
}
|
||||
}
|
||||
n = n + 2;
|
||||
}
|
||||
if (k != 0) {
|
||||
writeString("\n");
|
||||
}
|
||||
n = 1234567;
|
||||
while (n < 1234607) {
|
||||
writeInteger(n);
|
||||
writeString(" is ");
|
||||
if (isComposite(n)) {
|
||||
writeString("definitely composite\n");
|
||||
} else {
|
||||
writeString("probably prime\n");
|
||||
}
|
||||
n = n + 2;
|
||||
}
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
Boolean provePrime(Integer n) {
|
||||
// not implemented yet
|
||||
return false;
|
||||
}
|
||||
|
||||
void testProvePrime() {
|
||||
writeString("provePrime()\n");
|
||||
writeString("------------\n");
|
||||
writeString("<not implemented yet>\n");
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// Pollard's rho method
|
||||
|
||||
Integer findFactor1(Integer n) {
|
||||
local Integer y;
|
||||
local Integer x;
|
||||
local Integer x1;
|
||||
local Integer k;
|
||||
local Integer l;
|
||||
local Integer p;
|
||||
local Integer c;
|
||||
local Integer g;
|
||||
local Integer r;
|
||||
y = 2;
|
||||
x = 2;
|
||||
x1 = 2;
|
||||
k = 1;
|
||||
l = 1;
|
||||
p = 1;
|
||||
c = 0;
|
||||
while (true) {
|
||||
x = (x * x + 1) % n;
|
||||
p = (p * (x1 - x)) % n;
|
||||
c = c + 1;
|
||||
if (c == 20) {
|
||||
g = GCD(p, n);
|
||||
if (g > 1) {
|
||||
break;
|
||||
}
|
||||
y = x;
|
||||
c = 0;
|
||||
}
|
||||
k = k - 1;
|
||||
if (k == 0) {
|
||||
g = GCD(p, n);
|
||||
if (g > 1) {
|
||||
break;
|
||||
}
|
||||
x1 = x;
|
||||
k = l;
|
||||
l = 2 * l;
|
||||
r = 0;
|
||||
while (r < k) {
|
||||
x = (x * x + 1) % n;
|
||||
r = r + 1;
|
||||
}
|
||||
y = x;
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
do {
|
||||
y = (y * y + 1) % n;
|
||||
g = GCD(x1 - y, n);
|
||||
} while (g == 1);
|
||||
if (g < n) {
|
||||
return g;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Integer findFactor2(Integer n) {
|
||||
local Integer y;
|
||||
local Integer x;
|
||||
local Integer x1;
|
||||
local Integer k;
|
||||
local Integer l;
|
||||
local Integer p;
|
||||
local Integer c;
|
||||
local Integer g;
|
||||
local Integer r;
|
||||
y = 2;
|
||||
x = 2;
|
||||
x1 = 2;
|
||||
k = 1;
|
||||
l = 1;
|
||||
p = 1;
|
||||
c = 0;
|
||||
while (true) {
|
||||
x = (x * x - 1) % n;
|
||||
p = (p * (x1 - x)) % n;
|
||||
c = c + 1;
|
||||
if (c == 20) {
|
||||
g = GCD(p, n);
|
||||
if (g > 1) {
|
||||
break;
|
||||
}
|
||||
y = x;
|
||||
c = 0;
|
||||
}
|
||||
k = k - 1;
|
||||
if (k == 0) {
|
||||
g = GCD(p, n);
|
||||
if (g > 1) {
|
||||
break;
|
||||
}
|
||||
x1 = x;
|
||||
k = l;
|
||||
l = 2 * l;
|
||||
r = 0;
|
||||
while (r < k) {
|
||||
x = (x * x - 1) % n;
|
||||
r = r + 1;
|
||||
}
|
||||
y = x;
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
do {
|
||||
y = (y * y - 1) % n;
|
||||
g = GCD(x1 - y, n);
|
||||
} while (g == 1);
|
||||
if (g < n) {
|
||||
return g;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Integer findFactor3(Integer n) {
|
||||
local Integer y;
|
||||
local Integer x;
|
||||
local Integer x1;
|
||||
local Integer k;
|
||||
local Integer l;
|
||||
local Integer p;
|
||||
local Integer c;
|
||||
local Integer g;
|
||||
local Integer r;
|
||||
y = 2;
|
||||
x = 2;
|
||||
x1 = 2;
|
||||
k = 1;
|
||||
l = 1;
|
||||
p = 1;
|
||||
c = 0;
|
||||
while (true) {
|
||||
x = (x * x + 3) % n;
|
||||
p = (p * (x1 - x)) % n;
|
||||
c = c + 1;
|
||||
if (c == 20) {
|
||||
g = GCD(p, n);
|
||||
if (g > 1) {
|
||||
break;
|
||||
}
|
||||
y = x;
|
||||
c = 0;
|
||||
}
|
||||
k = k - 1;
|
||||
if (k == 0) {
|
||||
g = GCD(p, n);
|
||||
if (g > 1) {
|
||||
break;
|
||||
}
|
||||
x1 = x;
|
||||
k = l;
|
||||
l = 2 * l;
|
||||
r = 0;
|
||||
while (r < k) {
|
||||
x = (x * x + 3) % n;
|
||||
r = r + 1;
|
||||
}
|
||||
y = x;
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
do {
|
||||
y = (y * y + 3) % n;
|
||||
g = GCD(x1 - y, n);
|
||||
} while (g == 1);
|
||||
if (g < n) {
|
||||
return g;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Integer findFactor(Integer n) {
|
||||
local Integer r;
|
||||
r = findFactor1(n);
|
||||
if (r != 0) {
|
||||
return r;
|
||||
}
|
||||
r = findFactor2(n);
|
||||
if (r != 0) {
|
||||
return r;
|
||||
}
|
||||
r = findFactor3(n);
|
||||
return r;
|
||||
}
|
||||
|
||||
void testFindFactor() {
|
||||
writeString("findFactor()\n");
|
||||
writeString("------------\n");
|
||||
writeString("5*5*5 = ");
|
||||
writeInteger(5*5*5);
|
||||
writeString(" is a multiple of ");
|
||||
writeInteger(findFactor(5*5*5));
|
||||
writeString("\n");
|
||||
writeString("4421*5743*7699 = ");
|
||||
writeInteger(4421*5743*7699);
|
||||
writeString(" is a multiple of ");
|
||||
writeInteger(findFactor(4421*5743*7699));
|
||||
writeString("\n");
|
||||
writeInteger(9999000099990001);
|
||||
writeString(" is a multiple of ");
|
||||
writeInteger(findFactor(9999000099990001));
|
||||
writeString("\n");
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
List factorize(Integer x, Boolean verbose) {
|
||||
local List factors;
|
||||
local Integer f;
|
||||
local Integer f1;
|
||||
local Integer f2;
|
||||
local List moreFactors;
|
||||
if (verbose) {
|
||||
writeString("factorize(");
|
||||
writeInteger(x);
|
||||
writeString(")\n");
|
||||
}
|
||||
factors = nil;
|
||||
while (x > 1) {
|
||||
f = smallPrimeFactor(x);
|
||||
if (f == 0) {
|
||||
// no small prime factor found
|
||||
if (x < smallPrimesLimit * smallPrimesLimit) {
|
||||
// we know that x is prime
|
||||
f = x;
|
||||
} else {
|
||||
// we don't know anything
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (verbose) {
|
||||
writeString("detected small prime factor ");
|
||||
writeInteger(f);
|
||||
writeString("\n");
|
||||
}
|
||||
factors = addToList(f, factors);
|
||||
x = x / f;
|
||||
}
|
||||
if (x == 1) {
|
||||
// x has been completely factorized
|
||||
if (verbose) {
|
||||
writeString("the number has been completely factorized\n");
|
||||
}
|
||||
return factors;
|
||||
}
|
||||
if (verbose) {
|
||||
writeString("interim result:\n the remaining factor ");
|
||||
writeInteger(x);
|
||||
writeString("\n doesn't have any prime factors < ");
|
||||
writeInteger(smallPrimesLimit);
|
||||
writeString("\n ");
|
||||
}
|
||||
if (isComposite(x)) {
|
||||
// x is definitely composite
|
||||
if (verbose) {
|
||||
writeString("but is definitely composite\n");
|
||||
}
|
||||
f1 = findFactor(x);
|
||||
if (f1 == 0) {
|
||||
// cannot factorize x, give up
|
||||
writeString("cannot factorize ");
|
||||
writeInteger(x);
|
||||
writeString(", giving up\n");
|
||||
factors = addToList(x, factors);
|
||||
} else {
|
||||
// x = f1 * f2
|
||||
f2 = x / f1;
|
||||
if (verbose) {
|
||||
writeString("this number can be split into ");
|
||||
writeInteger(f1);
|
||||
writeString(" and ");
|
||||
writeInteger(f2);
|
||||
writeString("\n");
|
||||
}
|
||||
moreFactors = factorize(f1, verbose);
|
||||
factors = fuseLists(moreFactors, factors);
|
||||
moreFactors = factorize(f2, verbose);
|
||||
factors = fuseLists(moreFactors, factors);
|
||||
}
|
||||
} else {
|
||||
// x is very probably prime
|
||||
if (verbose) {
|
||||
writeString("and is very probably prime\n");
|
||||
}
|
||||
if (provePrime(x)) {
|
||||
if (verbose) {
|
||||
writeString("the primality of ");
|
||||
writeInteger(x);
|
||||
writeString(" has been proven\n");
|
||||
}
|
||||
} else {
|
||||
writeString("cannot prove the primality of ");
|
||||
writeInteger(x);
|
||||
writeString(", giving up\n");
|
||||
}
|
||||
factors = addToList(x, factors);
|
||||
}
|
||||
return factors;
|
||||
}
|
||||
|
||||
void testFactorize(Boolean verbose) {
|
||||
local List factors;
|
||||
writeString("factorize()\n");
|
||||
writeString("-----------\n");
|
||||
calcSmallPrimes(7);
|
||||
showSmallPrimes();
|
||||
writeString("3*5*7*7*141*49 = \n");
|
||||
factors = factorize(3*5*7*7*141*49, verbose);
|
||||
showList(sortList(factors));
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
void showBar() {
|
||||
writeString("---------------------------------");
|
||||
writeString("-------------------------------\n");
|
||||
}
|
||||
|
||||
void run(Boolean verbose) {
|
||||
local Integer i;
|
||||
local Integer x;
|
||||
local List factors;
|
||||
local Integer y;
|
||||
calcSmallPrimes(10000);
|
||||
showBar();
|
||||
i = 1;
|
||||
while (i <= 30) {
|
||||
writeString("10^");
|
||||
writeInteger(i);
|
||||
writeString("+1 = ");
|
||||
x = computeTarget(i);
|
||||
writeInteger(x);
|
||||
writeString(" = \n");
|
||||
factors = factorize(x, verbose);
|
||||
factors = sortList(factors);
|
||||
showList(factors);
|
||||
writeString("check: product = ");
|
||||
y = evalList(factors);
|
||||
writeInteger(y);
|
||||
writeString("\n");
|
||||
showBar();
|
||||
i = i+ 1;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
void tests(Boolean verbose) {
|
||||
writeString("\nTests\n");
|
||||
writeString("=====\n\n");
|
||||
testComputeTarget();
|
||||
testCalcSmallPrimes();
|
||||
testSmallPrimeFactor();
|
||||
testPowerMod();
|
||||
testGCD();
|
||||
testIsComposite();
|
||||
testProvePrime();
|
||||
testFindFactor();
|
||||
testFactorize(verbose);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
void main() {
|
||||
//tests(true);
|
||||
run(true);
|
||||
}
|
||||
@@ -2,11 +2,8 @@
|
||||
#define INSREUKTION
|
||||
|
||||
#define IMMEDIATE(x) ((x) & 0x00FFFFFF)
|
||||
#define MSB (1 << (8 * sizeof(unsigned int) - 1))
|
||||
#define IS_PRIMITIVE(objRef) (((objRef)->size & MSB) == 0)
|
||||
|
||||
#define SIGN_EXTEND(i) ((i) & 0x00800000 ? (i) | 0xFF000000 : (i))
|
||||
#define GET_ELEMENT_COUNT(objRef) ((objRef)->size & ~MSB)
|
||||
#define GET_REFS_PTR(objRef) ((ObjRef *) (objRef)-> data)
|
||||
|
||||
#define HALT 0
|
||||
#define PUSHC 1
|
||||
@@ -37,18 +34,5 @@
|
||||
#define CALL 26
|
||||
#define RET 27
|
||||
#define DROP 28
|
||||
#define PUSHR 29
|
||||
#define POPR 30
|
||||
#define DUP 31
|
||||
#define NEW 32
|
||||
#define GETF 33
|
||||
#define PUTF 34
|
||||
#define NEWA 35
|
||||
#define GETFA 36
|
||||
#define PUTFA 37
|
||||
#define GETSZ 38
|
||||
#define PUSHN 39
|
||||
#define REFEQ 40
|
||||
#define REFNE 41
|
||||
|
||||
#endif /* ifndef INSREUKTION */
|
||||
|
||||
778
matinv.asm
778
matinv.asm
@@ -1,778 +0,0 @@
|
||||
//
|
||||
// version
|
||||
//
|
||||
.vers 8
|
||||
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Integer gcd(Integer, Integer)
|
||||
//
|
||||
_gcd:
|
||||
asf 1
|
||||
jmp __2
|
||||
__1:
|
||||
pushl -4
|
||||
pushl -3
|
||||
mod
|
||||
popl 0
|
||||
pushl -3
|
||||
popl -4
|
||||
pushl 0
|
||||
popl -3
|
||||
__2:
|
||||
pushl -3
|
||||
pushc 0
|
||||
ne
|
||||
brt __1
|
||||
__3:
|
||||
pushl -4
|
||||
popr
|
||||
jmp __0
|
||||
__0:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } newFraction(Integer, Integer)
|
||||
//
|
||||
_newFraction:
|
||||
asf 4
|
||||
pushl -4
|
||||
pushc 0
|
||||
lt
|
||||
brf __5
|
||||
pushc 0
|
||||
pushl -4
|
||||
sub
|
||||
popl 0
|
||||
jmp __6
|
||||
__5:
|
||||
pushl -4
|
||||
popl 0
|
||||
__6:
|
||||
pushl -3
|
||||
pushc 0
|
||||
lt
|
||||
brf __7
|
||||
pushc 0
|
||||
pushl -3
|
||||
sub
|
||||
popl 1
|
||||
jmp __8
|
||||
__7:
|
||||
pushl -3
|
||||
popl 1
|
||||
__8:
|
||||
pushl 0
|
||||
pushl 1
|
||||
call _gcd
|
||||
drop 2
|
||||
pushr
|
||||
popl 2
|
||||
new 2
|
||||
popl 3
|
||||
pushl -4
|
||||
pushc 0
|
||||
lt
|
||||
pushl -3
|
||||
pushc 0
|
||||
lt
|
||||
ne
|
||||
brf __9
|
||||
pushl 3
|
||||
pushc 0
|
||||
pushl 0
|
||||
sub
|
||||
pushl 2
|
||||
div
|
||||
putf 0
|
||||
jmp __10
|
||||
__9:
|
||||
pushl 3
|
||||
pushl 0
|
||||
pushl 2
|
||||
div
|
||||
putf 0
|
||||
__10:
|
||||
pushl 3
|
||||
pushl 1
|
||||
pushl 2
|
||||
div
|
||||
putf 1
|
||||
pushl 3
|
||||
popr
|
||||
jmp __4
|
||||
__4:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeFraction(record { Integer num; Integer den; })
|
||||
//
|
||||
_writeFraction:
|
||||
asf 0
|
||||
pushl -3
|
||||
getf 0
|
||||
call _writeInteger
|
||||
drop 1
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 47
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl -3
|
||||
getf 1
|
||||
call _writeInteger
|
||||
drop 1
|
||||
__11:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } negFraction(record { Integer num; Integer den; })
|
||||
//
|
||||
_negFraction:
|
||||
asf 0
|
||||
pushc 0
|
||||
pushl -3
|
||||
getf 0
|
||||
sub
|
||||
pushl -3
|
||||
getf 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __12
|
||||
__12:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } addFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_addFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -3
|
||||
getf 0
|
||||
pushl -4
|
||||
getf 1
|
||||
mul
|
||||
add
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __13
|
||||
__13:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } subFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_subFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -3
|
||||
getf 0
|
||||
pushl -4
|
||||
getf 1
|
||||
mul
|
||||
sub
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __14
|
||||
__14:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } mulFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_mulFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 0
|
||||
mul
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __15
|
||||
__15:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } divFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_divFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 0
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __16
|
||||
__16:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Fraction[][] newMatrix(record { Integer num; Integer den; }, record { Integer num; Integer den; }, record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_newMatrix:
|
||||
asf 1
|
||||
pushc 2
|
||||
newa
|
||||
popl 0
|
||||
pushl 0
|
||||
pushc 0
|
||||
pushc 2
|
||||
newa
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
pushc 2
|
||||
newa
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
pushl -6
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
pushl -5
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
pushl -4
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
pushl -3
|
||||
putfa
|
||||
pushl 0
|
||||
popr
|
||||
jmp __17
|
||||
__17:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeMatrix(Fraction[][])
|
||||
//
|
||||
_writeMatrix:
|
||||
asf 2
|
||||
pushc 0
|
||||
popl 0
|
||||
jmp __20
|
||||
__19:
|
||||
pushc 0
|
||||
popl 1
|
||||
jmp __23
|
||||
__22:
|
||||
pushl -3
|
||||
pushl 0
|
||||
getfa
|
||||
pushl 1
|
||||
getfa
|
||||
call _writeFraction
|
||||
drop 1
|
||||
pushc 2
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 1
|
||||
pushc 32
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl 1
|
||||
pushc 1
|
||||
add
|
||||
popl 1
|
||||
__23:
|
||||
pushl 1
|
||||
pushl -3
|
||||
pushl 0
|
||||
getfa
|
||||
getsz
|
||||
lt
|
||||
brt __22
|
||||
__24:
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl 0
|
||||
pushc 1
|
||||
add
|
||||
popl 0
|
||||
__20:
|
||||
pushl 0
|
||||
pushl -3
|
||||
getsz
|
||||
lt
|
||||
brt __19
|
||||
__21:
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
__18:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Fraction[][] invertMatrix(Fraction[][])
|
||||
//
|
||||
_invertMatrix:
|
||||
asf 1
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
call _mulFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
call _mulFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _subFraction
|
||||
drop 2
|
||||
pushr
|
||||
popl 0
|
||||
pushl 0
|
||||
getf 0
|
||||
pushc 0
|
||||
eq
|
||||
brf __26
|
||||
pushc 33
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 1
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 2
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 3
|
||||
pushc 111
|
||||
putfa
|
||||
dup
|
||||
pushc 4
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 5
|
||||
pushc 58
|
||||
putfa
|
||||
dup
|
||||
pushc 6
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 7
|
||||
pushc 109
|
||||
putfa
|
||||
dup
|
||||
pushc 8
|
||||
pushc 97
|
||||
putfa
|
||||
dup
|
||||
pushc 9
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 10
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 11
|
||||
pushc 105
|
||||
putfa
|
||||
dup
|
||||
pushc 12
|
||||
pushc 120
|
||||
putfa
|
||||
dup
|
||||
pushc 13
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 14
|
||||
pushc 99
|
||||
putfa
|
||||
dup
|
||||
pushc 15
|
||||
pushc 97
|
||||
putfa
|
||||
dup
|
||||
pushc 16
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 17
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 18
|
||||
pushc 111
|
||||
putfa
|
||||
dup
|
||||
pushc 19
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 20
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 21
|
||||
pushc 98
|
||||
putfa
|
||||
dup
|
||||
pushc 22
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 23
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 24
|
||||
pushc 105
|
||||
putfa
|
||||
dup
|
||||
pushc 25
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 26
|
||||
pushc 118
|
||||
putfa
|
||||
dup
|
||||
pushc 27
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 28
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 29
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 30
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 31
|
||||
pushc 100
|
||||
putfa
|
||||
dup
|
||||
pushc 32
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
call _exit
|
||||
__26:
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
call _negFraction
|
||||
drop 1
|
||||
pushr
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
call _negFraction
|
||||
drop 1
|
||||
pushr
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _newMatrix
|
||||
drop 4
|
||||
pushr
|
||||
popr
|
||||
jmp __25
|
||||
__25:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void main()
|
||||
//
|
||||
_main:
|
||||
asf 3
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushc 7
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 4
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 6
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 5
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _newMatrix
|
||||
drop 4
|
||||
pushr
|
||||
popl 0
|
||||
pushl 0
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
pushl 0
|
||||
call _invertMatrix
|
||||
drop 1
|
||||
pushr
|
||||
popl 1
|
||||
pushl 1
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
pushl 1
|
||||
call _invertMatrix
|
||||
drop 1
|
||||
pushr
|
||||
popl 2
|
||||
pushl 2
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
__27:
|
||||
rsf
|
||||
ret
|
||||
BIN
matinv.bin
BIN
matinv.bin
Binary file not shown.
146
matinv.nj
146
matinv.nj
@@ -1,146 +0,0 @@
|
||||
//
|
||||
// matinv.nj -- invert 2x2 matrices of fractions
|
||||
//
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// greatest common divisor
|
||||
|
||||
Integer gcd(Integer a, Integer b) {
|
||||
local Integer h;
|
||||
while (b != 0) {
|
||||
h = a % b;
|
||||
a = b;
|
||||
b = h;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// fractions
|
||||
|
||||
type Fraction = record {
|
||||
Integer num;
|
||||
Integer den;
|
||||
};
|
||||
|
||||
Fraction newFraction(Integer num, Integer den) {
|
||||
local Integer n;
|
||||
local Integer d;
|
||||
local Integer g;
|
||||
local Fraction r;
|
||||
if (num < 0) {
|
||||
n = -num;
|
||||
} else {
|
||||
n = num;
|
||||
}
|
||||
if (den < 0) {
|
||||
d = -den;
|
||||
} else {
|
||||
d = den;
|
||||
}
|
||||
g = gcd(n, d);
|
||||
r = new(Fraction);
|
||||
if ((num < 0) != (den < 0)) {
|
||||
r.num = -n / g;
|
||||
} else {
|
||||
r.num = n / g;
|
||||
}
|
||||
r.den = d / g;
|
||||
return r;
|
||||
}
|
||||
|
||||
void writeFraction(Fraction f) {
|
||||
writeInteger(f.num);
|
||||
writeString("/");
|
||||
writeInteger(f.den);
|
||||
}
|
||||
|
||||
Fraction negFraction(Fraction f) {
|
||||
return newFraction(-f.num, f.den);
|
||||
}
|
||||
|
||||
Fraction addFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction subFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den - f2.num * f1.den, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction mulFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.num, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction divFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den, f1.den * f2.num);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// 2x2 matrices of fractions
|
||||
|
||||
type Matrix = Fraction[][];
|
||||
|
||||
Matrix newMatrix(Fraction a00, Fraction a01,
|
||||
Fraction a10, Fraction a11) {
|
||||
local Matrix m;
|
||||
m = new(Fraction[2][]);
|
||||
m[0] = new(Fraction[2]);
|
||||
m[1] = new(Fraction[2]);
|
||||
m[0][0] = a00;
|
||||
m[0][1] = a01;
|
||||
m[1][0] = a10;
|
||||
m[1][1] = a11;
|
||||
return m;
|
||||
}
|
||||
|
||||
void writeMatrix(Matrix m) {
|
||||
local Integer i;
|
||||
local Integer j;
|
||||
i = 0;
|
||||
while (i < sizeof(m)) {
|
||||
j = 0;
|
||||
while (j < sizeof(m[i])) {
|
||||
writeFraction(m[i][j]);
|
||||
writeString(" ");
|
||||
j = j + 1;
|
||||
}
|
||||
writeString("\n");
|
||||
i = i + 1;
|
||||
}
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
Matrix invertMatrix(Matrix m) {
|
||||
local Fraction det;
|
||||
det = subFraction(mulFraction(m[0][0], m[1][1]),
|
||||
mulFraction(m[0][1], m[1][0]));
|
||||
if (det.num == 0) {
|
||||
writeString("error: matrix cannot be inverted\n");
|
||||
exit();
|
||||
}
|
||||
return newMatrix(
|
||||
divFraction(m[1][1], det), divFraction(negFraction(m[0][1]), det),
|
||||
divFraction(negFraction(m[1][0]), det), divFraction(m[0][0], det)
|
||||
);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
void main() {
|
||||
local Matrix matrix;
|
||||
local Matrix result1;
|
||||
local Matrix result2;
|
||||
writeString("\n");
|
||||
matrix = newMatrix(
|
||||
newFraction(7, 1), newFraction(4, 1),
|
||||
newFraction(6, 1), newFraction(5, 1)
|
||||
);
|
||||
writeMatrix(matrix);
|
||||
result1 = invertMatrix(matrix);
|
||||
writeMatrix(result1);
|
||||
result2 = invertMatrix(result1);
|
||||
writeMatrix(result2);
|
||||
}
|
||||
BIN
njvm?inline=false.2 → nja-mac
Normal file → Executable file
BIN
njvm?inline=false.2 → nja-mac
Normal file → Executable file
Binary file not shown.
Binary file not shown.
11070
njlisp.asm
11070
njlisp.asm
File diff suppressed because it is too large
Load Diff
BIN
njlisp.bin
BIN
njlisp.bin
Binary file not shown.
BIN
njvm7 → njvm-3
BIN
njvm7 → njvm-3
Binary file not shown.
512
njvm.c
512
njvm.c
@@ -1,354 +1,306 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "consts.c"
|
||||
#include "instruktion.c"
|
||||
#include "code.c"
|
||||
#include "stack.c"
|
||||
#include "program.c"
|
||||
#include "codeReader.c"
|
||||
#include "debugMenu.c"
|
||||
#include "bigint.h"
|
||||
#include "record.c"
|
||||
#include "GC.c"
|
||||
#include "SDA.c"
|
||||
|
||||
// Debug
|
||||
int debug = 0;
|
||||
|
||||
// Program
|
||||
struct program program;
|
||||
struct program *program;
|
||||
|
||||
// SDA
|
||||
struct sda sda;
|
||||
|
||||
// Stack
|
||||
struct stack stack;
|
||||
#define SIZE 64
|
||||
|
||||
//Register
|
||||
struct stack reg;
|
||||
|
||||
unsigned fp;
|
||||
int fp;
|
||||
int debug = 0;
|
||||
|
||||
void version(void) {
|
||||
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__);
|
||||
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", VERSION, __DATE__, __TIME__);
|
||||
}
|
||||
|
||||
void help(void) {
|
||||
printf("Usage: ./njvm [options] <code file>\n\t--debug\tstart virtual machine in debug mode\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n");
|
||||
}
|
||||
|
||||
void execute(struct program program) {
|
||||
int bp = -1;
|
||||
int i;
|
||||
void execute(struct program *program, struct sda *sda) {
|
||||
struct stack stack;
|
||||
stack.size = 1000;
|
||||
stack.currentFrame = 0;
|
||||
struct stack callStack;
|
||||
callStack.size = 1000;
|
||||
callStack.currentFrame = 0;
|
||||
|
||||
for (int i = 0; i < stack.size; ++i) {
|
||||
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
|
||||
frame->fp = malloc(sizeof(int) * stack.size);
|
||||
frame->sp = malloc(sizeof(int) * stack.size);
|
||||
frame->bp = NULL;
|
||||
stack.frames[i] = frame;
|
||||
}
|
||||
|
||||
for (int i = 0; i < callStack.size; ++i) {
|
||||
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
|
||||
frame->fp = malloc(sizeof(int) * stack.size);
|
||||
frame->sp = malloc(sizeof(int) * stack.size);
|
||||
frame->bp = NULL;
|
||||
callStack.frames[i] = frame;
|
||||
}
|
||||
|
||||
struct stackFrame *currentFrame = NULL;
|
||||
//struct stackFrame *currentCallFrame = NULL;
|
||||
unsigned int tmp;
|
||||
int intInput;
|
||||
char charInput;
|
||||
StackSlot tempSlot;
|
||||
ObjRef tempObj;
|
||||
ObjRef tempObj2;
|
||||
int tempInt;
|
||||
for (i = 0; i < *program.size; ++i) {
|
||||
if (debug == 1 || bp == i) debugMenu(fp, stack, &debug, i, &bp);
|
||||
if(debug == 1) printf("(%i)",i);
|
||||
switch (program.program[i] >> 24) {
|
||||
|
||||
for (int i = 0; i < program->size; ++i) {
|
||||
unsigned int instruction = program->program[i];
|
||||
if (i >= program->size) {
|
||||
printf("Error: Jump out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
switch (instruction >> 24) {
|
||||
case HALT:
|
||||
if (debug == 1) printf("halt\n");
|
||||
goto end;
|
||||
case PUSHC:
|
||||
if (debug == 1) printf("pushc: %i\n", IMMEDIATE(program.program[i]));
|
||||
bigFromInt(SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
if (debug == 1) printf("PUSHC %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
push(&stack, SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
break;
|
||||
case ADD:
|
||||
if (debug == 1) printf("add: %i + %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigAdd();
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
if (debug == 1) printf("ADD\n");
|
||||
push(&stack, pop(&stack) + pop(&stack));
|
||||
break;
|
||||
case SUB:
|
||||
if (debug == 1) printf("sub: %i - %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigSub();
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
tmp = pop(&stack);
|
||||
if (debug == 1) printf("SUB\n");
|
||||
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||
push(&stack, pop(&stack) - tmp);
|
||||
break;
|
||||
case MUL:
|
||||
if (debug == 1) printf("mul: %i * %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigMul();
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
if (debug == 1) printf("MUL\n");
|
||||
push(&stack, pop(&stack) * pop(&stack));
|
||||
break;
|
||||
case DIV:
|
||||
if (debug == 1) printf("div: %i / %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigDiv();
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
tmp = pop(&stack);
|
||||
if (debug == 1) printf("DIV\n");
|
||||
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||
push(&stack, pop(&stack) / tmp);
|
||||
break;
|
||||
case MOD:
|
||||
if (debug == 1) printf("mod: %i %% %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigDiv();
|
||||
push(stack, stackSlotWithObjRef(bip.rem));
|
||||
tmp = pop(&stack);
|
||||
if (debug == 1) printf("MOD\n");
|
||||
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||
push(&stack, pop(&stack) % tmp);
|
||||
break;
|
||||
case RDINT:
|
||||
if (debug == 1) printf("rdint\n");
|
||||
bigRead(stdin);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
if (debug == 1) printf("pushed %i\n", peek(stack, 1));
|
||||
scanf("%i", &intInput);
|
||||
if (debug == 1) printf("RDINT %d\n", intInput);
|
||||
push(&stack, intInput);
|
||||
break;
|
||||
case WRINT:
|
||||
if (debug == 1) printf("wrint: %i\n", peek(stack, 1));
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigPrint(stdout);
|
||||
if (debug == 1) printf("WRINT\n");
|
||||
printf("%i", pop(&stack));
|
||||
break;
|
||||
case RDCHR:
|
||||
if (debug == 1) printf("rdchr\n");
|
||||
scanf("%c", &charInput);
|
||||
bigFromInt(charInput);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
if (debug == 1) printf("pushed %c\n", charInput);
|
||||
if (debug == 1) printf("RDCHR %c\n", charInput);
|
||||
push(&stack, charInput);
|
||||
break;
|
||||
case WRCHR:
|
||||
if (debug == 1) printf("wrchr: %c\n", peek(stack, 1));
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
printf("%c", bigToInt());
|
||||
if (debug == 1) printf("WRCHR\n");
|
||||
printf("%c", pop(&stack));
|
||||
break;
|
||||
case PUSHG:
|
||||
if (debug == 1) printf("pushg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stackSlotWithObjRef(getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)));
|
||||
if (debug == 1) printf("PUSHG %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->bp = currentFrame->sp;
|
||||
|
||||
*currentFrame->sp++ = getSDA(SIGN_EXTEND(IMMEDIATE(instruction)), sda);
|
||||
break;
|
||||
case POPG:
|
||||
if (debug == 1) printf("popg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack).u.objRef, sda);
|
||||
if (debug == 1) printf("POPG %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->bp = currentFrame->sp;
|
||||
|
||||
setSDA(SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack), sda);
|
||||
break;
|
||||
case ASF:
|
||||
if (debug == 1) printf("asf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stackSlotWitchNumber(fp));
|
||||
fp = *stack.current;
|
||||
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||
if (debug == 1) printf("ASF %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
push(&stack, *currentFrame->sp);
|
||||
*currentFrame->sp = stack.currentFrame;
|
||||
currentFrame->bp = currentFrame->sp;
|
||||
|
||||
stack.currentFrame += SIGN_EXTEND(IMMEDIATE(instruction));
|
||||
break;
|
||||
case RSF:
|
||||
if (debug == 1) printf("rsf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
*stack.current = fp;
|
||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||
tempSlot = pop(stack);
|
||||
fp = tempSlot.u.number;
|
||||
break;
|
||||
case POPL:
|
||||
if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
|
||||
if (debug == 1) printf("RSF\n");
|
||||
stack.currentFrame = pop(&stack);
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->bp = NULL;
|
||||
break;
|
||||
case PUSHL:
|
||||
if (debug == 1) printf("pushl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
|
||||
printf("PUSHL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->bp = currentFrame->sp;
|
||||
int x = SIGN_EXTEND(IMMEDIATE(instruction));
|
||||
push(&stack, currentFrame->fp[x]);
|
||||
break;
|
||||
case NE:
|
||||
if (debug == 1) printf("ne: %i != %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigFromInt(bigCmp() != 0);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
case POPL:
|
||||
if (debug == 1) printf("POPL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))] = pop(&stack);
|
||||
break;
|
||||
case EQ:
|
||||
if (debug == 1) printf("eq: %i == %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigFromInt(bigCmp() == 0);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case LT:
|
||||
if (debug == 1) printf("lt: %i < %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigFromInt(bigCmp() < 0);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case LE:
|
||||
if (debug == 1) printf("le: %i <= %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigFromInt(bigCmp() <= 0);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case GT:
|
||||
if (debug == 1) printf("gt: %i > %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigFromInt(bigCmp() > 0);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case GE:
|
||||
if (debug == 1) printf("ge: %i >= %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bigFromInt(bigCmp() >= 0);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case BRF:
|
||||
if (debug == 1) printf("brf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
int b = bigToInt();
|
||||
if (b == false) {
|
||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
} else if (b != true) {
|
||||
printf("Error: brf: %i\n", b);
|
||||
exit(EXIT_FAILURE);
|
||||
if (debug == 1) printf("EQ\n");
|
||||
if (pop(&stack) == pop(&stack)) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
break;
|
||||
case BRT:
|
||||
if (debug == 1) printf("brt: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
b = bigToInt();
|
||||
if (b == true) {
|
||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
} else if (b != false) {
|
||||
printf("Error: brt: %i\n", b);
|
||||
exit(EXIT_FAILURE);
|
||||
case NE:
|
||||
if (debug == 1) printf("NE\n");
|
||||
if (pop(&stack) != pop(&stack)) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
break;
|
||||
case LT:
|
||||
if (debug == 1) printf("LT\n");
|
||||
tmp = pop(&stack);
|
||||
if (pop(&stack) < tmp) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
break;
|
||||
case LE:
|
||||
if (debug == 1) printf("LE\n");
|
||||
tmp = pop(&stack);
|
||||
if (pop(&stack) <= tmp) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
break;
|
||||
case GT:
|
||||
if (debug == 1) printf("GT\n");
|
||||
if (debug == 1) printf("peek(1): %d\n", peek(&stack, 1));
|
||||
if (debug == 1) printf("peek(2): %d\n", peek(&stack, 2));
|
||||
if (debug == 1) printf("peek(1) > peek(2): %d\n", peek(&stack, 2) > peek(&stack, 1));
|
||||
tmp = pop(&stack);
|
||||
if (pop(&stack) > tmp) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
break;
|
||||
case GE:
|
||||
if (debug == 1) printf("GE\n");
|
||||
tmp = pop(&stack);
|
||||
if (pop(&stack) >= tmp) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
break;
|
||||
case JMP:
|
||||
if (debug == 1) printf("jmp: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
if (debug == 1) printf("JMP %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||
if (debug == 1) printf("JMP %d\n", j);
|
||||
if (j-- >= program->size) {
|
||||
printf("Error: Jump out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
i = j;
|
||||
break;
|
||||
case BRF: // branch on false
|
||||
if (debug == 1) printf("BRF %d\nStack: %d\n", SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack));
|
||||
if (pop(&stack) == 0) {
|
||||
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||
if (j-- >= program->size) {
|
||||
printf("Error: BRF out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
break;
|
||||
case BRT:
|
||||
if (debug == 1) printf("BRT %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
if (pop(&stack) == 1) {
|
||||
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||
if (j-- >= program->size) {
|
||||
printf("Error: BRT out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
break;
|
||||
case CALL:
|
||||
if (debug == 1) printf("call: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stackSlotWitchNumber(i));
|
||||
if (debug == 1) printf("push: %i\n", i + 1);
|
||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
tmp = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||
if (j-- >= program->size) {
|
||||
printf("Error: Call out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
push(&callStack, i + 1);
|
||||
i = tmp;
|
||||
break;
|
||||
case RET:
|
||||
if (debug == 1) printf("ret\n");
|
||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||
i = getIntValfromStackSlot(pop(stack));
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
if (debug == 1) printf("RET\n");
|
||||
tmp = pop(&callStack);
|
||||
if (tmp-- >= program->size) {
|
||||
printf("Error: Return out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
i = tmp;
|
||||
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");
|
||||
tempObj = pop(stack).u.objRef;
|
||||
push(stack, stackSlotWithObjRef(tempObj));
|
||||
push(stack, stackSlotWithObjRef(tempObj));
|
||||
break;
|
||||
case POPR:
|
||||
if (debug == 1) printf("popr\n");
|
||||
push(reg, pop(stack));
|
||||
break;
|
||||
case PUSHR:
|
||||
if (debug == 1) printf("pushr\n");
|
||||
push(stack, pop(reg));
|
||||
break;
|
||||
case NEW:
|
||||
if (debug == 1) printf("new\t%i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stackSlotWithObjRef(newRecord(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
||||
break;
|
||||
case GETF:
|
||||
if (debug == 1) printf("getf\n");
|
||||
tempObj = pop(stack).u.objRef;
|
||||
push(stack, stackSlotWithObjRef(getField(tempObj,SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
||||
break;
|
||||
case PUTF:
|
||||
if (debug == 1) printf("putf\t%i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
tempObj = pop(stack).u.objRef;
|
||||
tempObj2 = pop(stack).u.objRef;
|
||||
setField(tempObj2, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj);
|
||||
break;
|
||||
case NEWA:
|
||||
if(debug == 1) printf("newa\n");
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
push(stack, stackSlotWithObjRef(newRecord(bigToInt())));
|
||||
break;
|
||||
case GETFA:
|
||||
if(debug == 1) printf("getfa\n");
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
tempInt = bigToInt();
|
||||
tempObj = pop(stack).u.objRef;
|
||||
push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt())));
|
||||
break;
|
||||
case PUTFA:
|
||||
if (debug == 1) printf("putfa\n");
|
||||
tempObj = pop(stack).u.objRef; // Value
|
||||
tempObj2 = pop(stack).u.objRef; // Index
|
||||
bip.op1 = tempObj2;
|
||||
tempInt = bigToInt();
|
||||
setField(pop(stack).u.objRef, tempInt,tempObj);
|
||||
break;
|
||||
case GETSZ:
|
||||
if (debug == 1) printf("getsz\n");
|
||||
tempObj = pop(stack).u.objRef;
|
||||
if(IS_PRIMITIVE(tempObj)) bigFromInt(-1);
|
||||
else bigFromInt(GET_ELEMENT_COUNT(tempObj));
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case PUSHN:
|
||||
if (debug == 1) printf("pushn\n");
|
||||
push(stack, stackSlotWithObjRef(NULL));
|
||||
break;
|
||||
case REFEQ:
|
||||
if (debug == 1) printf("refeq\n");
|
||||
if(pop(stack).u.objRef == pop(stack).u.objRef) bigFromInt(true);
|
||||
else bigFromInt(false);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case REFNE:
|
||||
if (debug == 1) printf("refeq\n");
|
||||
if(pop(stack).u.objRef != pop(stack).u.objRef) bigFromInt(true);
|
||||
else bigFromInt(false);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
tmp = SIGN_EXTEND(IMMEDIATE(instruction));
|
||||
if (debug == 1) printf("DROP %d\n", tmp);
|
||||
for (int b = 0; b < tmp; ++b) {
|
||||
pop(&stack);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
return;
|
||||
}
|
||||
|
||||
// run prog2.bin
|
||||
void tests(void) {
|
||||
printf("Test started\n");
|
||||
/*struct sda *sda = malloc(sizeof(struct sda));
|
||||
unsigned int s[1000];
|
||||
sda->size = 1000;
|
||||
sda->sda = s;
|
||||
fromFile("prog2.bin", program);
|
||||
execute(program, (struct sda *) &sda);*/
|
||||
printf("Test finished\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Init the Heap
|
||||
initHeap(1000);
|
||||
|
||||
// Initialize the Stack
|
||||
struct stack stack;
|
||||
int size = SIZE;
|
||||
int current = 0;
|
||||
stack.size = &size;
|
||||
stack.current = ¤t;
|
||||
stack.stack = malloc(SIZE * 1024);
|
||||
if (stack.stack == NULL) {
|
||||
perror("malloc");
|
||||
}
|
||||
|
||||
// Initialize the registery
|
||||
int rSize = 100000;
|
||||
int rCurrent = 0;
|
||||
StackSlot r[100000];
|
||||
reg.size = &rSize;
|
||||
reg.current = &rCurrent;
|
||||
reg.stack = r;
|
||||
stack.size = size;
|
||||
stack.currentFrame = 0;
|
||||
|
||||
// Initialize ProgrammSpeicher
|
||||
int psize = 100000;
|
||||
int saveProgram = 0;
|
||||
unsigned int p[100000];
|
||||
program.size = &psize;
|
||||
program.program = p;
|
||||
program.saveProgram = &saveProgram;
|
||||
int psize = SIZE;
|
||||
unsigned int p[1000];
|
||||
program = malloc(sizeof(struct program));
|
||||
program->size = psize;
|
||||
program->program = p;
|
||||
|
||||
// Initialize runtime variables
|
||||
int run = 0;
|
||||
int sizeSDA;
|
||||
|
||||
|
||||
if (argc > 1) {
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (strcmp(argv[i], "--debug") == 0) {
|
||||
@@ -359,39 +311,35 @@ int main(int argc, char *argv[]) {
|
||||
} else if (strcmp(argv[i], "--help") == 0) {
|
||||
help();
|
||||
return 0;
|
||||
} else if (strcmp(argv[i], "--stack") == 0) {
|
||||
i++;
|
||||
// TODO: implement stack size
|
||||
} else if (strcmp(argv[i], "--heap") == 0) {
|
||||
i++;
|
||||
// TODO: implement heap size
|
||||
} else if (strcmp(argv[i], "--gcpurge") == 0) {
|
||||
// TODO: implement gcpurge
|
||||
} else {
|
||||
sizeSDA = fromFile(argv[i], program);
|
||||
run = 1;
|
||||
}
|
||||
}
|
||||
// Init the sda
|
||||
ObjRef s[sizeSDA];
|
||||
sda.size = &sizeSDA;
|
||||
sda.sda = s;
|
||||
}
|
||||
|
||||
/*
|
||||
* Debug mode
|
||||
*/
|
||||
if (debug) {
|
||||
tests();
|
||||
}
|
||||
|
||||
/*
|
||||
* Run program
|
||||
*/
|
||||
if (run) {
|
||||
printf("Ninja Virtual Machine started\n");
|
||||
ObjRef s[sizeSDA];
|
||||
sda.size = &sizeSDA;
|
||||
sda.sda = s;
|
||||
struct sda *sda = malloc(sizeof(struct sda));
|
||||
unsigned int s[sizeSDA];
|
||||
sda->size = sizeSDA;
|
||||
sda->sda = s;
|
||||
if (debug == 1) printProgram(program);
|
||||
execute(program);
|
||||
execute(program, (struct sda *) &sda);
|
||||
printf("Ninja Virtual Machine stopped\n");
|
||||
} else {
|
||||
printf("Error: no code file specified\n");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
19
objref.c
19
objref.c
@@ -1,19 +0,0 @@
|
||||
#ifndef OBJREF
|
||||
#define OBJREF
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct ObjRef{
|
||||
// if brkokenHeart and forward_pointer added at this point bip throws errors
|
||||
// bool brokenHeart;
|
||||
// struct ObjRef *forward_pointer;
|
||||
unsigned int size;
|
||||
unsigned char data[1];
|
||||
} *ObjRef;
|
||||
|
||||
typedef struct{
|
||||
unsigned char brokenHeart;
|
||||
ObjRef forward_pointer;
|
||||
} *ObjRefGC;
|
||||
|
||||
#endif /* ifndef OBJREF
|
||||
#define OBJREF */
|
||||
30
program.c
30
program.c
@@ -9,23 +9,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
struct program {
|
||||
int *size;
|
||||
unsigned int *program;
|
||||
int *saveProgram;
|
||||
int size;
|
||||
};
|
||||
|
||||
void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) {
|
||||
void copyToProgram(const unsigned int codeToCopy[], int size, struct program *program) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
program.program[i] = codeToCopy[i];
|
||||
program->program[i] = codeToCopy[i];
|
||||
}
|
||||
*program.size = size;
|
||||
*program.saveProgram = 1;
|
||||
program->size = size;
|
||||
}
|
||||
|
||||
void printProgram(struct program program) {
|
||||
void printProgram(struct program *program) {
|
||||
char c[10];
|
||||
for (int i = 0; i < *program.size; i++) {
|
||||
switch (program.program[i] >> 24) {
|
||||
for (int i = 0; i < program->size; i++) {
|
||||
switch (program->program[i] >> 24) {
|
||||
case PUSHC:
|
||||
strcpy(c, "pushc");
|
||||
break;
|
||||
@@ -86,9 +84,6 @@ void printProgram(struct program program) {
|
||||
case LT:
|
||||
strcpy(c,"lt");
|
||||
break;
|
||||
case LE:
|
||||
strcpy(c, "le");
|
||||
break;
|
||||
case GT:
|
||||
strcpy(c,"gt");
|
||||
break;
|
||||
@@ -113,20 +108,11 @@ void printProgram(struct program program) {
|
||||
case DROP:
|
||||
strcpy(c,"drop");
|
||||
break;
|
||||
case PUSHR:
|
||||
strcpy(c, "pushr");
|
||||
break;
|
||||
case POPR:
|
||||
strcpy(c, "popr");
|
||||
break;
|
||||
case DUP:
|
||||
strcpy(c, "dup");
|
||||
break;
|
||||
default:
|
||||
strcpy(c, "ERROR");
|
||||
break;
|
||||
}
|
||||
IMMEDIATE(program.program[i]) ? printf("%03i:\t%s\t%i\n", i, c, SIGN_EXTEND(IMMEDIATE(program.program[i]))) : printf(
|
||||
IMMEDIATE(program->program[i]) ? printf("%03i:\t%s\t%i\n", i, c, SIGN_EXTEND(IMMEDIATE(program->program[i]))) : printf(
|
||||
"%03i:\t%s\n", i, c);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,778 +0,0 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Integer gcd(Integer, Integer)
|
||||
//
|
||||
_gcd:
|
||||
asf 1
|
||||
jmp __2
|
||||
__1:
|
||||
pushl -4
|
||||
pushl -3
|
||||
mod
|
||||
popl 0
|
||||
pushl -3
|
||||
popl -4
|
||||
pushl 0
|
||||
popl -3
|
||||
__2:
|
||||
pushl -3
|
||||
pushc 0
|
||||
ne
|
||||
brt __1
|
||||
__3:
|
||||
pushl -4
|
||||
popr
|
||||
jmp __0
|
||||
__0:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } newFraction(Integer, Integer)
|
||||
//
|
||||
_newFraction:
|
||||
asf 4
|
||||
pushl -4
|
||||
pushc 0
|
||||
lt
|
||||
brf __5
|
||||
pushc 0
|
||||
pushl -4
|
||||
sub
|
||||
popl 0
|
||||
jmp __6
|
||||
__5:
|
||||
pushl -4
|
||||
popl 0
|
||||
__6:
|
||||
pushl -3
|
||||
pushc 0
|
||||
lt
|
||||
brf __7
|
||||
pushc 0
|
||||
pushl -3
|
||||
sub
|
||||
popl 1
|
||||
jmp __8
|
||||
__7:
|
||||
pushl -3
|
||||
popl 1
|
||||
__8:
|
||||
pushl 0
|
||||
pushl 1
|
||||
call _gcd
|
||||
drop 2
|
||||
pushr
|
||||
popl 2
|
||||
new 2
|
||||
popl 3
|
||||
pushl -4
|
||||
pushc 0
|
||||
lt
|
||||
pushl -3
|
||||
pushc 0
|
||||
lt
|
||||
ne
|
||||
brf __9
|
||||
pushl 3
|
||||
pushc 0
|
||||
pushl 0
|
||||
sub
|
||||
pushl 2
|
||||
div
|
||||
putf 0
|
||||
jmp __10
|
||||
__9:
|
||||
pushl 3
|
||||
pushl 0
|
||||
pushl 2
|
||||
div
|
||||
putf 0
|
||||
__10:
|
||||
pushl 3
|
||||
pushl 1
|
||||
pushl 2
|
||||
div
|
||||
putf 1
|
||||
pushl 3
|
||||
popr
|
||||
jmp __4
|
||||
__4:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeFraction(record { Integer num; Integer den; })
|
||||
//
|
||||
_writeFraction:
|
||||
asf 0
|
||||
pushl -3
|
||||
getf 0
|
||||
call _writeInteger
|
||||
drop 1
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 47
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl -3
|
||||
getf 1
|
||||
call _writeInteger
|
||||
drop 1
|
||||
__11:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } negFraction(record { Integer num; Integer den; })
|
||||
//
|
||||
_negFraction:
|
||||
asf 0
|
||||
pushc 0
|
||||
pushl -3
|
||||
getf 0
|
||||
sub
|
||||
pushl -3
|
||||
getf 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __12
|
||||
__12:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } addFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_addFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -3
|
||||
getf 0
|
||||
pushl -4
|
||||
getf 1
|
||||
mul
|
||||
add
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __13
|
||||
__13:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } subFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_subFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -3
|
||||
getf 0
|
||||
pushl -4
|
||||
getf 1
|
||||
mul
|
||||
sub
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __14
|
||||
__14:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } mulFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_mulFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 0
|
||||
mul
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __15
|
||||
__15:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// record { Integer num; Integer den; } divFraction(record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_divFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 0
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __16
|
||||
__16:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Fraction[][] newMatrix(record { Integer num; Integer den; }, record { Integer num; Integer den; }, record { Integer num; Integer den; }, record { Integer num; Integer den; })
|
||||
//
|
||||
_newMatrix:
|
||||
asf 1
|
||||
pushc 2
|
||||
newa
|
||||
popl 0
|
||||
pushl 0
|
||||
pushc 0
|
||||
pushc 2
|
||||
newa
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
pushc 2
|
||||
newa
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
pushl -6
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
pushl -5
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
pushl -4
|
||||
putfa
|
||||
pushl 0
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
pushl -3
|
||||
putfa
|
||||
pushl 0
|
||||
popr
|
||||
jmp __17
|
||||
__17:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void writeMatrix(Fraction[][])
|
||||
//
|
||||
_writeMatrix:
|
||||
asf 2
|
||||
pushc 0
|
||||
popl 0
|
||||
jmp __20
|
||||
__19:
|
||||
pushc 0
|
||||
popl 1
|
||||
jmp __23
|
||||
__22:
|
||||
pushl -3
|
||||
pushl 0
|
||||
getfa
|
||||
pushl 1
|
||||
getfa
|
||||
call _writeFraction
|
||||
drop 1
|
||||
pushc 2
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 1
|
||||
pushc 32
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl 1
|
||||
pushc 1
|
||||
add
|
||||
popl 1
|
||||
__23:
|
||||
pushl 1
|
||||
pushl -3
|
||||
pushl 0
|
||||
getfa
|
||||
getsz
|
||||
lt
|
||||
brt __22
|
||||
__24:
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushl 0
|
||||
pushc 1
|
||||
add
|
||||
popl 0
|
||||
__20:
|
||||
pushl 0
|
||||
pushl -3
|
||||
getsz
|
||||
lt
|
||||
brt __19
|
||||
__21:
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
__18:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// Fraction[][] invertMatrix(Fraction[][])
|
||||
//
|
||||
_invertMatrix:
|
||||
asf 1
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
call _mulFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
call _mulFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _subFraction
|
||||
drop 2
|
||||
pushr
|
||||
popl 0
|
||||
pushl 0
|
||||
getf 0
|
||||
pushc 0
|
||||
eq
|
||||
brf __26
|
||||
pushc 33
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 1
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 2
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 3
|
||||
pushc 111
|
||||
putfa
|
||||
dup
|
||||
pushc 4
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 5
|
||||
pushc 58
|
||||
putfa
|
||||
dup
|
||||
pushc 6
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 7
|
||||
pushc 109
|
||||
putfa
|
||||
dup
|
||||
pushc 8
|
||||
pushc 97
|
||||
putfa
|
||||
dup
|
||||
pushc 9
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 10
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 11
|
||||
pushc 105
|
||||
putfa
|
||||
dup
|
||||
pushc 12
|
||||
pushc 120
|
||||
putfa
|
||||
dup
|
||||
pushc 13
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 14
|
||||
pushc 99
|
||||
putfa
|
||||
dup
|
||||
pushc 15
|
||||
pushc 97
|
||||
putfa
|
||||
dup
|
||||
pushc 16
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 17
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 18
|
||||
pushc 111
|
||||
putfa
|
||||
dup
|
||||
pushc 19
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 20
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 21
|
||||
pushc 98
|
||||
putfa
|
||||
dup
|
||||
pushc 22
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 23
|
||||
pushc 32
|
||||
putfa
|
||||
dup
|
||||
pushc 24
|
||||
pushc 105
|
||||
putfa
|
||||
dup
|
||||
pushc 25
|
||||
pushc 110
|
||||
putfa
|
||||
dup
|
||||
pushc 26
|
||||
pushc 118
|
||||
putfa
|
||||
dup
|
||||
pushc 27
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 28
|
||||
pushc 114
|
||||
putfa
|
||||
dup
|
||||
pushc 29
|
||||
pushc 116
|
||||
putfa
|
||||
dup
|
||||
pushc 30
|
||||
pushc 101
|
||||
putfa
|
||||
dup
|
||||
pushc 31
|
||||
pushc 100
|
||||
putfa
|
||||
dup
|
||||
pushc 32
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
call _exit
|
||||
__26:
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 1
|
||||
getfa
|
||||
call _negFraction
|
||||
drop 1
|
||||
pushr
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 1
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
call _negFraction
|
||||
drop 1
|
||||
pushr
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushl -3
|
||||
pushc 0
|
||||
getfa
|
||||
pushc 0
|
||||
getfa
|
||||
pushl 0
|
||||
call _divFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _newMatrix
|
||||
drop 4
|
||||
pushr
|
||||
popr
|
||||
jmp __25
|
||||
__25:
|
||||
rsf
|
||||
ret
|
||||
|
||||
//
|
||||
// void main()
|
||||
//
|
||||
_main:
|
||||
asf 3
|
||||
pushc 1
|
||||
newa
|
||||
dup
|
||||
pushc 0
|
||||
pushc 10
|
||||
putfa
|
||||
call _writeString
|
||||
drop 1
|
||||
pushc 7
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 4
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 6
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
pushc 5
|
||||
pushc 1
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
call _newMatrix
|
||||
drop 4
|
||||
pushr
|
||||
popl 0
|
||||
pushl 0
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
pushl 0
|
||||
call _invertMatrix
|
||||
drop 1
|
||||
pushr
|
||||
popl 1
|
||||
pushl 1
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
pushl 1
|
||||
call _invertMatrix
|
||||
drop 1
|
||||
pushr
|
||||
popl 2
|
||||
pushl 2
|
||||
call _writeMatrix
|
||||
drop 1
|
||||
__27:
|
||||
rsf
|
||||
ret
|
||||
@@ -1,146 +0,0 @@
|
||||
//
|
||||
// matinv.nj -- invert 2x2 matrices of fractions
|
||||
//
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// greatest common divisor
|
||||
|
||||
Integer gcd(Integer a, Integer b) {
|
||||
local Integer h;
|
||||
while (b != 0) {
|
||||
h = a % b;
|
||||
a = b;
|
||||
b = h;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// fractions
|
||||
|
||||
type Fraction = record {
|
||||
Integer num;
|
||||
Integer den;
|
||||
};
|
||||
|
||||
Fraction newFraction(Integer num, Integer den) {
|
||||
local Integer n;
|
||||
local Integer d;
|
||||
local Integer g;
|
||||
local Fraction r;
|
||||
if (num < 0) {
|
||||
n = -num;
|
||||
} else {
|
||||
n = num;
|
||||
}
|
||||
if (den < 0) {
|
||||
d = -den;
|
||||
} else {
|
||||
d = den;
|
||||
}
|
||||
g = gcd(n, d);
|
||||
r = new(Fraction);
|
||||
if ((num < 0) != (den < 0)) {
|
||||
r.num = -n / g;
|
||||
} else {
|
||||
r.num = n / g;
|
||||
}
|
||||
r.den = d / g;
|
||||
return r;
|
||||
}
|
||||
|
||||
void writeFraction(Fraction f) {
|
||||
writeInteger(f.num);
|
||||
writeString("/");
|
||||
writeInteger(f.den);
|
||||
}
|
||||
|
||||
Fraction negFraction(Fraction f) {
|
||||
return newFraction(-f.num, f.den);
|
||||
}
|
||||
|
||||
Fraction addFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction subFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den - f2.num * f1.den, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction mulFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.num, f1.den * f2.den);
|
||||
}
|
||||
|
||||
Fraction divFraction(Fraction f1, Fraction f2) {
|
||||
return newFraction(f1.num * f2.den, f1.den * f2.num);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// 2x2 matrices of fractions
|
||||
|
||||
type Matrix = Fraction[][];
|
||||
|
||||
Matrix newMatrix(Fraction a00, Fraction a01,
|
||||
Fraction a10, Fraction a11) {
|
||||
local Matrix m;
|
||||
m = new(Fraction[2][]);
|
||||
m[0] = new(Fraction[2]);
|
||||
m[1] = new(Fraction[2]);
|
||||
m[0][0] = a00;
|
||||
m[0][1] = a01;
|
||||
m[1][0] = a10;
|
||||
m[1][1] = a11;
|
||||
return m;
|
||||
}
|
||||
|
||||
void writeMatrix(Matrix m) {
|
||||
local Integer i;
|
||||
local Integer j;
|
||||
i = 0;
|
||||
while (i < sizeof(m)) {
|
||||
j = 0;
|
||||
while (j < sizeof(m[i])) {
|
||||
writeFraction(m[i][j]);
|
||||
writeString(" ");
|
||||
j = j + 1;
|
||||
}
|
||||
writeString("\n");
|
||||
i = i + 1;
|
||||
}
|
||||
writeString("\n");
|
||||
}
|
||||
|
||||
Matrix invertMatrix(Matrix m) {
|
||||
local Fraction det;
|
||||
det = subFraction(mulFraction(m[0][0], m[1][1]),
|
||||
mulFraction(m[0][1], m[1][0]));
|
||||
if (det.num == 0) {
|
||||
writeString("error: matrix cannot be inverted\n");
|
||||
exit();
|
||||
}
|
||||
return newMatrix(
|
||||
divFraction(m[1][1], det), divFraction(negFraction(m[0][1]), det),
|
||||
divFraction(negFraction(m[1][0]), det), divFraction(m[0][0], det)
|
||||
);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
void main() {
|
||||
local Matrix matrix;
|
||||
local Matrix result1;
|
||||
local Matrix result2;
|
||||
writeString("\n");
|
||||
matrix = newMatrix(
|
||||
newFraction(7, 1), newFraction(4, 1),
|
||||
newFraction(6, 1), newFraction(5, 1)
|
||||
);
|
||||
writeMatrix(matrix);
|
||||
result1 = invertMatrix(matrix);
|
||||
writeMatrix(result1);
|
||||
result2 = invertMatrix(result1);
|
||||
writeMatrix(result2);
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
_subFraction:
|
||||
asf 0
|
||||
pushl -4
|
||||
getf 0
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
pushl -3
|
||||
getf 0
|
||||
pushl -4
|
||||
getf 1
|
||||
mul
|
||||
sub
|
||||
pushl -4
|
||||
getf 1
|
||||
pushl -3
|
||||
getf 1
|
||||
mul
|
||||
call _newFraction
|
||||
drop 2
|
||||
pushr
|
||||
popr
|
||||
jmp __14
|
||||
__14:
|
||||
rsf
|
||||
ret
|
||||
BIN
programs/nja
BIN
programs/nja
Binary file not shown.
@@ -1,15 +1,11 @@
|
||||
pushc 3
|
||||
pushc 4
|
||||
eq
|
||||
wrint
|
||||
add
|
||||
pushc 10
|
||||
wrchr
|
||||
|
||||
pushc 6
|
||||
pushc 6
|
||||
eq
|
||||
sub
|
||||
mul
|
||||
wrint
|
||||
pushc 10
|
||||
wrchr
|
||||
halt
|
||||
|
||||
BIN
programs/prog-test-1.bin
Normal file
BIN
programs/prog-test-1.bin
Normal file
Binary file not shown.
9
programs/prog-test-2.asm
Normal file
9
programs/prog-test-2.asm
Normal file
@@ -0,0 +1,9 @@
|
||||
pushc -2
|
||||
rdint
|
||||
mul
|
||||
pushc 3
|
||||
add
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
halt
|
||||
BIN
programs/prog-test-2.bin
Normal file
BIN
programs/prog-test-2.bin
Normal file
Binary file not shown.
5
programs/prog-test-3.asm
Normal file
5
programs/prog-test-3.asm
Normal file
@@ -0,0 +1,5 @@
|
||||
rdchr
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
halt
|
||||
BIN
programs/prog-test-3.bin
Normal file
BIN
programs/prog-test-3.bin
Normal file
Binary file not shown.
@@ -1,28 +0,0 @@
|
||||
//
|
||||
// prog02.asm -- call/ret with args, but without ret value
|
||||
//
|
||||
|
||||
asf 3
|
||||
pushc 11
|
||||
pushc 33
|
||||
call proc
|
||||
drop 2
|
||||
rsf
|
||||
halt
|
||||
|
||||
proc:
|
||||
asf 2
|
||||
pushl -4
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushc 22
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushl -3
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
@@ -1,32 +0,0 @@
|
||||
//
|
||||
// prog04.asm -- call/ret with args, and with ret value
|
||||
//
|
||||
|
||||
asf 3
|
||||
pushc 11
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushc 11
|
||||
pushc 33
|
||||
call proc
|
||||
drop 2
|
||||
pushr
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushc 33
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
halt
|
||||
|
||||
proc:
|
||||
asf 2
|
||||
pushl -3
|
||||
pushl -4
|
||||
sub
|
||||
popr
|
||||
rsf
|
||||
ret
|
||||
@@ -1,31 +0,0 @@
|
||||
//
|
||||
// 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
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,9 +1,61 @@
|
||||
pushc 1
|
||||
asf 2
|
||||
asf 4
|
||||
pushc 23
|
||||
asf 3
|
||||
rsf
|
||||
rsf
|
||||
rsf
|
||||
//
|
||||
// prog1.asm -- an assembler example with global variables
|
||||
//
|
||||
|
||||
//
|
||||
// compute the gcd of two positive numbers
|
||||
//
|
||||
// global Integer x;
|
||||
// global Integer y;
|
||||
// x = readInteger();
|
||||
// y = readInteger();
|
||||
// while (x != y) {
|
||||
// if (x > y) {
|
||||
// x = x - y;
|
||||
// } else {
|
||||
// y = y - x;
|
||||
// }
|
||||
// }
|
||||
// writeInteger(x);
|
||||
// writeCharacter('\n');
|
||||
|
||||
// x = readInteger();
|
||||
rdint
|
||||
popg 0
|
||||
// y = readInteger();
|
||||
rdint
|
||||
popg 1
|
||||
// while ...
|
||||
L1:
|
||||
// x != y
|
||||
pushg 0 // 4
|
||||
pushg 1
|
||||
ne
|
||||
brf L2
|
||||
// if ...
|
||||
pushg 0
|
||||
pushg 1
|
||||
gt
|
||||
brf L3
|
||||
// x = x - y
|
||||
pushg 0
|
||||
pushg 1
|
||||
sub
|
||||
popg 0
|
||||
jmp L4
|
||||
L3:
|
||||
// y = y - x
|
||||
pushg 1 // 17
|
||||
pushg 0
|
||||
sub
|
||||
popg 1
|
||||
L4:
|
||||
jmp L1 // 21
|
||||
L2:
|
||||
// writeInteger(x);
|
||||
pushg 0 // 22
|
||||
wrint
|
||||
// writeCharacter('\n');
|
||||
pushc '\n'
|
||||
wrchr
|
||||
halt
|
||||
|
||||
Binary file not shown.
@@ -1,12 +1,12 @@
|
||||
//
|
||||
// prog1.asm -- an assembler example with global variables
|
||||
// prog2.asm -- an assembler example with local variables
|
||||
//
|
||||
|
||||
//
|
||||
// compute the gcd of two positive numbers
|
||||
//
|
||||
// global Integer x;
|
||||
// global Integer y;
|
||||
// local Integer x;
|
||||
// local Integer y;
|
||||
// x = readInteger();
|
||||
// y = readInteger();
|
||||
// while (x != y) {
|
||||
@@ -19,43 +19,45 @@
|
||||
// writeInteger(x);
|
||||
// writeCharacter('\n');
|
||||
|
||||
asf 2
|
||||
// x = readInteger();
|
||||
rdint
|
||||
popg 0
|
||||
popl 0
|
||||
// y = readInteger();
|
||||
rdint
|
||||
popg 1
|
||||
popl 1
|
||||
// while ...
|
||||
L1:
|
||||
// x != y
|
||||
pushg 0
|
||||
pushg 1
|
||||
pushl 0
|
||||
pushl 1
|
||||
ne
|
||||
brf L2
|
||||
// if ...
|
||||
pushg 0
|
||||
pushg 1
|
||||
pushl 0
|
||||
pushl 1
|
||||
gt
|
||||
brf L3
|
||||
// x = x - y
|
||||
pushg 0
|
||||
pushg 1
|
||||
pushl 0
|
||||
pushl 1
|
||||
sub
|
||||
popg 0
|
||||
popl 0
|
||||
jmp L4
|
||||
L3:
|
||||
// y = y - x
|
||||
pushg 1
|
||||
pushg 0
|
||||
pushl 1
|
||||
pushl 0
|
||||
sub
|
||||
popg 1
|
||||
popl 1
|
||||
L4:
|
||||
jmp L1
|
||||
L2:
|
||||
// writeInteger(x);
|
||||
pushg 0
|
||||
pushl 0
|
||||
wrint
|
||||
// writeCharacter('\n');
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
halt
|
||||
|
||||
Binary file not shown.
@@ -1,63 +1,35 @@
|
||||
//
|
||||
// prog2.asm -- an assembler example with local variables
|
||||
// prog01.asm -- call/ret without args, and without ret value
|
||||
//
|
||||
|
||||
//
|
||||
// compute the gcd of two positive numbers
|
||||
//
|
||||
// local Integer x;
|
||||
// local Integer y;
|
||||
// x = readInteger();
|
||||
// y = readInteger();
|
||||
// while (x != y) {
|
||||
// if (x > y) {
|
||||
// x = x - y;
|
||||
// } else {
|
||||
// y = y - x;
|
||||
// }
|
||||
// }
|
||||
// writeInteger(x);
|
||||
// writeCharacter('\n');
|
||||
|
||||
asf 2
|
||||
// x = readInteger();
|
||||
rdint
|
||||
popl 0
|
||||
// y = readInteger();
|
||||
rdint
|
||||
popl 1
|
||||
// while ...
|
||||
L1:
|
||||
// x != y
|
||||
pushl 0
|
||||
pushl 1
|
||||
ne
|
||||
brf L2
|
||||
// if ...
|
||||
pushl 0
|
||||
pushl 1
|
||||
gt
|
||||
brf L3
|
||||
// x = x - y
|
||||
pushl 0
|
||||
pushl 1
|
||||
sub
|
||||
popl 0
|
||||
jmp L4
|
||||
L3:
|
||||
// y = y - x
|
||||
pushl 1
|
||||
pushl 0
|
||||
sub
|
||||
popl 1
|
||||
L4:
|
||||
jmp L1
|
||||
L2:
|
||||
// writeInteger(x);
|
||||
pushl 0
|
||||
asf 3
|
||||
pushc 11
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
call proc
|
||||
pushc 44
|
||||
wrint
|
||||
// writeCharacter('\n');
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
halt
|
||||
|
||||
proc:
|
||||
asf 2
|
||||
pushc 22
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
call proctwo
|
||||
rsf
|
||||
ret
|
||||
|
||||
proctwo:
|
||||
asf 2
|
||||
pushc 33
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
|
||||
Binary file not shown.
@@ -1,61 +1,28 @@
|
||||
//
|
||||
// prog1.asm -- an assembler example with global variables
|
||||
// prog02.asm -- call/ret with args, but without ret value
|
||||
//
|
||||
|
||||
//
|
||||
// compute the gcd of two positive numbers
|
||||
//
|
||||
// global Integer x;
|
||||
// global Integer y;
|
||||
// x = readInteger();
|
||||
// y = readInteger();
|
||||
// while (x != y) {
|
||||
// if (x > y) {
|
||||
// x = x - y;
|
||||
// } else {
|
||||
// y = y - x;
|
||||
// }
|
||||
// }
|
||||
// writeInteger(x);
|
||||
// writeCharacter('\n');
|
||||
asf 3
|
||||
pushc 11
|
||||
pushc 33
|
||||
call proc
|
||||
drop 2
|
||||
rsf
|
||||
halt
|
||||
|
||||
// x = readInteger();
|
||||
rdint
|
||||
popg 0
|
||||
// y = readInteger();
|
||||
rdint
|
||||
popg 1
|
||||
// while ...
|
||||
L1:
|
||||
// x != y
|
||||
pushg 0
|
||||
pushg 1
|
||||
ne
|
||||
brf L2
|
||||
// if ...
|
||||
pushg 0
|
||||
pushg 1
|
||||
gt
|
||||
brf L3
|
||||
// x = x - y
|
||||
pushg 0
|
||||
pushg 1
|
||||
sub
|
||||
popg 0
|
||||
jmp L4
|
||||
L3:
|
||||
// y = y - x
|
||||
pushg 1
|
||||
pushg 0
|
||||
sub
|
||||
popg 1
|
||||
L4:
|
||||
jmp L1
|
||||
L2:
|
||||
// writeInteger(x);
|
||||
pushg 0
|
||||
proc:
|
||||
asf 2
|
||||
pushl -4
|
||||
wrint
|
||||
// writeCharacter('\n');
|
||||
pushc '\n'
|
||||
wrchr
|
||||
halt
|
||||
pushc 22
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushl -3
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
|
||||
@@ -1,16 +1,27 @@
|
||||
//
|
||||
// prog04.asm -- call/ret with args, and with ret value
|
||||
// prog02.asm -- call/ret with args, but without ret value
|
||||
//
|
||||
|
||||
pushc 12
|
||||
rdint
|
||||
mul
|
||||
wrint
|
||||
asf 3
|
||||
pushc 11
|
||||
pushc 33
|
||||
call proc
|
||||
drop 2
|
||||
rsf
|
||||
halt
|
||||
|
||||
proc:
|
||||
asf 1
|
||||
asf 2
|
||||
pushl 4
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushc 22
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushl 3
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
|
||||
Binary file not shown.
56
record.c
56
record.c
@@ -1,56 +0,0 @@
|
||||
//
|
||||
// Created by Nils Polek on 23.01.24.
|
||||
//
|
||||
#ifndef RECORD
|
||||
#define RECORD
|
||||
#include "stackslot.c"
|
||||
#include "instruktion.c"
|
||||
#include "GC.c"
|
||||
ObjRef newRecord(int size){
|
||||
ObjRef record;
|
||||
unsigned int objSize;
|
||||
objSize = sizeof(*record) + (size * sizeof(void *));
|
||||
if((record = malloc(objSize)) == NULL){
|
||||
perror("malloc");
|
||||
}
|
||||
record->size = MSB;
|
||||
record->size = record->size + size;
|
||||
for(int i = 0; i < size; i++) {
|
||||
GET_REFS_PTR(record)[i] = NULL;
|
||||
}
|
||||
return record;
|
||||
}
|
||||
int getSize(ObjRef arr){
|
||||
if(arr == NULL) return 0;
|
||||
return GET_ELEMENT_COUNT(arr);
|
||||
}
|
||||
ObjRef getField(ObjRef arr, int point){
|
||||
if(arr == NULL) perror("Record is null");
|
||||
if(0 > point || point > getSize(arr)){
|
||||
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
||||
}
|
||||
return *(ObjRef *)GET_REFS_PTR(arr)[point]->data;
|
||||
}
|
||||
void setField(ObjRef arr, int point, ObjRef value){
|
||||
bool isNull = false;
|
||||
if(value == NULL) isNull = true;
|
||||
if(0 > point || point >= getSize(arr)){
|
||||
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(arr == NULL) perror("Record is null");
|
||||
if(IS_PRIMITIVE(arr)) perror("Record is a primitive");
|
||||
int size;
|
||||
if (isNull) size = sizeof(void *);
|
||||
else {
|
||||
if (IS_PRIMITIVE(value))
|
||||
size = value->size;
|
||||
else
|
||||
size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *));
|
||||
}
|
||||
if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc");
|
||||
GET_REFS_PTR(arr)[point] ->size = size;
|
||||
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
|
||||
}
|
||||
|
||||
#endif
|
||||
87
stack.c
87
stack.c
@@ -3,79 +3,46 @@
|
||||
//
|
||||
#ifndef STACK
|
||||
#define STACK
|
||||
#define SIZE 1000
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "stackslot.c"
|
||||
|
||||
struct stack {
|
||||
int* size;
|
||||
int* current;
|
||||
StackSlot *stack;
|
||||
struct stackFrame {
|
||||
int *fp; // Frame pointer
|
||||
int *sp; // Stack pointer
|
||||
int *bp; // Base pointer
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
unsigned int size;
|
||||
ObjRef *refs;
|
||||
}ObjRefContainer;
|
||||
struct stack {
|
||||
int size;
|
||||
int currentFrame;
|
||||
struct stackFrame *frames[SIZE];
|
||||
};
|
||||
|
||||
void printStack(struct stack stack, int fp) {
|
||||
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
||||
for (int i = *stack.current -1; i >= 0; --i) {
|
||||
printf("%i\t",i);
|
||||
if(stack.stack[i].u.objRef == NULL) printf("|NULL|");
|
||||
else
|
||||
if(stack.stack[i].isObjRef){
|
||||
printf("|%p|", (void *)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");
|
||||
void printStack(struct stack *stack) {
|
||||
printf("Stack:\n");
|
||||
for (int i = 0; i < stack->size; ++i) {
|
||||
printf("[%d] = %d\n", i, stack->frames[stack->currentFrame]->sp[i]);
|
||||
}
|
||||
}
|
||||
|
||||
//ObjRefContainer getRefs(struct stack stack){
|
||||
// ObjRefContainer continer;
|
||||
// int counter = 0;
|
||||
// for (int i = 0; i <= *stack.current; i++) {
|
||||
// if(stack.stack[i].isObjRef == true) counter++;
|
||||
// }
|
||||
// continer.size = counter;
|
||||
// ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef));
|
||||
// for (int i = 0; i<= *stack.current; i++)
|
||||
// if(stack.stack[i].isObjRef == true)
|
||||
// list[counter--] = stack.stack[i].u.objRef;
|
||||
// continer.refs = list;
|
||||
// return continer;
|
||||
//}
|
||||
|
||||
void push(struct stack s, StackSlot value) {
|
||||
if (*s.current >= *s.size) {
|
||||
printf("Stack Overflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
s.stack[*s.current] = value;
|
||||
*s.current=*s.current + 1;
|
||||
void push(struct stack *stack, unsigned int value) {
|
||||
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
||||
*(currentFrame->sp) = value;
|
||||
currentFrame->sp++;
|
||||
}
|
||||
|
||||
StackSlot pop(struct stack s) {
|
||||
if (*s.current == 0) {
|
||||
printf("Stack Underflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*s.current = *s.current -1;
|
||||
return s.stack[*s.current];
|
||||
int pop(struct stack *stack) {
|
||||
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
||||
currentFrame->sp--;
|
||||
return *(currentFrame->sp);
|
||||
}
|
||||
|
||||
int peek(struct stack s, int steps) {
|
||||
if (*s.current - steps < 0) {
|
||||
printf("Stack Underflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return getIntValfromStackSlot(s.stack[*s.current - steps]);
|
||||
int peek(struct stack *stack, int steps) { // peek is pop without removing the value
|
||||
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
||||
return *(currentFrame->sp - steps);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
66
stackslot.c
66
stackslot.c
@@ -1,66 +0,0 @@
|
||||
#ifndef STACKSLOT
|
||||
#define STACKSLOT
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "objref.c"
|
||||
|
||||
typedef int Object;
|
||||
|
||||
|
||||
typedef struct {
|
||||
bool isObjRef;
|
||||
union {
|
||||
ObjRef objRef;
|
||||
int number;
|
||||
} u;
|
||||
} StackSlot;
|
||||
|
||||
ObjRef getIntObj(int val) {
|
||||
ObjRef intObject;
|
||||
unsigned int objSize = sizeof(ObjRef) + sizeof(int);
|
||||
if ((intObject = malloc(objSize)) == NULL) {
|
||||
perror("malloc");
|
||||
}
|
||||
*(int *) intObject->data = val;
|
||||
intObject->size = objSize;
|
||||
return intObject;
|
||||
}
|
||||
|
||||
int getValFromIntObj(ObjRef iref) {
|
||||
if (iref == NULL) perror("ObjRef is null");
|
||||
return *(int *) iref->data;
|
||||
}
|
||||
|
||||
int getIntValfromStackSlot(StackSlot s) {
|
||||
if (s.isObjRef) {
|
||||
return *(int *) s.u.objRef->data;
|
||||
}
|
||||
return s.u.number;
|
||||
}
|
||||
|
||||
void setValIntObj(ObjRef iref, int val) {
|
||||
if (iref == NULL) perror("ObjRef is null");
|
||||
iref->size = sizeof(int)+sizeof(ObjRef);
|
||||
*(int *) iref->data = val;
|
||||
}
|
||||
|
||||
StackSlot stackSlotWithObjRef(ObjRef val) {
|
||||
StackSlot *stackSlot;
|
||||
stackSlot = malloc(sizeof(StackSlot));
|
||||
if(stackSlot == NULL) perror("malloc");
|
||||
stackSlot->isObjRef = true;
|
||||
stackSlot->u.objRef = val;
|
||||
return *stackSlot;
|
||||
}
|
||||
|
||||
StackSlot stackSlotWitchNumber(int val) {
|
||||
StackSlot *stackSlot;
|
||||
stackSlot = malloc(sizeof(StackSlot));
|
||||
if(stackSlot == NULL) perror("malloc");
|
||||
stackSlot->isObjRef = false;
|
||||
stackSlot->u.number = val;
|
||||
return *stackSlot;
|
||||
}
|
||||
|
||||
#endif
|
||||
28
support.c
28
support.c
@@ -1,28 +0,0 @@
|
||||
|
||||
#include "support.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "objref.c"
|
||||
#include "GC.c"
|
||||
void fatalError(char *msg){
|
||||
printf("Fatal error: %s\n", msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void * newPrimObject(int dataSize) {
|
||||
ObjRef bigObjRef;
|
||||
|
||||
bigObjRef = malloc(sizeof(unsigned int) +
|
||||
dataSize * sizeof(unsigned char));
|
||||
if (bigObjRef == NULL) {
|
||||
fatalError("newPrimObject() got no memory");
|
||||
}
|
||||
bigObjRef->size = sizeof(unsigned int) + dataSize * sizeof(unsigned char);
|
||||
return bigObjRef;
|
||||
}
|
||||
|
||||
void * getPrimObjectDataPointer(void * obj){
|
||||
ObjRef oo = ((ObjRef) (obj));
|
||||
return oo->data;
|
||||
}
|
||||
155
test.asm
155
test.asm
@@ -1,155 +0,0 @@
|
||||
//
|
||||
// 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
|
||||
BIN
test/.DS_Store
vendored
BIN
test/.DS_Store
vendored
Binary file not shown.
BIN
test/tests/1.bin
BIN
test/tests/1.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
test/tests/2.bin
BIN
test/tests/2.bin
Binary file not shown.
BIN
test/tests/3.bin
BIN
test/tests/3.bin
Binary file not shown.
BIN
test/tests/4.bin
BIN
test/tests/4.bin
Binary file not shown.
BIN
test/tests/5.bin
BIN
test/tests/5.bin
Binary file not shown.
BIN
test/tests/6.bin
BIN
test/tests/6.bin
Binary file not shown.
BIN
test/tests/7.bin
BIN
test/tests/7.bin
Binary file not shown.
BIN
test/tests/8.bin
BIN
test/tests/8.bin
Binary file not shown.
BIN
test/tests/9.bin
BIN
test/tests/9.bin
Binary file not shown.
@@ -1,18 +0,0 @@
|
||||
new 3
|
||||
popg 0
|
||||
|
||||
pushg 0
|
||||
pushc 10
|
||||
putf 0
|
||||
pushg 0
|
||||
pushc 11
|
||||
putf 1
|
||||
|
||||
pushg 0
|
||||
getf 0
|
||||
pushg 0
|
||||
getf 1
|
||||
add
|
||||
wrint
|
||||
halt
|
||||
// Sollte 10 + 11 ergeben
|
||||
@@ -1,13 +0,0 @@
|
||||
asf 5
|
||||
pushc 11
|
||||
call x
|
||||
wrint
|
||||
rsf
|
||||
halt
|
||||
|
||||
x:
|
||||
asf 3
|
||||
pushc 22
|
||||
wrint
|
||||
rsf
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
//
|
||||
// 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 0
|
||||
pushc 4421
|
||||
pushc 5743
|
||||
mul
|
||||
pushc 7699
|
||||
mul
|
||||
call _writeInteger
|
||||
drop 1
|
||||
__0:
|
||||
rsf
|
||||
ret
|
||||
@@ -1,5 +0,0 @@
|
||||
pushc 5
|
||||
pushc 4
|
||||
add
|
||||
wrint
|
||||
halt
|
||||
@@ -1,5 +0,0 @@
|
||||
pushc 5
|
||||
pushc 4
|
||||
div
|
||||
wrint
|
||||
halt
|
||||
@@ -1,5 +0,0 @@
|
||||
pushc 5
|
||||
pushc 4
|
||||
mod
|
||||
wrint
|
||||
halt
|
||||
@@ -1,5 +0,0 @@
|
||||
pushc 5
|
||||
pushc 4
|
||||
mul
|
||||
wrint
|
||||
halt
|
||||
@@ -1,5 +0,0 @@
|
||||
pushc 5
|
||||
pushc 4
|
||||
sub
|
||||
wrint
|
||||
halt
|
||||
@@ -1,16 +0,0 @@
|
||||
pushc 3
|
||||
pushc 4
|
||||
eq
|
||||
brf L1
|
||||
|
||||
pushc 4
|
||||
|
||||
L1:
|
||||
pushc 7
|
||||
|
||||
wrint
|
||||
pushc 10
|
||||
wrchr
|
||||
|
||||
halt
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
pushc 4
|
||||
pushc 4
|
||||
eq
|
||||
brf L1
|
||||
|
||||
pushc 6
|
||||
wrint
|
||||
pushc 10
|
||||
wrchr
|
||||
halt
|
||||
|
||||
L1:
|
||||
pushc 7
|
||||
wrint
|
||||
pushc 10
|
||||
wrchr
|
||||
halt
|
||||
@@ -1,19 +0,0 @@
|
||||
pushc 3
|
||||
pushc 4
|
||||
eq
|
||||
brt L1
|
||||
|
||||
pushc 5
|
||||
wrint
|
||||
pushc 10
|
||||
wrchr
|
||||
halt
|
||||
|
||||
L1:
|
||||
pushc 7
|
||||
|
||||
wrint
|
||||
pushc 10
|
||||
wrchr
|
||||
halt
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user