linux操作系统学习-微指令程序执行模拟

代码(java实现)

package helloword;

interface IInstruction{
	void exe(int input);
}
abstract class Instruction implements IInstruction{
	static protected int register1;
	static protected int register2;
	static protected int ends;
	static protected boolean state=true;
	public static Memory[] mMemory = new Memory[10];
	static void add(Memory me) {
		mMemory[ends] = me;
		ends++;
	}
	static void readMemory() {
		for(int i=0;i<Instruction.ends;i++) {
			mMemory[i].exe();
		}
	}
}
class Input extends  Instruction{
	static public Input mInput= new Input();
	static Input get() {
		return mInput;
	}
	public void exe(int no) {
		if(state) {
			register1 = no;
			state = false;
		}else {
			register2 = no;
			state = true;
		}
	}
}
class Add extends  Instruction{
	static public Add mInput= new Add();
	static Add get() {
		return mInput;
	}
	public void exe(int no) {
		register1 = register1+register2;
	}
}
class Output extends  Instruction{
	static public Output mInput= new Output();
	static Output get() {
		return mInput;
	}
	public void exe(int no) {
		mMemory[no].value = register1;
	}
}
class Finish extends Instruction{
	static public Finish mInput= new Finish();
	static Finish get() {
		return mInput;
	}
	public void exe(int no) {
		//ends = 0;
	}
}
class IOut extends Instruction{
	static public IOut mInput= new IOut();
	static IOut get() {
		return mInput;
	}
	public void exe(int no) {
		ends = 0;
		System.out.println(register1);
	}
}
class CPU{
	static void exe(){
		Instruction.readMemory();	
	}
}

class Memory{
	public Memory(IInstruction ins, int v) {
		mInstruction = ins;
		value = v;
	}
	public IInstruction mInstruction;
	public int value;
	void exe() {
		mInstruction.exe(value);
	}
}

public class Test {
	public static void main(String[] args) {
		System.out.println("hello worldff !");
		Instruction.add(new Memory(Input.get(),5 ));
		Instruction.add(new Memory(Input.get(),6 ));
		Instruction.add(new Memory(Add.get(),0 ));
		Instruction.add(new Memory(Finish.get(),0 ));
		Instruction.add(new Memory(IOut.get(),0 ));
		
		CPU.exe();
	}
}

猜你喜欢

转载自blog.csdn.net/xie__jin__cheng/article/details/88930240