Compare commits
40 Commits
084b715729
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 32346f8a28 | |||
| 240e93a90f | |||
| bc41dcda4a | |||
| f62030772a | |||
| 43663714ca | |||
| 38bfd5c7d9 | |||
|
|
f348174229 | ||
|
|
bc8a037a30 | ||
|
|
958c383f85 | ||
|
|
f3829559d4 | ||
|
|
9155216fd2 | ||
|
|
b6320ecba7 | ||
|
|
3bd8b9ac20 | ||
|
|
147415beef | ||
|
|
5fb42ff984 | ||
|
|
d7bb53687d | ||
|
|
ee27de7481 | ||
|
|
eaab6ab023 | ||
|
|
38f06b1575 | ||
|
|
6ebc065a7a | ||
|
|
8c45b02769 | ||
|
|
4cc117fdd4 | ||
|
|
49e4911238 | ||
|
|
21c1f8fee6 | ||
|
|
30311a393d | ||
|
|
daacbc705d | ||
|
|
b77ea3d3d8 | ||
|
|
29dc364f94 | ||
|
|
fd15e54cdd | ||
|
|
460def3fc6 | ||
|
|
4cf1a82974 | ||
|
|
bc56a6a1c2 | ||
|
|
84320f00c7 | ||
|
|
8d667ec836 | ||
|
|
520cfa57d5 | ||
|
|
2df1debe1a | ||
|
|
9475dd6666 | ||
|
|
03b88a8a76 | ||
|
|
91b36a53d5 | ||
|
|
169217fd2c |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,5 +3,3 @@ cmake-build-debug
|
|||||||
njvm
|
njvm
|
||||||
njvm.dSYM
|
njvm.dSYM
|
||||||
njvm.exe
|
njvm.exe
|
||||||
njvm2
|
|
||||||
test
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ set(CMAKE_C_STANDARD 99)
|
|||||||
|
|
||||||
add_compile_options(-g -Wall -pedantic)
|
add_compile_options(-g -Wall -pedantic)
|
||||||
|
|
||||||
add_executable(njvm njvm.c)
|
add_executable(njvm njvm.c
|
||||||
|
njvm.h)
|
||||||
|
|
||||||
# Include directories for njvm executable
|
# Include directories for njvm executable
|
||||||
target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
||||||
@@ -13,7 +14,9 @@ target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build
|
|||||||
# Add the library
|
# Add the library
|
||||||
add_library(bigint STATIC ${CMAKE_SOURCE_DIR}/bigint/src/bigint.c
|
add_library(bigint STATIC ${CMAKE_SOURCE_DIR}/bigint/src/bigint.c
|
||||||
support.c
|
support.c
|
||||||
heap.c)
|
GC.c
|
||||||
|
heap.c
|
||||||
|
heap.h)
|
||||||
|
|
||||||
# Include directories for the bigint library
|
# Include directories for the bigint library
|
||||||
target_include_directories(bigint PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
target_include_directories(bigint PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
|
||||||
|
|||||||
99
GC.c
Normal file
99
GC.c
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
#ifndef GC
|
||||||
|
#define GC
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "stackslot.c"
|
||||||
|
#include "stack.c"
|
||||||
|
#include "bigint.h"
|
||||||
|
#include "instruktion.c"
|
||||||
|
#include "record.c"
|
||||||
|
#include "SDA.c"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned int freiZeiger;
|
||||||
|
unsigned int size;
|
||||||
|
unsigned char *data;
|
||||||
|
} *heapPartRef;
|
||||||
|
|
||||||
|
// SDA
|
||||||
|
struct sda sda;
|
||||||
|
|
||||||
|
// Stack
|
||||||
|
struct stack stack;
|
||||||
|
#define SIZE 1000;
|
||||||
|
|
||||||
|
//Register
|
||||||
|
struct stack reg;
|
||||||
|
|
||||||
|
|
||||||
|
//heapPartRef primary;
|
||||||
|
//heapPartRef secondary;
|
||||||
|
//unsigned int heapSize;
|
||||||
|
//void initHeap(int size){
|
||||||
|
// heapSize = 2 * sizeof (unsigned int) + size;
|
||||||
|
// if ((primary = malloc(heapSize)) == NULL) perror("malloc");
|
||||||
|
// if ((secondary = malloc(heapSize)) == NULL) perror("malloc");
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//// nimmt obj und copier es in den secondary Speicher
|
||||||
|
//void copy(ObjRef obj){
|
||||||
|
// if(obj->brokenHeart == true) return;
|
||||||
|
// obj->brokenHeart = true;
|
||||||
|
// if (IS_PRIMITIVE(obj)){
|
||||||
|
// if(obj->size > secondary->size-secondary->freiZeiger) perror("Heap is to Small");
|
||||||
|
// obj->forward_pointer = memcpy((void *) secondary->data[secondary->freiZeiger], obj, obj->size);
|
||||||
|
// secondary->freiZeiger += obj->size;
|
||||||
|
// } else {
|
||||||
|
// if(sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *)) > secondary->size-secondary->freiZeiger) perror("Heap is to Small");
|
||||||
|
// GET_ELEMENT_COUNT(obj);
|
||||||
|
// obj->forward_pointer = memcpy((void *) secondary->data[secondary->freiZeiger], obj, sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *)));
|
||||||
|
// secondary->freiZeiger += sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *));
|
||||||
|
// for (int i = 0; i < GET_ELEMENT_COUNT(obj); ++i) {
|
||||||
|
// copy(getField(obj,i));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//void runGC(){
|
||||||
|
// int rootSize = *sda.size + *reg.size + *stack.size + 4;
|
||||||
|
// ObjRef* rootObjs = malloc(sizeof(ObjRef) * rootSize);
|
||||||
|
// if(rootObjs == NULL) perror("malloc");
|
||||||
|
// int counter = 0;
|
||||||
|
// //Bip
|
||||||
|
// rootObjs[0] = bip.op1;
|
||||||
|
// rootObjs[counter++] = bip.op2;
|
||||||
|
// rootObjs[counter++] = bip.res;
|
||||||
|
// rootObjs[counter++] = bip.rem;
|
||||||
|
// //SDA
|
||||||
|
// for (int i = 0; i < *sda.size; ++i) {
|
||||||
|
// rootObjs[counter++] = sda.sda[i];
|
||||||
|
// }
|
||||||
|
// //REG
|
||||||
|
// for (int i = 0; i < *reg.size; ++i) {
|
||||||
|
// rootObjs[counter++] = reg.stack[i].u.objRef;
|
||||||
|
// }
|
||||||
|
// //STACK
|
||||||
|
// for (int i = 0; i < *stack.size; ++i) {
|
||||||
|
// rootObjs[counter++] = stack.stack[i].u.objRef;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// for (int i = 0; i < rootSize; ++i) {
|
||||||
|
// if(rootObjs[i] == NULL) continue;
|
||||||
|
// copy(rootObjs[i]);
|
||||||
|
// }
|
||||||
|
// heapPartRef temp = primary;
|
||||||
|
// primary = secondary;
|
||||||
|
// secondary = temp;
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//void *alloc(size_t size){
|
||||||
|
// if(primary->size-primary->freiZeiger < size){
|
||||||
|
// runGC();
|
||||||
|
// }
|
||||||
|
// primary->freiZeiger += size;
|
||||||
|
// return (void *) primary->data[primary->freiZeiger - size];
|
||||||
|
//}
|
||||||
|
#endif
|
||||||
1
SDA.c
1
SDA.c
@@ -15,6 +15,7 @@ ObjRef getSDA(int i, struct sda s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setSDA(int point, ObjRef val, struct sda s) {
|
void setSDA(int point, ObjRef val, struct sda s) {
|
||||||
|
if (val == NULL) perror("Value is null");
|
||||||
s.sda[point] = val;
|
s.sda[point] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -38,13 +38,13 @@ void fatalError(char *msg) {
|
|||||||
*/
|
*/
|
||||||
void * newPrimObject(int dataSize) {
|
void * newPrimObject(int dataSize) {
|
||||||
ObjRef bigObjRef;
|
ObjRef bigObjRef;
|
||||||
|
int size = sizeof(unsigned int) + dataSize * sizeof(unsigned char);
|
||||||
bigObjRef = malloc(sizeof(unsigned int) +
|
bigObjRef = malloc(size);
|
||||||
dataSize * sizeof(unsigned char));
|
bigObjRef->size = size;
|
||||||
if (bigObjRef == NULL) {
|
if (bigObjRef == NULL) {
|
||||||
fatalError("newPrimObject() got no memory");
|
fatalError("newPrimObject() got no memory");
|
||||||
}
|
}
|
||||||
bigObjRef->size = dataSize;
|
bigObjRef->size = size;
|
||||||
return bigObjRef;
|
return bigObjRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
2
consts.c
2
consts.c
@@ -1,6 +1,6 @@
|
|||||||
#ifndef CONSTS
|
#ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
#define VERSION 7
|
#define VERSION 8
|
||||||
|
|
||||||
#endif /* ifndef CONSTS
|
#endif /* ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
|
|||||||
12
debugMenu.c
12
debugMenu.c
@@ -18,7 +18,6 @@ void inspect(struct stack s, int fp){
|
|||||||
if (input[0] == 'o'){
|
if (input[0] == 'o'){
|
||||||
scanf("%19s",ref);
|
scanf("%19s",ref);
|
||||||
ObjRefContainer container;
|
ObjRefContainer container;
|
||||||
container = getRefs(s);
|
|
||||||
for (int i = 0; i<= container.size; i++) {
|
for (int i = 0; i<= container.size; i++) {
|
||||||
sprintf(refStr, "%p", (void *)&container.refs[i]);
|
sprintf(refStr, "%p", (void *)&container.refs[i]);
|
||||||
if(strcmp(ref, refStr) == 0) printf("Adress exists\n");
|
if(strcmp(ref, refStr) == 0) printf("Adress exists\n");
|
||||||
@@ -30,11 +29,14 @@ void inspect(struct stack s, int fp){
|
|||||||
void list(void){
|
void list(void){
|
||||||
//todo
|
//todo
|
||||||
}
|
}
|
||||||
void breakpoint(void){
|
void breakpoint(int *bp){
|
||||||
//todo
|
printf("BREAKPOINT: ");
|
||||||
|
char input[20];
|
||||||
|
fgets(input,20,stdin);
|
||||||
|
*bp = atoi(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
void debugMenu(int fp, struct stack stack, int* debug, int point){
|
void debugMenu(int fp, struct stack stack, int* debug, int point, int* bp){
|
||||||
char input[20];
|
char input[20];
|
||||||
while (true) {
|
while (true) {
|
||||||
printf("DEBUG(%i): inspect, list, breakpoint, run, step, quit?",point);
|
printf("DEBUG(%i): inspect, list, breakpoint, run, step, quit?",point);
|
||||||
@@ -42,7 +44,7 @@ void debugMenu(int fp, struct stack stack, int* debug, int point){
|
|||||||
printf("%s",input);
|
printf("%s",input);
|
||||||
if(input[0] == 'i') {inspect(stack,fp);}
|
if(input[0] == 'i') {inspect(stack,fp);}
|
||||||
if(input[0] == 'l') list();
|
if(input[0] == 'l') list();
|
||||||
if(input[0] == 'b') breakpoint();
|
if(input[0] == 'b') breakpoint(bp);
|
||||||
if(input[0] == 's') break;
|
if(input[0] == 's') break;
|
||||||
if(input[0] == 'r') {*debug = 0; break;};
|
if(input[0] == 'r') {*debug = 0; break;};
|
||||||
if(input[0] == 'q') exit(0);
|
if(input[0] == 'q') exit(0);
|
||||||
|
|||||||
8301
factor.asm
Normal file
8301
factor.asm
Normal file
File diff suppressed because it is too large
Load Diff
BIN
factor.bin
Normal file
BIN
factor.bin
Normal file
Binary file not shown.
794
factor.nj
Normal file
794
factor.nj
Normal file
@@ -0,0 +1,794 @@
|
|||||||
|
//
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
38
heap.c
38
heap.c
@@ -1,11 +1,33 @@
|
|||||||
//
|
//
|
||||||
// Created by Nils Polek on 23.01.24.
|
// Created by Elias Bennour on 28.01.24.
|
||||||
//
|
//
|
||||||
typedef struct {
|
|
||||||
int size;
|
|
||||||
int hcurrent;
|
|
||||||
}heap;
|
|
||||||
|
|
||||||
//void* alloc(int size){
|
#ifndef HEAP
|
||||||
// return {}
|
#define HEAP
|
||||||
//}
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int maxHeapSize = 8192 * 1024;
|
||||||
|
|
||||||
|
void* my_malloc(size_t size) {
|
||||||
|
static size_t total_allocated = 0;
|
||||||
|
|
||||||
|
if (total_allocated + size > maxHeapSize) {
|
||||||
|
perror("Memory limit exceeded\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* ptr = malloc(size);
|
||||||
|
if (ptr != NULL) {
|
||||||
|
total_allocated += size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void initHeap(int size) {
|
||||||
|
maxHeapSize = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //NINJA_NJVM_H
|
||||||
12
heap.h
Normal file
12
heap.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
//
|
||||||
|
// Created by Elias Bennour on 28.01.24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef NINJA_HEAP_H
|
||||||
|
#define NINJA_HEAP_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void* my_malloc(size_t size);
|
||||||
|
|
||||||
|
#endif //NINJA_HEAP_H
|
||||||
778
matinv.asm
Normal file
778
matinv.asm
Normal file
@@ -0,0 +1,778 @@
|
|||||||
|
//
|
||||||
|
// 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
Normal file
BIN
matinv.bin
Normal file
Binary file not shown.
146
matinv.nj
Normal file
146
matinv.nj
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
//
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
11070
njlisp.asm
Normal file
11070
njlisp.asm
Normal file
File diff suppressed because it is too large
Load Diff
BIN
njlisp.bin
Normal file
BIN
njlisp.bin
Normal file
Binary file not shown.
63
njvm.c
63
njvm.c
@@ -1,3 +1,6 @@
|
|||||||
|
#ifndef NJVM
|
||||||
|
#define NJVM
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -5,26 +8,18 @@
|
|||||||
#include "stack.c"
|
#include "stack.c"
|
||||||
#include "program.c"
|
#include "program.c"
|
||||||
#include "codeReader.c"
|
#include "codeReader.c"
|
||||||
#include "SDA.c"
|
|
||||||
#include "debugMenu.c"
|
#include "debugMenu.c"
|
||||||
#include "bigint.h"
|
#include "bigint.h"
|
||||||
#include "record.c"
|
#include "record.c"
|
||||||
|
#include "GC.c"
|
||||||
|
#include "heap.c"
|
||||||
|
|
||||||
// Debug
|
// Debug
|
||||||
int debug = 0;
|
int debug = 0;
|
||||||
|
|
||||||
// Stack
|
|
||||||
struct stack stack;
|
|
||||||
#define SIZE 64
|
|
||||||
|
|
||||||
//Register
|
|
||||||
struct stack reg;
|
|
||||||
|
|
||||||
// Program
|
// Program
|
||||||
struct program program;
|
struct program program;
|
||||||
|
|
||||||
// SDA
|
|
||||||
struct sda sda;
|
|
||||||
unsigned fp;
|
unsigned fp;
|
||||||
|
|
||||||
void version(void) {
|
void version(void) {
|
||||||
@@ -36,6 +31,7 @@ void help(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void execute(struct program program) {
|
void execute(struct program program) {
|
||||||
|
int bp = -1;
|
||||||
int i;
|
int i;
|
||||||
char charInput;
|
char charInput;
|
||||||
StackSlot tempSlot;
|
StackSlot tempSlot;
|
||||||
@@ -43,7 +39,8 @@ void execute(struct program program) {
|
|||||||
ObjRef tempObj2;
|
ObjRef tempObj2;
|
||||||
int tempInt;
|
int tempInt;
|
||||||
for (i = 0; i < *program.size; ++i) {
|
for (i = 0; i < *program.size; ++i) {
|
||||||
if (debug == 1) debugMenu(fp, stack, &debug, i);
|
if (debug == 1 || bp == i) debugMenu(fp, stack, &debug, i, &bp);
|
||||||
|
if(debug == 1) printf("(%i)",i);
|
||||||
switch (program.program[i] >> 24) {
|
switch (program.program[i] >> 24) {
|
||||||
case HALT:
|
case HALT:
|
||||||
if (debug == 1) printf("halt\n");
|
if (debug == 1) printf("halt\n");
|
||||||
@@ -237,42 +234,42 @@ void execute(struct program program) {
|
|||||||
push(stack, stackSlotWithObjRef(tempObj));
|
push(stack, stackSlotWithObjRef(tempObj));
|
||||||
break;
|
break;
|
||||||
case POPR:
|
case POPR:
|
||||||
if (debug == 1) printf("popr");
|
if (debug == 1) printf("popr\n");
|
||||||
push(reg, pop(stack));
|
push(reg, pop(stack));
|
||||||
break;
|
break;
|
||||||
case PUSHR:
|
case PUSHR:
|
||||||
if (debug == 1) printf("pushr");
|
if (debug == 1) printf("pushr\n");
|
||||||
push(stack, pop(reg));
|
push(stack, pop(reg));
|
||||||
break;
|
break;
|
||||||
case NEW:
|
case NEW:
|
||||||
if (debug == 1) printf("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])))));
|
push(stack, stackSlotWithObjRef(newRecord(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
||||||
break;
|
break;
|
||||||
case GETF:
|
case GETF:
|
||||||
if (debug == 1) printf("getf");
|
if (debug == 1) printf("getf\n");
|
||||||
tempObj = pop(stack).u.objRef;
|
tempObj = pop(stack).u.objRef;
|
||||||
push(stack, stackSlotWithObjRef(getField(tempObj,SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
push(stack, stackSlotWithObjRef(getField(tempObj,SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
||||||
break;
|
break;
|
||||||
case PUTF:
|
case PUTF:
|
||||||
if (debug == 1) printf("putf");
|
if (debug == 1) printf("putf\t%i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
tempObj = pop(stack).u.objRef;
|
tempObj = pop(stack).u.objRef;
|
||||||
tempObj2 = pop(stack).u.objRef;
|
tempObj2 = pop(stack).u.objRef;
|
||||||
setField(tempObj2, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj);
|
setField(tempObj2, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj);
|
||||||
break;
|
break;
|
||||||
case NEWA:
|
case NEWA:
|
||||||
if(debug == 1) printf("newa");
|
if(debug == 1) printf("newa\n");
|
||||||
bip.op1 = pop(stack).u.objRef;
|
bip.op1 = pop(stack).u.objRef;
|
||||||
push(stack, stackSlotWithObjRef(newRecord(bigToInt())));
|
push(stack, stackSlotWithObjRef(newRecord(bigToInt())));
|
||||||
break;
|
break;
|
||||||
case GETFA:
|
case GETFA:
|
||||||
if(debug == 1) printf("getfa");
|
if(debug == 1) printf("getfa\n");
|
||||||
bip.op1 = pop(stack).u.objRef;
|
bip.op1 = pop(stack).u.objRef;
|
||||||
tempInt = bigToInt();
|
tempInt = bigToInt();
|
||||||
tempObj = pop(stack).u.objRef;
|
tempObj = pop(stack).u.objRef;
|
||||||
push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt())));
|
push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt())));
|
||||||
break;
|
break;
|
||||||
case PUTFA:
|
case PUTFA:
|
||||||
if (debug == 1) printf("putfa");
|
if (debug == 1) printf("putfa\n");
|
||||||
tempObj = pop(stack).u.objRef; // Value
|
tempObj = pop(stack).u.objRef; // Value
|
||||||
tempObj2 = pop(stack).u.objRef; // Index
|
tempObj2 = pop(stack).u.objRef; // Index
|
||||||
bip.op1 = tempObj2;
|
bip.op1 = tempObj2;
|
||||||
@@ -280,24 +277,24 @@ void execute(struct program program) {
|
|||||||
setField(pop(stack).u.objRef, tempInt,tempObj);
|
setField(pop(stack).u.objRef, tempInt,tempObj);
|
||||||
break;
|
break;
|
||||||
case GETSZ:
|
case GETSZ:
|
||||||
if (debug == 1) printf("getsz");
|
if (debug == 1) printf("getsz\n");
|
||||||
tempObj = pop(stack).u.objRef;
|
tempObj = pop(stack).u.objRef;
|
||||||
if(IS_PRIMITIVE(tempObj)) bigFromInt(-1);
|
if(IS_PRIMITIVE(tempObj)) bigFromInt(-1);
|
||||||
else bigFromInt(GET_ELEMENT_COUNT(tempObj));
|
else bigFromInt(GET_ELEMENT_COUNT(tempObj));
|
||||||
push(stack, stackSlotWithObjRef(bip.res));
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
break;
|
break;
|
||||||
case PUSHN:
|
case PUSHN:
|
||||||
if (debug == 1) printf("pushn");
|
if (debug == 1) printf("pushn\n");
|
||||||
push(stack, stackSlotWithObjRef(NULL));
|
push(stack, stackSlotWithObjRef(NULL));
|
||||||
break;
|
break;
|
||||||
case REFEQ:
|
case REFEQ:
|
||||||
if (debug == 1) printf("refeq");
|
if (debug == 1) printf("refeq\n");
|
||||||
if(pop(stack).u.objRef == pop(stack).u.objRef) bigFromInt(true);
|
if(pop(stack).u.objRef == pop(stack).u.objRef) bigFromInt(true);
|
||||||
else bigFromInt(false);
|
else bigFromInt(false);
|
||||||
push(stack, stackSlotWithObjRef(bip.res));
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
break;
|
break;
|
||||||
case REFNE:
|
case REFNE:
|
||||||
if (debug == 1) printf("refeq");
|
if (debug == 1) printf("refeq\n");
|
||||||
if(pop(stack).u.objRef != pop(stack).u.objRef) bigFromInt(true);
|
if(pop(stack).u.objRef != pop(stack).u.objRef) bigFromInt(true);
|
||||||
else bigFromInt(false);
|
else bigFromInt(false);
|
||||||
push(stack, stackSlotWithObjRef(bip.res));
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
@@ -312,27 +309,28 @@ void tests(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
|
||||||
// Initialize the Stack
|
// Initialize the Stack
|
||||||
int size = SIZE;
|
int size = SIZE;
|
||||||
int current = 0;
|
int current = 0;
|
||||||
stack.size = &size;
|
stack.size = &size;
|
||||||
stack.current = ¤t;
|
stack.current = ¤t;
|
||||||
stack.stack = malloc(SIZE * 10024);
|
stack.stack = malloc(size * 1024);
|
||||||
|
if (stack.stack == NULL) {
|
||||||
|
perror("malloc");
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the registery
|
// Initialize the registery
|
||||||
int rSize = 1000;
|
int rSize = 100000;
|
||||||
int rCurrent = 0;
|
int rCurrent = 0;
|
||||||
StackSlot r[1000];
|
StackSlot r[100000];
|
||||||
reg.size = &rSize;
|
reg.size = &rSize;
|
||||||
reg.current = &rCurrent;
|
reg.current = &rCurrent;
|
||||||
reg.stack = r;
|
reg.stack = r;
|
||||||
|
|
||||||
// Initialize ProgrammSpeicher
|
// Initialize ProgrammSpeicher
|
||||||
int psize = 1000;
|
int psize = 100000;
|
||||||
int saveProgram = 0;
|
int saveProgram = 0;
|
||||||
unsigned int p[1000];
|
unsigned int p[100000];
|
||||||
program.size = &psize;
|
program.size = &psize;
|
||||||
program.program = p;
|
program.program = p;
|
||||||
program.saveProgram = &saveProgram;
|
program.saveProgram = &saveProgram;
|
||||||
@@ -355,9 +353,8 @@ int main(int argc, char *argv[]) {
|
|||||||
// TODO: implement stack size
|
// TODO: implement stack size
|
||||||
} else if (strcmp(argv[i], "--heap") == 0) {
|
} else if (strcmp(argv[i], "--heap") == 0) {
|
||||||
i++;
|
i++;
|
||||||
// TODO: implement heap size
|
initHeap(atoi(argv[i]) * 1024);
|
||||||
} else if (strcmp(argv[i], "--gcpurge") == 0) {
|
} else if (strcmp(argv[i], "--gcpurge") == 0) {
|
||||||
i++;
|
|
||||||
// TODO: implement gcpurge
|
// TODO: implement gcpurge
|
||||||
} else {
|
} else {
|
||||||
sizeSDA = fromFile(argv[i], program);
|
sizeSDA = fromFile(argv[i], program);
|
||||||
@@ -383,3 +380,5 @@ int main(int argc, char *argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif /* ifndef NJVM */
|
||||||
8
objref.c
8
objref.c
@@ -1,9 +1,11 @@
|
|||||||
#ifndef OBJREF
|
#ifndef OBJREF
|
||||||
#define OBJREF
|
#define OBJREF
|
||||||
|
|
||||||
typedef struct {
|
#include <stdbool.h>
|
||||||
unsigned int size;
|
|
||||||
unsigned char data[1];
|
typedef struct ObjRef{
|
||||||
|
unsigned int size;
|
||||||
|
unsigned char data[1];
|
||||||
} *ObjRef;
|
} *ObjRef;
|
||||||
|
|
||||||
#endif /* ifndef OBJREF
|
#endif /* ifndef OBJREF
|
||||||
|
|||||||
778
programs/matrix.asm
Normal file
778
programs/matrix.asm
Normal file
@@ -0,0 +1,778 @@
|
|||||||
|
//
|
||||||
|
// 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
|
||||||
24
record.c
24
record.c
@@ -9,10 +9,10 @@ ObjRef newRecord(int size){
|
|||||||
ObjRef record;
|
ObjRef record;
|
||||||
unsigned int objSize;
|
unsigned int objSize;
|
||||||
objSize = sizeof(*record) + (size * sizeof(void *));
|
objSize = sizeof(*record) + (size * sizeof(void *));
|
||||||
if((record = malloc(objSize)) == NULL){
|
if((record = my_malloc(objSize)) == NULL){
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
}
|
}
|
||||||
record->size = (1 << ((sizeof(int) * 8) - 1));
|
record->size = MSB;
|
||||||
record->size = record->size + size;
|
record->size = record->size + size;
|
||||||
for(int i = 0; i < size; i++) {
|
for(int i = 0; i < size; i++) {
|
||||||
GET_REFS_PTR(record)[i] = NULL;
|
GET_REFS_PTR(record)[i] = NULL;
|
||||||
@@ -20,19 +20,35 @@ ObjRef newRecord(int size){
|
|||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
int getSize(ObjRef arr){
|
int getSize(ObjRef arr){
|
||||||
|
if(arr == NULL) return 0;
|
||||||
return GET_ELEMENT_COUNT(arr);
|
return GET_ELEMENT_COUNT(arr);
|
||||||
}
|
}
|
||||||
ObjRef getField(ObjRef arr, int point){
|
ObjRef getField(ObjRef arr, int point){
|
||||||
|
if(arr == NULL) perror("Record is null");
|
||||||
if(0 > point || point > getSize(arr)){
|
if(0 > point || point > getSize(arr)){
|
||||||
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
||||||
}
|
}
|
||||||
return *(ObjRef *)GET_REFS_PTR(arr)[point]->data;
|
return *(ObjRef *)GET_REFS_PTR(arr)[point]->data;
|
||||||
}
|
}
|
||||||
void setField(ObjRef arr, int point, ObjRef value){
|
void setField(ObjRef arr, int point, ObjRef value){
|
||||||
if(0 > point || point > getSize(arr)){
|
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));
|
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
GET_REFS_PTR(arr)[point] = malloc(8);
|
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] = my_malloc(size)) == NULL) perror("malloc");
|
||||||
|
GET_REFS_PTR(arr)[point] ->size = size;
|
||||||
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
|
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
30
stack.c
30
stack.c
@@ -23,6 +23,8 @@ void printStack(struct stack stack, int fp) {
|
|||||||
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
||||||
for (int i = *stack.current -1; i >= 0; --i) {
|
for (int i = *stack.current -1; i >= 0; --i) {
|
||||||
printf("%i\t",i);
|
printf("%i\t",i);
|
||||||
|
if(stack.stack[i].u.objRef == NULL) printf("|NULL|");
|
||||||
|
else
|
||||||
if(stack.stack[i].isObjRef){
|
if(stack.stack[i].isObjRef){
|
||||||
printf("|%p|", (void *)stack.stack[i].u.objRef);
|
printf("|%p|", (void *)stack.stack[i].u.objRef);
|
||||||
if(stack.stack[i].u.objRef->size == sizeof(int))
|
if(stack.stack[i].u.objRef->size == sizeof(int))
|
||||||
@@ -36,20 +38,20 @@ void printStack(struct stack stack, int fp) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjRefContainer getRefs(struct stack stack){
|
//ObjRefContainer getRefs(struct stack stack){
|
||||||
ObjRefContainer continer;
|
// ObjRefContainer continer;
|
||||||
int counter = 0;
|
// int counter = 0;
|
||||||
for (int i = 0; i <= *stack.current; i++) {
|
// for (int i = 0; i <= *stack.current; i++) {
|
||||||
if(stack.stack[i].isObjRef == true) counter++;
|
// if(stack.stack[i].isObjRef == true) counter++;
|
||||||
}
|
// }
|
||||||
continer.size = counter;
|
// continer.size = counter;
|
||||||
ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef));
|
// ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef));
|
||||||
for (int i = 0; i<= *stack.current; i++)
|
// for (int i = 0; i<= *stack.current; i++)
|
||||||
if(stack.stack[i].isObjRef == true)
|
// if(stack.stack[i].isObjRef == true)
|
||||||
list[counter--] = stack.stack[i].u.objRef;
|
// list[counter--] = stack.stack[i].u.objRef;
|
||||||
continer.refs = list;
|
// continer.refs = list;
|
||||||
return continer;
|
// return continer;
|
||||||
}
|
//}
|
||||||
|
|
||||||
void push(struct stack s, StackSlot value) {
|
void push(struct stack s, StackSlot value) {
|
||||||
if (*s.current >= *s.size) {
|
if (*s.current >= *s.size) {
|
||||||
|
|||||||
98
stackslot.c
98
stackslot.c
@@ -1,57 +1,67 @@
|
|||||||
|
#ifndef STACKSLOT
|
||||||
|
#define STACKSLOT
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#ifndef STACKSLOT
|
#include "objref.c"
|
||||||
#define STACKSLOT
|
#include "heap.h"
|
||||||
typedef int Object;
|
|
||||||
typedef struct {
|
|
||||||
unsigned int size;
|
|
||||||
unsigned char data[1];
|
|
||||||
} *ObjRef;
|
|
||||||
|
|
||||||
typedef struct{
|
typedef int Object;
|
||||||
bool isObjRef;
|
|
||||||
union {
|
|
||||||
ObjRef objRef;
|
typedef struct {
|
||||||
int number;
|
bool isObjRef;
|
||||||
} u;
|
union {
|
||||||
|
ObjRef objRef;
|
||||||
|
int number;
|
||||||
|
} u;
|
||||||
} StackSlot;
|
} StackSlot;
|
||||||
|
|
||||||
ObjRef getIntObj(int val){
|
ObjRef getIntObj(int val) {
|
||||||
ObjRef intObject;
|
ObjRef intObject;
|
||||||
unsigned int objSize = sizeof(unsigned int) + sizeof(int);
|
unsigned int objSize = sizeof(ObjRef) + sizeof(int);
|
||||||
if ((intObject = malloc(objSize)) == NULL) {
|
if ((intObject = my_malloc(objSize)) == NULL) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
}
|
}
|
||||||
*(int *)intObject->data=val;
|
*(int *) intObject->data = val;
|
||||||
intObject->size=sizeof(int);
|
intObject->size = objSize;
|
||||||
return intObject;
|
return intObject;
|
||||||
}
|
}
|
||||||
int getValFromIntObj(ObjRef iref){
|
|
||||||
return *(int *)iref->data;
|
int getValFromIntObj(ObjRef iref) {
|
||||||
|
if (iref == NULL) perror("ObjRef is null");
|
||||||
|
return *(int *) iref->data;
|
||||||
}
|
}
|
||||||
int getIntValfromStackSlot(StackSlot s){
|
|
||||||
if(s.isObjRef){
|
int getIntValfromStackSlot(StackSlot s) {
|
||||||
return *(int *)s.u.objRef->data;
|
if (s.isObjRef) {
|
||||||
}
|
return *(int *) s.u.objRef->data;
|
||||||
return s.u.number;
|
}
|
||||||
|
return s.u.number;
|
||||||
}
|
}
|
||||||
void setValIntObj(ObjRef iref, int val){
|
|
||||||
iref->size=sizeof(int);
|
void setValIntObj(ObjRef iref, int val) {
|
||||||
*(int *)iref->data=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 stackSlotWithObjRef(ObjRef val) {
|
||||||
stackSlot=malloc(sizeof(StackSlot));
|
StackSlot *stackSlot;
|
||||||
stackSlot->isObjRef=true;
|
stackSlot = my_malloc(sizeof(StackSlot));
|
||||||
stackSlot->u.objRef=val;
|
if(stackSlot == NULL) perror("malloc");
|
||||||
return *stackSlot;
|
stackSlot->isObjRef = true;
|
||||||
|
stackSlot->u.objRef = val;
|
||||||
|
return *stackSlot;
|
||||||
}
|
}
|
||||||
StackSlot stackSlotWitchNumber(int val){
|
|
||||||
StackSlot *stackSlot;
|
StackSlot stackSlotWitchNumber(int val) {
|
||||||
stackSlot= malloc(sizeof(StackSlot));
|
StackSlot *stackSlot;
|
||||||
stackSlot->isObjRef=false;
|
stackSlot = my_malloc(sizeof(StackSlot));
|
||||||
stackSlot->u.number=val;
|
if(stackSlot == NULL) perror("malloc");
|
||||||
return *stackSlot;
|
stackSlot->isObjRef = false;
|
||||||
|
stackSlot->u.number = val;
|
||||||
|
return *stackSlot;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "objref.c"
|
#include "objref.c"
|
||||||
|
#include "heap.h"
|
||||||
|
|
||||||
void fatalError(char *msg){
|
void fatalError(char *msg){
|
||||||
printf("Fatal error: %s\n", msg);
|
printf("Fatal error: %s\n", msg);
|
||||||
@@ -12,12 +13,12 @@ void fatalError(char *msg){
|
|||||||
void * newPrimObject(int dataSize) {
|
void * newPrimObject(int dataSize) {
|
||||||
ObjRef bigObjRef;
|
ObjRef bigObjRef;
|
||||||
|
|
||||||
bigObjRef = malloc(sizeof(unsigned int) +
|
bigObjRef = my_malloc(sizeof(unsigned int) +
|
||||||
dataSize * sizeof(unsigned char));
|
dataSize * sizeof(unsigned char));
|
||||||
if (bigObjRef == NULL) {
|
if (bigObjRef == NULL) {
|
||||||
fatalError("newPrimObject() got no memory");
|
fatalError("newPrimObject() got no memory");
|
||||||
}
|
}
|
||||||
bigObjRef->size = dataSize;
|
bigObjRef->size = sizeof(unsigned int) + dataSize * sizeof(unsigned char);
|
||||||
return bigObjRef;
|
return bigObjRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
BIN
test/.DS_Store
vendored
Normal file
BIN
test/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
test/tests/1.bin
Normal file
BIN
test/tests/1.bin
Normal file
Binary file not shown.
BIN
test/tests/10.bin
Normal file
BIN
test/tests/10.bin
Normal file
Binary file not shown.
BIN
test/tests/11.bin
Normal file
BIN
test/tests/11.bin
Normal file
Binary file not shown.
BIN
test/tests/12.bin
Normal file
BIN
test/tests/12.bin
Normal file
Binary file not shown.
BIN
test/tests/13.bin
Normal file
BIN
test/tests/13.bin
Normal file
Binary file not shown.
BIN
test/tests/14.bin
Normal file
BIN
test/tests/14.bin
Normal file
Binary file not shown.
BIN
test/tests/15.bin
Normal file
BIN
test/tests/15.bin
Normal file
Binary file not shown.
BIN
test/tests/2.bin
Normal file
BIN
test/tests/2.bin
Normal file
Binary file not shown.
BIN
test/tests/3.bin
Normal file
BIN
test/tests/3.bin
Normal file
Binary file not shown.
BIN
test/tests/4.bin
Normal file
BIN
test/tests/4.bin
Normal file
Binary file not shown.
BIN
test/tests/5.bin
Normal file
BIN
test/tests/5.bin
Normal file
Binary file not shown.
BIN
test/tests/6.bin
Normal file
BIN
test/tests/6.bin
Normal file
Binary file not shown.
BIN
test/tests/7.bin
Normal file
BIN
test/tests/7.bin
Normal file
Binary file not shown.
BIN
test/tests/8.bin
Normal file
BIN
test/tests/8.bin
Normal file
Binary file not shown.
BIN
test/tests/9.bin
Normal file
BIN
test/tests/9.bin
Normal file
Binary file not shown.
18
test/tests/arrTest.asm
Normal file
18
test/tests/arrTest.asm
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
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
|
||||||
13
test/tests/asf_rsfTest.asm
Normal file
13
test/tests/asf_rsfTest.asm
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
asf 5
|
||||||
|
pushc 11
|
||||||
|
call x
|
||||||
|
wrint
|
||||||
|
rsf
|
||||||
|
halt
|
||||||
|
|
||||||
|
x:
|
||||||
|
asf 3
|
||||||
|
pushc 22
|
||||||
|
wrint
|
||||||
|
rsf
|
||||||
|
|
||||||
125
test/tests/bigMultiplication.asm
Normal file
125
test/tests/bigMultiplication.asm
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
//
|
||||||
|
// 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
|
||||||
5
test/tests/bigint/bigIntTestAdd.asm
Normal file
5
test/tests/bigint/bigIntTestAdd.asm
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pushc 5
|
||||||
|
pushc 4
|
||||||
|
add
|
||||||
|
wrint
|
||||||
|
halt
|
||||||
5
test/tests/bigint/bigIntTestDiv.asm
Normal file
5
test/tests/bigint/bigIntTestDiv.asm
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pushc 5
|
||||||
|
pushc 4
|
||||||
|
div
|
||||||
|
wrint
|
||||||
|
halt
|
||||||
5
test/tests/bigint/bigIntTestMod.asm
Normal file
5
test/tests/bigint/bigIntTestMod.asm
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pushc 5
|
||||||
|
pushc 4
|
||||||
|
mod
|
||||||
|
wrint
|
||||||
|
halt
|
||||||
5
test/tests/bigint/bigIntTestMul.asm
Normal file
5
test/tests/bigint/bigIntTestMul.asm
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pushc 5
|
||||||
|
pushc 4
|
||||||
|
mul
|
||||||
|
wrint
|
||||||
|
halt
|
||||||
5
test/tests/bigint/bigIntTestSub.asm
Normal file
5
test/tests/bigint/bigIntTestSub.asm
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pushc 5
|
||||||
|
pushc 4
|
||||||
|
sub
|
||||||
|
wrint
|
||||||
|
halt
|
||||||
16
test/tests/brfTest1.asm
Normal file
16
test/tests/brfTest1.asm
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
eq
|
||||||
|
brf L1
|
||||||
|
|
||||||
|
pushc 4
|
||||||
|
|
||||||
|
L1:
|
||||||
|
pushc 7
|
||||||
|
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
halt
|
||||||
|
|
||||||
17
test/tests/brfTest2.asm
Normal file
17
test/tests/brfTest2.asm
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
pushc 4
|
||||||
|
pushc 4
|
||||||
|
eq
|
||||||
|
brf L1
|
||||||
|
|
||||||
|
pushc 6
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
|
L1:
|
||||||
|
pushc 7
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
19
test/tests/brtTest1.asm
Normal file
19
test/tests/brtTest1.asm
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
eq
|
||||||
|
brt L1
|
||||||
|
|
||||||
|
pushc 5
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
|
L1:
|
||||||
|
pushc 7
|
||||||
|
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
17
test/tests/brtTest2.asm
Normal file
17
test/tests/brtTest2.asm
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
pushc 4
|
||||||
|
pushc 4
|
||||||
|
eq
|
||||||
|
brt L1
|
||||||
|
|
||||||
|
pushc 6
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
|
L1:
|
||||||
|
pushc 7
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
15
test/tests/equalsTest.asm
Normal file
15
test/tests/equalsTest.asm
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
eq
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
pushc 6
|
||||||
|
pushc 6
|
||||||
|
eq
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
24
test/tests/greaterEqualsTest.asm
Normal file
24
test/tests/greaterEqualsTest.asm
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
ge
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 8
|
||||||
|
pushc 4
|
||||||
|
ge
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 6
|
||||||
|
pushc 6
|
||||||
|
ge
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
24
test/tests/greaterThanTest.asm
Normal file
24
test/tests/greaterThanTest.asm
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
gt
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 8
|
||||||
|
pushc 4
|
||||||
|
gt
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 6
|
||||||
|
pushc 6
|
||||||
|
gt
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
19
test/tests/jumpTest.asm
Normal file
19
test/tests/jumpTest.asm
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 3
|
||||||
|
jmp L1
|
||||||
|
|
||||||
|
pushc 4
|
||||||
|
|
||||||
|
L1:
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
halt
|
||||||
|
|
||||||
24
test/tests/lowerEqualsTest.asm
Normal file
24
test/tests/lowerEqualsTest.asm
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
le
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 8
|
||||||
|
pushc 4
|
||||||
|
le
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 6
|
||||||
|
pushc 6
|
||||||
|
le
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
24
test/tests/lowerThanTest.asm
Normal file
24
test/tests/lowerThanTest.asm
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
lt
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 8
|
||||||
|
pushc 4
|
||||||
|
lt
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
|
||||||
|
pushc 6
|
||||||
|
pushc 6
|
||||||
|
lt
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
BIN
test/tests/nja
Executable file
BIN
test/tests/nja
Executable file
Binary file not shown.
15
test/tests/notEqualsTest.asm
Normal file
15
test/tests/notEqualsTest.asm
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
ne
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
pushc 6
|
||||||
|
pushc 6
|
||||||
|
ne
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
|
|
||||||
23
test/tests/objTest.asm
Normal file
23
test/tests/objTest.asm
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
pushc 3
|
||||||
|
newa
|
||||||
|
popg 0
|
||||||
|
|
||||||
|
pushg 0
|
||||||
|
pushc 0
|
||||||
|
pushc 9
|
||||||
|
putfa
|
||||||
|
pushg 0
|
||||||
|
pushc 1
|
||||||
|
pushc 22
|
||||||
|
putfa
|
||||||
|
|
||||||
|
pushg 0
|
||||||
|
pushc 0
|
||||||
|
getfa
|
||||||
|
pushg 0
|
||||||
|
pushc 1
|
||||||
|
getfa
|
||||||
|
add
|
||||||
|
wrint
|
||||||
|
halt
|
||||||
|
// Sollte 21 + 9 ergeben
|
||||||
16
test/tests/prog1.asm
Normal file
16
test/tests/prog1.asm
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
pushc 3
|
||||||
|
pushc 4
|
||||||
|
eq
|
||||||
|
brf L1
|
||||||
|
|
||||||
|
pushc 4
|
||||||
|
|
||||||
|
L1:
|
||||||
|
pushc 7
|
||||||
|
|
||||||
|
wrint
|
||||||
|
pushc 10
|
||||||
|
wrchr
|
||||||
|
|
||||||
|
halt
|
||||||
|
|
||||||
Reference in New Issue
Block a user