⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpu.cpp

📁 此程序类似于我们一般使用的CPU
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <iostream.h>
#include "cpu.h"
CPU::CPU()
{
	myMemory=new Memory();
}
void CPU::InsertProgramCodeToMemory(char * code)
{
	if ((*code=='M')&&(*(code+1)=='O')&&(*(code+2)=='V'))
	{
		BYTE tempByte;
		int i;
		int addr;
		if (((*(code+4)>='0')&&(*(code+4)<='9'))||(*(code+4)=='-'))
		{
		//指令为MOV,且第一个参数是数字时,二进制命令前8位为0000 0000
		//第0~3位是指令标识,第7位是数字或变量标识
		//当第一参数是数字时,第8~23位存储数字的值
			tempByte=(BYTE)(0x00);
			myMemory->InsertCode(tempByte);
			int num=0;
			int flag=1;
			i=4;
			if (*(code+4)=='-')
			{
				i=5;
				flag=-1;
			}
				
			while (*(code+i)!=' ')
			{
				num=num*10;
				num=num-48;
				num=num+(int)(*(code+i));
				i++;
			}
			num=num*flag;
			tempByte=(BYTE)((num&0xff00)>>8);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(num&0x00ff);
			myMemory->InsertCode(tempByte);
		}
		else if (((*(code+4)>='a')&&(*(code+4)<='z'))||((*(code+4)>='A')&&(*(code+4)<='Z')))
		{
		//指令为MOV,且第一个参数是变量,前8位为0000 0001
		//当第一个参数是变量时,第8~15位存储该变量的地址
			tempByte=(BYTE)(0x01);
			myMemory->InsertCode(tempByte);
			addr=myMemory->FindVarAddr(*(code+4));
			if (addr==-1)
			{
				cout<<"ERROR!"<<endl;
				return;
			}
			tempByte=(BYTE)(addr&0xff);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(0x00);
			myMemory->InsertCode(tempByte);
			i=5;
		}
		//指令的最后8位是第二个变量的地址,若第二个变量之前没有定义,则定义并返回地址
		i++;
		char tempVar=*(code+i);
		addr=myMemory->FindVarAddr(tempVar);
		if (addr==-1)
		{
			addr=myMemory->InsertIntoMemory(tempVar,0);
		}
		tempByte=(BYTE)(addr&0xff);
		myMemory->InsertCode(tempByte);
		return;
	}
	else if ((*code=='A')&&(*(code+1)=='D')&&(*(code+2)=='D'))
	{
		BYTE tempByte;
		int i;
		int addr;
		if (((*(code+4)>='0')&&(*(code+4)<='9'))||(*(code+4)=='-'))
		{
			//指令为ADD,且第一个参数是数字时,二进制命令前8位为0001 0000
			//当第一参数是数字时,第8~23位存储数字的值
			tempByte=(BYTE)(0x10);
			myMemory->InsertCode(tempByte);
			int num=0;
			int flag=1;
			i=4;
			if (*(code+4)=='-')
			{
				i=5;
				flag=-1;
			}
			while (*(code+i)!=' ')
			{
				num=num*10;
				num=num-48;
				num=num+(int)(*(code+i));
				i++;
			}
			num=num*flag;
			tempByte=(BYTE)((num&0xff00)>>8);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(num&0x00ff);
			myMemory->InsertCode(tempByte);
		}
		else if (((*(code+4)>='a')&&(*(code+4)<='z'))||((*(code+4)>='A')&&(*(code+4)<='Z')))
		{
			//指令为ADD,且第一个参数是变量,前8位为0001 0001
			//当第一个参数是变量时,第8~15位存储该变量的地址
			tempByte=(BYTE)(0x11);
			myMemory->InsertCode(tempByte);
			addr=myMemory->FindVarAddr(*(code+4));
			if (addr==-1)
			{
				cout<<"ERROR!"<<endl;
				return;
			}
			tempByte=(BYTE)(addr&0xff);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(0x00);
			myMemory->InsertCode(tempByte);
			i=5;
		}
		i++;
		char tempVar=*(code+i);
		addr=myMemory->FindVarAddr(tempVar);
		if (addr==-1)
		{
			addr=myMemory->InsertIntoMemory(tempVar,0);
		}
		tempByte=(BYTE)(addr&0xff);
		myMemory->InsertCode(tempByte);
		return;	
	}
	else if ((*code=='S')&&(*(code+1)=='U')&&(*(code+2)=='B'))
	{
		BYTE tempByte;
		int i;
		int addr;
		if (((*(code+4)>='0')&&(*(code+4)<='9'))||(*(code+4)=='-'))
		{
			//指令为SUB,且第一个参数是数字时,二进制命令前8位为0010 0000
			//当第一参数是数字时,第8~23位存储数字的值
			tempByte=(BYTE)(0x20);
			myMemory->InsertCode(tempByte);
			int num=0;
			int flag=1;
			i=4;
			if (*(code+4)=='-')
			{
				i=5;
				flag=-1;
			}
			while (*(code+i)!=' ')
			{
				num=num*10;
				num=num-48;
				num=num+(int)(*(code+i));
				i++;
			}
			num=num*flag;
			tempByte=(BYTE)((num&0xff00)>>8);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(num&0x00ff);
			myMemory->InsertCode(tempByte);
		}
		else if (((*(code+4)>='a')&&(*(code+4)<='z'))||((*(code+4)>='A')&&(*(code+4)<='Z')))
		{
			//指令为SUB,且第一个参数是变量,前8位为0010 0001
			//当第一个参数是变量时,第8~15位存储该变量的地址
			tempByte=(BYTE)(0x21);
			myMemory->InsertCode(tempByte);
			addr=myMemory->FindVarAddr(*(code+4));
			if (addr==-1)
			{
				cout<<"ERROR!"<<endl;
				return;
			}
			tempByte=(BYTE)(addr&0xff);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(0x00);
			myMemory->InsertCode(tempByte);
			i=5;
		}
		i++;
		char tempVar=*(code+i);
		addr=myMemory->FindVarAddr(tempVar);
		if (addr==-1)
		{
			addr=myMemory->InsertIntoMemory(tempVar,0);
		}
		tempByte=(BYTE)(addr&0xff);
		myMemory->InsertCode(tempByte);
		return;	
	}
	else if ((*code=='M')&&(*(code+1)=='U')&&(*(code+2)=='L'))
	{
		BYTE tempByte;
		int i;
		int addr;
		if (((*(code+4)>='0')&&(*(code+4)<='9'))||(*(code+4)=='-'))
		{
			//指令为MUL,且第一个参数是数字时,二进制命令前8位为0011 0000
			//当第一参数是数字时,第8~23位存储数字的值
			tempByte=(BYTE)(0x30);
			myMemory->InsertCode(tempByte);
			int num=0;
			int flag=1;
			i=4;
			if (*(code+4)=='-')
			{
				i=5;
				flag=-1;
			}
			while (*(code+i)!=' ')
			{
				num=num*10;
				num=num-48;
				num=num+(int)(*(code+i));
				i++;
			}
			num=num*flag;
			tempByte=(BYTE)((num&0xff00)>>8);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(num&0x00ff);
			myMemory->InsertCode(tempByte);
		}
		else if (((*(code+4)>='a')&&(*(code+4)<='z'))||((*(code+4)>='A')&&(*(code+4)<='Z')))
		{
			//指令为MUL,且第一个参数是变量,前8位为0011 0001
			//当第一个参数是变量时,第8~15位存储该变量的地址
			tempByte=(BYTE)(0x31);
			myMemory->InsertCode(tempByte);
			addr=myMemory->FindVarAddr(*(code+4));
			if (addr==-1)
			{
				cout<<"ERROR!"<<endl;
				return;
			}
			tempByte=(BYTE)(addr&0xff);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(0x00);
			myMemory->InsertCode(tempByte);
			i=5;
		}
		i++;
		char tempVar=*(code+i);
		addr=myMemory->FindVarAddr(tempVar);
		if (addr==-1)
		{
			addr=myMemory->InsertIntoMemory(tempVar,0);
		}
		tempByte=(BYTE)(addr&0xff);
		myMemory->InsertCode(tempByte);
		return;
	}
	else if ((*code=='C')&&(*(code+1)=='O')&&(*(code+2)=='P')&&(*(code+3)=='Y'))
	{
		BYTE tempByte;
		int i;
		int addr;
		if (*(code+5)=='\'')
		{
		//指令是COPY,且第一个参数为字符时,前8位为0100 0000
		//第8~15位存储该字符
			tempByte=(BYTE)(0x40);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(*(code+6));
			myMemory->InsertCode(tempByte);
			i=8;
		}
		else if (((*(code+4)>='a')&&(*(code+4)<='z'))||((*(code+4)>='A')&&(*(code+4)<='Z')))
		{
		//第一个参数是变量时,前8位为0100 0001
		//第8~15位存储该变量的地址
			tempByte=(BYTE)(0x41);
			myMemory->InsertCode(tempByte);
			addr=myMemory->FindVarAddr(*(code+5));
			if (addr==-1)
			{
				cout<<"ERROR!"<<endl;
				return;
			}
			tempByte=(BYTE)(addr&0xff);
			myMemory->InsertCode(tempByte);
			i=6;
		}
		i++;
		tempByte=(BYTE)(0x00);
		myMemory->InsertCode(tempByte);
		char tempVar=*(code+i);
		addr=myMemory->FindVarAddr(tempVar);
		if (addr==-1)
		{
			addr=myMemory->InsertIntoMemory(tempVar,' ');
		}
		tempByte=(BYTE)(addr&0xff);
		myMemory->InsertCode(tempByte);
		return;
	}
	else if ((*code=='P')&&(*(code+1)=='R')&&(*(code+2)=='I')&&(*(code+3)=='N')&&(*(code+4)=='T'))
	{
		
		BYTE tempByte;
		int i;
		int addr;
		if (((*(code+6)>='0')&&(*(code+6)<='9'))||(*(code+6)=='-'))
		{
		//指令为PRINT,且参数为数字,二进制指令前8位为0101 0000
		//第8~23位存储该数字
			tempByte=(BYTE)(0x50);
			myMemory->InsertCode(tempByte);
			i=6;
			int flag=1;
			int num=0;
			if (*(code+6)=='-')
			{
				i=7;
				flag=-1;
			}
			while (*(code+i)!='\0')
			{
				
				num=num*10;
				num=num-48;
				num=num+(int)(*(code+i));
				i++;
			}
			num=num*flag;
			tempByte=(BYTE)((num&0xff00)>>8);
			
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(num&0x00ff);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -