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

本文介绍了一个用Java实现的简单指令模拟系统。该系统包括输入、加法、输出等基本指令,并通过内存管理和CPU执行流程展示了指令的执行过程。适用于初学者理解计算机基本指令的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码(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();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值