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

📄 cpu.cpp

📁 此程序类似于我们一般使用的CPU
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			
			myMemory->InsertCode(tempByte);
		}
		else if (*(code+6)=='\'')
		{
		//指令为PRINT,且参数为一个字符,二进制指令前8位为0101 0001
		//第8~15位存储该字符,第16~23位置0
			tempByte=(BYTE)(0x51);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(*(code+7));
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(0x00);
			myMemory->InsertCode(tempByte);
			i=9;
		}
		else if (((*(code+6)>='a')&&(*(code+6)<='z'))||((*(code+6)>='A')&&(*(code+6)<='Z')))
		{
		//指令为PRINT,且参数为一个变量
			
			style tempStyle=myMemory->GetVarStyle(*(code+6));
			if (tempStyle==EMPTY)
			{
				cout<<"ERROR!"<<endl;
				return;
			}
			else if (tempStyle==INTS)
			{
			//参数为int型时,前8位二进制指令为0101 0010
				tempByte=(BYTE)(0x52);
				myMemory->InsertCode(tempByte);
				
			}
			else if (tempStyle==CHARS)
			{
			//参数为char型时,前8位二进制指令为0101 0011
				tempByte=(BYTE)(0x53);
				myMemory->InsertCode(tempByte);
				
			}
			//第8~15位存储变量的地址,第16~23位都置0
			addr=myMemory->FindVarAddr(*(code+6));
			if (addr==-1)
			{
				cout<<"ERROR!"<<endl;
				return;
			}
			tempByte=(BYTE)(addr&0xff);
			myMemory->InsertCode(tempByte);
			tempByte=(BYTE)(0x00);
			myMemory->InsertCode(tempByte);
		}
		//将第24~31位置0
		tempByte=(BYTE)(0x00);
		myMemory->InsertCode(tempByte);
		
	}
}
void CPU::ExecuteCodeSegment()
{
	//IP为指令指针
	int IP=0;
	//flag为进位标志
	int flag=0;
	int Top=myMemory->pCodeNail;
	
	BYTE tempByte;
	int tempInt=0;
	int addr;
	while(IP<Top)
	{
		flag=0;
		
		tempByte=myMemory->CodeSegment[IP];
		
		tempInt=(int)(tempByte>>4);
		
		if (tempInt==0)
		{//该命令是MOV操作
			tempInt=(int)(tempByte&0x0f);
			if (tempInt==0)
			{//第一个参数是数字
				addr=(int)myMemory->CodeSegment[IP+3];
				tempByte=myMemory->CodeSegment[IP+1];
				
				myMemory->ChangeValues(addr,tempByte);
				tempByte=myMemory->CodeSegment[IP+2];
				myMemory->ChangeValues(addr+1,tempByte);
				
			}
			else if (tempInt==1)
			{//第一个参数是变量
				addr=(int)myMemory->CodeSegment[IP+1];
				int tempAddr=(int)myMemory->CodeSegment[IP+3];
				tempByte=myMemory->ReadData(addr);
				myMemory->ChangeValues(tempAddr,tempByte);
				tempByte=myMemory->ReadData(addr+1);
				myMemory->ChangeValues(tempAddr+1,tempByte);
			}
		}
		else if (tempInt==1)
		{//指令为ADD
			tempInt=(int)(tempByte&0x0f);
			if (tempInt==0)
			{//第一个参数是数字
				addr=(int)myMemory->CodeSegment[IP+3];
				BYTE tempByteX=myMemory->ReadData(addr+1);
				tempByte=myMemory->CodeSegment[IP+2];
				int AX=(int)tempByte+(int)tempByteX;
				if (AX>255)
					flag=1;
				tempByte=(BYTE)(AX&0xff);
				myMemory->ChangeValues(addr+1,tempByte);
				tempByteX=myMemory->ReadData(addr);
				tempByte=myMemory->CodeSegment[IP+1];
				AX=(int)tempByte+(int)tempByteX;
				AX=AX+flag;
				tempByte=(BYTE)(AX&0xff);
				myMemory->ChangeValues(addr,tempByte);
			}
			else if (tempInt==1)
			{//第一个参数是变量
				addr=(int)myMemory->CodeSegment[IP+3];
				tempByte=myMemory->ReadData(addr+1);
				int tempAddr=(int)myMemory->CodeSegment[IP+1];
				BYTE tempByteX=myMemory->ReadData(tempAddr+1);
				int AX=(int)tempByte+(int)tempByteX;
				if (AX>255)
					flag=1;
				tempByte=(BYTE)(AX&0xff);
				myMemory->ChangeValues(addr+1,tempByte);
				tempByte=myMemory->ReadData(tempAddr);
				tempByteX=myMemory->ReadData(addr);
				AX=(int)tempByte+(int)tempByteX;
				AX=AX+flag;
				tempByte=(BYTE)(AX&0xff);
				myMemory->ChangeValues(addr,tempByte);
			}
		}
		else if (tempInt==2)
		{//指令为SUB,运算为第二个参数减去第一个参数
			tempInt=(int)(tempByte&0x0f);
			if (tempInt==0)
			{//第一个参数为数字
			//	int AX=(int)(myMemory->CodeSegment[IP+2]^0xff);
				int AX=(int)myMemory->CodeSegment[IP+2];
				
				AX=AX^0x00ff;
				
				AX++;
				if (AX>255)
					flag++;
				
				addr=(int)myMemory->CodeSegment[IP+3];
				tempByte=myMemory->ReadData(addr+1);
				int BX=AX+(int)tempByte;
				tempByte=(BYTE)(BX&0xff);
				myMemory->ChangeValues(addr+1,tempByte);
				if (BX>255)
					flag++;
			//	AX=(int)(myMemory->CodeSegment[IP+1]^0x7f);
				AX=(int)myMemory->CodeSegment[IP+1];
				AX=AX^0x00ff;
				
				tempByte=myMemory->ReadData(addr);
				BX=AX+(int)tempByte;
				BX=BX+flag;
				tempByte=(BYTE)(BX&0xff);
				myMemory->ChangeValues(addr,tempByte);
			}
			else if (tempInt==1)
			{//第一个参数是变量
				int tempAddr=(int)myMemory->CodeSegment[IP+1];
				int AX=(int)(myMemory->ReadData(tempAddr+1)^0xff);
				AX++;
				if (AX>255)
					flag++;
				AX=AX&0xff;
				addr=(int)myMemory->CodeSegment[IP+3];
				tempByte=myMemory->ReadData(addr+1);
				int BX=AX+(int)tempByte;
				tempByte=(BYTE)(BX&0xff);
				myMemory->ChangeValues(addr+1,tempByte);
				if (BX>255)
					flag++;
				AX=(int)(myMemory->ReadData(tempAddr)^0xff);
				tempByte=myMemory->ReadData(addr);
				BX=AX+(int)tempByte;
				BX=BX+flag;
				tempByte=(BYTE)(BX&0xff);
				myMemory->ChangeValues(addr,tempByte);
			}
		}
		else if (tempInt==3)
		{//指令为MUL
			tempInt=(int)(tempByte&0x0f);
			int AX=0;
			int BX=0;
			int CX=0;
			int flag=1;
			if (tempInt==0)
			{//第一个参数是数字
				AX=(int)myMemory->CodeSegment[IP+1];
				AX=AX<<8;
				BX=(int)myMemory->CodeSegment[IP+2];
				AX=AX|BX;
				if (AX>=0x8000)
				{
					flag=flag*(-1);
					AX--;
					AX=AX^0xffff;
					
				}
				
			}
			else if (tempInt==1)
			{//第一个参数是变量
				addr=(int)myMemory->CodeSegment[IP+1];
				AX=(int)myMemory->ReadData(addr);
				AX=AX<<8;
				BX=(int)myMemory->ReadData(addr+1);
				AX=AX|BX;
				if (AX>=0x8000)
				{
					flag=flag*(-1);
					AX--;
					AX=AX^0xffff;					
				}
				
			}
			addr=(int)myMemory->CodeSegment[IP+3];
			BX=(int)myMemory->ReadData(addr);
			BX=BX<<8;
			CX=(int)myMemory->ReadData(addr+1);
			BX=BX|CX;
			if (BX>=0x8000)
			{
				flag=flag*(-1);
				BX--;
				BX=BX^0xffff;
				
			}
			CX=AX*BX;
			CX=CX*flag;
			
			tempByte=(BYTE)(CX>>8);
			myMemory->ChangeValues(addr,tempByte);
		//	cout<<"----------"<<(int)tempByte<<endl;
			tempByte=(BYTE)(CX&0x00ff);
			myMemory->ChangeValues(addr+1,tempByte);
		//	cout<<"------------"<<(int)tempByte<<endl;
		}
		else if (tempInt==4)
		{//指令为COPY
			tempInt=(int)(tempByte&0x0f);
			if (tempInt==0)
			{//第一个参数是数字
				addr=(int)myMemory->CodeSegment[IP+3];
				tempByte=myMemory->CodeSegment[IP+1];
				myMemory->ChangeValues(addr,tempByte);
			}
			else if (tempInt==1)
			{//第一个参数是变量
				addr=(int)myMemory->CodeSegment[IP+1];
				int tempAddr=(int)myMemory->CodeSegment[IP+3];
				tempByte=myMemory->ReadData(addr);
				myMemory->ChangeValues(tempAddr,tempByte);
			}
		}
		else if (tempInt==5)
		{//指令为PRINT
			
			tempInt=(int)(tempByte&0x0f);
			if (tempInt==0)
			{//参数是数字
				int AX=0;
				int BX=0;
				
				AX=(int)myMemory->CodeSegment[IP+1];
				
				AX=AX<<8;
				
				BX=(int)myMemory->CodeSegment[IP+2];
				
				AX=AX|BX;
				if (AX>=0x8000)
				{
					
					AX--;
					AX=AX^0xffff;
					AX=AX*(-1);
				}
				
				cout<<AX<<endl;
			}
			else if (tempInt==1)
			{//参数是字符
				char AX=(char)myMemory->CodeSegment[IP+1];
				cout<<AX<<endl;
			}
			else if (tempInt==2)
			{//参数是int型变量
				int AX=0;
				int BX=0;
				
				addr=(int)myMemory->CodeSegment[IP+1];
				AX=(int)myMemory->ReadData(addr);
				AX=AX<<8;
				BX=(int)myMemory->ReadData(addr+1);
				AX=AX|BX;
				if (AX>=0x8000)
				{
					
					AX--;
					AX=AX^0xffff;
					AX=AX*(-1);
				}
				cout<<AX<<endl;
				
			}
			else if (tempInt==3)
			{//参数是char型变量
				char AX;
				addr=(int)myMemory->CodeSegment[IP+1];
				AX=(char)myMemory->ReadData(addr);
				cout<<AX<<endl;
			}
		}
		IP=IP+4;
	}
	myMemory->MemoryCodeClear();
}

⌨️ 快捷键说明

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