[英]Java Compiler - Load Method
So I have been working on a java project where the goal is to create a virtual computer. 因此,我一直在致力于创建虚拟计算机的Java项目中。 So I am basically done but with one problem. 所以我基本上完成了,但是有一个问题。 I have created a compiler which translates a txt document with assembly code in it and my compiler has created a new-file with this code written as machine executable ints. 我已经创建了一个编译器,该编译器可以翻译其中带有汇编代码的txt文档,而我的编译器已经创建了一个新文件,并将此代码写为机器可执行int。 But now I need to write a load method that reads these ints and runs the program but I am having difficulty doing this. 但是现在我需要编写一个读取这些int并运行程序的加载方法,但是我很难做到这一点。 Any help is much appreciated....also this is not homework if you are thinking this. 非常感谢您的任何帮助。...如果您正在考虑这一点,这也不是家庭作业。 The project was simply to make a compiler and now I am trying to complete it for my own interest. 该项目只是为了制作一个编译器,现在我出于自己的利益尝试尝试完成它。 Thanks. 谢谢。
Here is what I have so far for load: 到目前为止,这是我的负载:
public void load(String newfile) throws FileNotFoundException
{
try{
File file = new File(newfile);
FileInputStream fs = new FileInputStream(file);
DataInputStream dos = new DataInputStream(fs);
dos.readInt();
dos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
Ok here is the part of the Compiler that does the writeInts: 好的,这是执行writeInts的编译器的一部分:
public void SecondPass(SymbolList symbolTable, String filename){
try {
int dc = 99;
//Open file for reading
File file = new File(filename);
Scanner scan = new Scanner(file);
//Make filename of new executable file
String newfile = makeFilename(filename);
//Open Output Stream for writing new file.
FileOutputStream fs = new FileOutputStream(newfile);
DataOutputStream dos = new DataOutputStream(fs);
//Read First line. Split line by Spaces into linearray.
String line = scan.nextLine();
String[] linearray = line.split(" ");
while(line!=null){
if(!linearray[0].equals("REM")){
int inst = 0, opcode, loc;
if(isInstruction(linearray[0])){
opcode = getOpcode(linearray[0]);
loc = symbolTable.searchName(linearray[1]).getMemloc();
inst = (opcode*100)+loc;
} else if(!isInstruction(linearray[0])){
if(isInstruction(linearray[1])){
opcode = getOpcode(linearray[1]);
if(linearray[1].equals("STOP"))
inst=0000;
else {
loc = symbolTable.searchName(linearray[2]).getMemloc();
inst = (opcode*100)+loc;
}
}
if(linearray[1].equals("DC"))
dc--;
}
dos.writeInt(inst);
System.out.println(" inst is being written as:" + inst);
}
try{
line = scan.nextLine();
}
catch(NoSuchElementException e){
line = null;
break;
}
linearray = line.split(" ");
}
scan.close();
for(int i=lc; i<=dc; i++){
dos.writeInt(0);
}
for(int i = dc+1; i < 100; i++)
{
dos.writeInt(symbolTable.searchLocation(i).getValue());
}
dos.close();
fs.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
So what I have done is write a file in txt like: 所以我要做的是在txt中写一个文件,例如:
IN X
In Y
SUB X
STO Y
OUT Y
DC: X 0
DC: Y 0
And I wrote a compiler that has now converted this file into machine code so I have created a file for example called program.txt.ex and it contains a bunch of @@@@@@@ or machine code and I did this using the SecondPass code above and now I need to write a load method that will allow me to load and run this file. 我编写了一个编译器,现在将该文件转换为机器代码,因此我创建了一个名为program.txt.ex的文件,其中包含一堆@@@@@@@@或机器代码,而我使用上面的SecondPass代码,现在我需要编写一个加载方法,该方法允许我加载和运行此文件。
Here is my Run method 这是我的运行方法
public void run(String filename) throws IOException
{
if (mem == null)
System.out.println("mem null");
if (filename == null)
System.out.println("filename null");
mem.loadFromFile(filename);
cpu.reset();
cpu.setMDR(mem.read(cpu.getMAR()));
cpu.fetch2();
while (!cpu.stop())
{
cpu.decode();
if (cpu.OutFlag())
OutPut.display(mem.read(cpu.getMAR()));
if (cpu.InFlag())
mem.write(cpu.getMDR(),in.getInt());
if (cpu.StoreFlag())
{
mem.write(cpu.getMAR(),in.getInt());
cpu.getMDR();
}
else
{
cpu.setMDR(mem.read(cpu.getMAR()));
cpu.execute();
cpu.fetch();
cpu.setMDR(mem.read(cpu.getMAR()));
cpu.fetch2();
}
}
}
The Run Method: 运行方法:
public void run(int mem)
{
cpu.reset();
cpu.setMDR(mem.read(cpu.getMAR()));
cpu.fetch2();
while (!cpu.stop())
{
cpu.decode();
if (cpu.OutFlag())
OutPut.display(mem.read(cpu.getMAR()));
if (cpu.InFlag())
mem.write(cpu.getMDR(),in.getInt());
if (cpu.StoreFlag())
{
mem.write(cpu.getMAR(),in.getInt());
cpu.getMDR();
}
else
{
cpu.setMDR(mem.read(cpu.getMAR()));
cpu.execute();
cpu.fetch();
cpu.setMDR(mem.read(cpu.getMAR()));
cpu.fetch2();
}
}
}
If seems that you need to load the whole file in memory before starting execution, so it would go: 如果似乎您需要在开始执行之前将整个文件加载到内存中,那么它将进行:
public int[] load(String newfile) throws FileNotFoundException
{
int mem[] = new int[100];
try {
File file = new File(newfile);
FileInputStream fs = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fs);
for (int i = 0; i < mem.length; ++i) {
mem[i] = dis.readInt();
}
dos.readInt();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
return mem;
}
void run(int mem[]) {
// now execute code
int pc = 0;
loop: while (true) {
int inst = mem[pc++];
int opcode = inst/100;
int loc = inst%100;
switch (opcode) {
case OpCode.STOP:
break loop;
case OpCode.IN:
...
}
}
}
I notice that your loader does a single 我注意到您的装载机只完成一个
dos.readInt();
...which will read a single integer value from your file. ...这将从您的文件中读取一个整数值。 What you probably want to do is create a loop that reads int
s until you hit the end-of-file on dos
(which might more aptly be named dis
, no?). 您可能想做的是创建一个读取int
的循环,直到您在dos
文件末尾为止(它可能更合适地命名为dis
,否?)。 You could add
those int
s to a dynamic container like an ArrayList
, which will grow with every element you stuff into it. 您可以add
这些int
add
到诸如ArrayList
的动态容器中,该容器将随您填充到其中的每个元素一起增长。 Once done loading, you can use toArray
to copy all those int
s to an array of the appropriate size. 加载完成后,您可以使用toArray
将所有这些int
复制到适当大小的数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.