//running: soll der interpreter noch laufen puplic static boolean running = true; //commandcounter: aktueller befehl puplic static int commandcounter = 1; //iterations: wieviele durchlaeufe gab es bisher public static int iterations = 1; /*--------------------------------------------------------------------------------------------------------------------------------------------*/ //--------------------------speicher für die befehle class Commands { int cnumb; int cadr; Commands next; boolean tail; static Commands tailcom; //--------------------------hole den xten befehl von hier aus Commands getcommand(int listnumber) { if (this.tail == true) { if (listnumber != 1) { System.out.println("error: command number too high"); } return this; }//endif else { if (listnumber == 1) { return this; } else{next.getcommand(listnumber-1); } }//endelse }//endgetcommand //--------------------------schreibe befehl an das ende der liste void putcommand(int number, int adress) { Commands newborne = new Commands; newborne.cnumb = number; newborne.cadr = adress; newborne.tail = true; tailcom.tail = false; tailcom.next = newborne; tailcom = newborne; }//endputcommand }//endCommands /*--------------------------------------------------------------------------------------------------------------------------------------------*/ //--------------------------speicher für die daten class Datis { int valu; Datis next; boolean tail; static Datis taildat; //--------------------------hole das xte datum von hier aus Datis getdati(int listnumber) { if (this.tail == true) { if (listnumber != 1) { System.out.println("error: data number too high"); } return this; }//endif else { if (listnumber == 1) { return this; } else{next.getdati(listnumber-1); } }//endelse }//endgetdati //--------------------------schreibe datum an das ende der liste void putdati(int valuem) { Datis newborne = new Datis; newborne.valu = valuem; newborne.tail = true; taildat.tail = false; taildat.next = newborne; taildat = newborne; }//endputdati //--------------------------drucke alle daten von hier an void printdatis(int numbi) { if (tail == false){ System.out.println("Register",numbi,":"); System.out.println(valu); next.printdatis(numbi+1); }//endif }//endprintdati }//endDatis /*--------------------------------------------------------------------------------------------------------------------------------------------*/ //--------------------------fuehre aktuellen befehl aus void executecommand(Commands com, Datis dat) { Commands current = com.getcommand(commandcounter); int curtype = current.cnumb; //inc if (curtype == 0) { System.out.println("command nr. ",commandcounter,": increase variable nr. ",current.cadr); Datis curdati = dat.getdati(current.cadr); System.out.println(curdati.valu," + 1 = ",curdati.valu + 1); curdati.valu = curdati.valu + 1; } //dec if (curtype == 1) { System.out.println("command nr. ",commandcounter,": decrease variable nr. ",current.cadr); Datis curdati = dat.getdati(current.cadr); curdati.valu = curdati.valu - 1; if (curdati.valu < 0){ curdati.valu = 0;} System.out.println(curdati.valu+1," - 1 = ",curdati.valu); } //jmp if (curtype == 2) { System.out.println("command nr. ",commandcounter,": jump to command nr. ",current.cadr); commandcounter = current.cadr; } //tst if (curtype == 3) { System.out.println("command nr. ",commandcounter,": test variable nr. ",current.cadr); Datis curdati = dat.getdati(current.cadr); if (curdati.valu == 0){ commandcounter++; System.out.println("value = 0, test suceeded"); } else { System.out.println("value = ",curdati.valu",", test failed"); } //hlt if (curtype == 4) { System.out.println("command nr. ",commandcounter,": halt"); running = false; } //naechster befehl bzw uebernaechster wenn tst gelungen commandcounter++; }//endexecute /*--------------------------------------------------------------------------------------------------------------------------------------------*/ //char = 0-9? boolean charint(char testme){ if(testme == "0"){return true;} if(testme == "1"){return true;} if(testme == "2"){return true;} if(testme == "3"){return true;} if(testme == "4"){return true;} if(testme == "5"){return true;} if(testme == "6"){return true;} if(testme == "7"){return true;} if(testme == "8"){return true;} if(testme == "9"){return true;} } //char->int integer intme(char dome){ if(testme == "0"){return 0;} if(testme == "1"){return 1;} if(testme == "2"){return 2;} if(testme == "3"){return 3;} if(testme == "4"){return 4;} if(testme == "5"){return 5;} if(testme == "6"){return 6;} if(testme == "7"){return 7;} if(testme == "8"){return 8;} if(testme == "9"){return 9;} return 0; } /*--------------------------------------------------------------------------------------------------------------------------------------------*/ //verwandle einen array of chars in eine befehlsliste void chartoclist(char[] coms, Commands liste){ int leng = coms.length; int position = 0; int ctype; int cadr; while (position < leng) { if (coms[position] == "I"){ ctype = 0; position += 4; }//endif elseif (coms[position] == "D"){ ctype = 1; position += 4; }//endif elseif (coms[position] == "J"){ ctype = 2; position += 4; }//endif elseif (coms[position] == "T"){ ctype = 3; position += 4; }//endif elseif (coms[position] == "H"){ ctype = 4; position += 3; }//endif cadr = 0; while (charint(coms[position])){ cadr *= 10; cadr += intme(coms[position]); position++; }//endwhile adressor liste.putcommand(ctype,cadr); }//endwhile iterator }//endchartoclist /*--------------------------------------------------------------------------------------------------------------------------------------------*/ //verwandle einen array of chars in eine speicherliste void chartodlist(char[] coms, Datis liste){ int leng = coms.length; int position = 0; int cadr; while (position < leng) { cadr = 0; while (charint(coms[position])){ cadr *= 10; cadr += intme(coms[position]); position++; }//endwhile adressor liste.putdatis(cadr); }//endwhile iterator }//endchartodlist /*--------------------------------------------------------------------------------------------------------------------------------------------*/ class Main { //erstelle leere listen Commands cliste = new Commands; cliste.tail = true; cliste.tailcom = cliste; Datis dliste = new Datis; dliste.tail = true; dliste.taildat = dliste; //beispielalgorithmus fuers malrechnen cliste.putcommand(3,1); cliste.putcommand(2,4); cliste.putcommand(2,27); cliste.putcommand(1,1); cliste.putcommand(0,6); cliste.putcommand(3,2); cliste.putcommand(2,9); cliste.putcommand(2,12); cliste.putcommand(1,2); cliste.putcommand(0,3); cliste.putcommand(2,6); cliste.putcommand(0,6); cliste.putcommand(3,3); cliste.putcommand(2,16); cliste.putcommand(2,20); cliste.putcommand(1,3); cliste.putcommand(0,2); cliste.putcommand(0,4); cliste.putcommand(2,13); cliste.putcommand(0,6); cliste.putcommand(3,4); cliste.putcommand(2,24); cliste.putcommand(2,1); cliste.putcommand(0,5); cliste.putcommand(1,4); cliste.putcommand(2,21); cliste.putcommand(4,0); //5 mal 4 als daten dliste.putdatis(5); dliste.putdatis(4); dliste.putdatis(0); dliste.putdatis(0); dliste.putdatis(0); dliste.putdatis(0); while (running) { System.out.println("iteration Nr. ",iterations); executecommand(cliste,dliste); iterations++; } System.out.println("algorithm terminated"); dliste.printdatis(1); }