Java编译器加载方法
所以我一直在做一个java项目,目标是创建一台虚拟计算机。我基本上完成了,但有一个问题。我已经创建了一个编译器,它可以翻译一个包含汇编代码的txt文档,并且我的编译器已经创建了一个新文件,其中的代码被编写为机器可执行文件ints。但是现在我需要编写一个load方法来读取这些int并运行程序,但是我在这方面遇到了困难。非常感谢您的帮助……如果您这样想,这也不是家庭作业。这个项目仅仅是为了制作一个编译器,现在我正试图为了自己的兴趣来完成它。谢谢 以下是我到目前为止的负载:Java编译器加载方法,java,Java,所以我一直在做一个java项目,目标是创建一台虚拟计算机。我基本上完成了,但有一个问题。我已经创建了一个编译器,它可以翻译一个包含汇编代码的txt文档,并且我的编译器已经创建了一个新文件,其中的代码被编写为机器可执行文件ints。但是现在我需要编写一个load方法来读取这些int并运行程序,但是我在这方面遇到了困难。非常感谢您的帮助……如果您这样想,这也不是家庭作业。这个项目仅仅是为了制作一个编译器,现在我正试图为了自己的兴趣来完成它。谢谢 以下是我到目前为止的负载: public void
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();
}
}
好的,这是编译器执行写入操作的部分:
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();
}
}
我编写了一个编译器,现在已经将这个文件转换成机器代码,所以我创建了一个文件,例如program.txt.ex,它包含一堆机器代码,我使用上面的第二个过程代码完成了这项工作,现在我需要编写一个加载方法,允许我加载和运行这个文件
这是我的跑步方法
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();
}
}
}
运行方法:
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();
}
}
}
如果您似乎需要在开始执行之前将整个文件加载到内存中,那么它将:
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:
...
}
}
}
我注意到你的加载器只执行一次
dos.readInt();
…将从文件中读取单个整数值。您可能想做的是创建一个循环来读取ints,直到您在dos上到达文件的末尾,这个文件可能更适合命名为dis,no?。您可以将这些int添加到动态容器中,如ArrayList,它将随着您放入其中的每个元素而增长。加载完成后,您可以使用toArray将所有这些int复制到适当大小的数组。这是机器可执行代码,为什么需要加载它?你为什么不直接从机器上运行呢?我不明白这些是真的机器指令,还是nasm源代码,或者是你自己创建的虚拟计算机的指令?你是怎么写的,是用DataOutputStream还是别的什么?为什么你不接受一些答案?这太模糊了。请提出一个更具体的问题。你到底在做什么?你能运行内存中已经存在的指令吗?或者更直截了当地说,这是一个文件I/O问题还是一个虚拟机模拟问题?好的,Maurice,我已经写了一个run方法,我将发布它,但是在你的代码中,你说现在执行代码的地方就是我运行代码的地方。或者在我的主程序中,我可以先执行computer.loadfilename,然后再执行computer.runfilename,而不编写执行部分吗?最好让load返回加载的代码,然后查看更新的postok Maurice,所以这是我的Run方法,但我认为它不正确。我会在上面发帖的。
dos.readInt();