그래서 가상 컴퓨터를 만드는 것이 목표 인 자바 프로젝트에서 작업 해 왔습니다. 그래서 기본적으로 한 가지 문제 만 있습니다. 나는 어셈블리 코드가있는 txt 문서를 번역하는 컴파일러를 만들었고, 나의 컴파일러는이 코드를 기계 실행 가능한 int로 작성한 새로운 파일을 만들었다. 하지만 이제는이 int를 읽고 프로그램을 실행하는로드 메서드를 작성해야하지만이 작업을 수행하는 데 어려움을 겪고 있습니다. 어떤 도움을 많이 주시면 감사하겠습니다 .... 이것은 숙제가 아닙니다. 이 프로젝트는 단순히 컴파일러를 만드는 것이었고 지금은 내 자신의 이익을 위해 완성하려고합니다. 감사.Java 컴파일러 -로드 방법
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();
}
}
그래서 내가 무슨 짓을하는 것은 파일을 쓸 수 있습니다 : 여기
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();
}
}
확인 writeInts을 수행하는 컴파일러의 일부입니다 : 여기
내가 부하 지금까지 무엇을 가지고IN X
In Y
SUB X
STO Y
OUT Y
DC: X 0
DC: Y 0
그리고 기계 코드로이 파일을 변환 이제 한 컴파일러를 썼다 그래서 난 CREAT가 : TXT 등의 예를 들어 program.txt.ex라는 파일을 열었습니다.이 파일에는 @@@@@@@ 또는 기계 코드가 들어 있으며 위의 SecondPass 코드를 사용하여이 작업을 수행했습니다. 이제는로드 메소드를 작성해야합니다. 이 파일을로드하고 실행하십시오. 당신이 실행을 시작하기 전에 메모리에 전체 파일을로드 할 필요가 있다는 것 같으면
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();
}
}
}
그것은 기계 실행 가능 코드입니다. 왜로드해야합니까? 왜 그냥 기계에서 실행하지 않으시겠습니까? 나는 그것을 얻지 않는다 – OscarRyz
당신의 창조의 어떤 사실상 컴퓨터를위한이 진짜 기계 지시, 또는 근원, 또는 지시는인가? DataOutputStream 등으로 어떻게 작성 했습니까? – bmargulies
일부 답변을 수락하지 않은 이유는 무엇입니까? – WhirlWind