C语言写个简单的虚拟机

伪代码:
寄存器用变量表示 内存用数组表示
定义宏定义为操作码功能
1.数据定义
2.打印功能
3.错误判断

1.数据定义
{
定义寄存器
定义计数器
定义指针寄存器(指向下一条将要执行的指令EIP)
定义操作码
定义操作数

}

2.打印函数
{
打印信息

}
3.错误判断
{
判断是否溢出
判断除数
}

main函数
{
do
{
do{

	}whlie
}whlie
输入

do{
SWITCH语句执行操作



}(操作码不等于43)

}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>

#define NUMBER 100
#define FLAG -9999

/*操作码*/
#define READ 10
#define WRITHE 11

#define LOAD 20
#define STORE 21

/*算术操作*/
#define ADD 30
#define SUBTRACT 31
#define DIVIDE 32
#define MULTIPLY 33

/*控制操作*/
#define BRANCH 40
#define BRANCHNEG 41
#define BRANCHZERO 42
#define HALT 43 

//打印函数
void printfmycomputer(int memory[], int accumulator, int instructionCounter,
	int instructionRegister, int operationCode, int operand);

bool errZero(int operand);//除0错误判断
bool errAccumulator(int accumulator);//累加器溢出错误
bool errorOperandCode(int operationCode);//操作码错误


int main() {

	printf("*** Hi there!***\n"
		"*** 请每次输入一条指令或一条数据 ***\n"
		"*** 在指令或数据前将会打上位置编号和‘?’ ***\n"
		"*** 然后在该位置输入对应的一个‘word’ ***\n"
		"*** 当输入哨兵值 - 9999 时,停止输入 ***\n"
		"*** your program. ***\n\n");

	int memory[NUMBER] = { 0 }; //内存初始化

	/*寄存器值初始化*/
	int accumulator = 0;          //累积器
	int instructionCounter = 0;   //内存编号
	int instructionRegister = 0;  //‘word'
	int operationCode = 0;        //操作码
	int operand = 0;              //位置编号 
	   /*打印内存*/
	printfmycomputer(memory, accumulator, instructionCounter,
		instructionRegister, operationCode, operand);
	printf("\n\n");

	do 
	{
		do 
		{
			printf("%02d ? +", instructionCounter);
			scanf("%d", &memory[instructionCounter]);
			if (memory[instructionCounter] > 9999 || memory[instructionCounter] < -9999)
				printf("输入错误,请重新输入!\n");

		} while (memory[instructionCounter] > 9999 || memory[instructionCounter] < -9999);
		++instructionCounter;
	} while (memory[instructionCounter-1]!=FLAG);//内存结束


	printf("\n*****程序开始执行****\n");
	instructionCounter = 0;
	do 
	{
		instructionRegister = memory[instructionCounter];
		operationCode = instructionRegister / 100; //取操作码
		if (errorOperandCode(operationCode)== false)
		{
			operationCode = 43;//结束程序
			printf("\n***操作码错误***\n");
		}

		operand = instructionRegister % 100;

		switch (operationCode)
		{
		case 10:
			printf("?");
			scanf("%d", &memory[operand]);
			printfmycomputer(memory, accumulator, instructionCounter,
				instructionRegister, operationCode, operand);
			++instructionCounter;
			break;
		case 11:
			printf("%d\n", memory[operand]);
			printfmycomputer(memory, accumulator, instructionCounter,
				instructionRegister, operationCode, operand);
			++instructionCounter;
			break;
		case 20:
			accumulator = memory[operand];
			printfmycomputer(memory, accumulator, instructionCounter,
				instructionRegister, operationCode, operand); 
			++instructionCounter;
			break;
		case 21:
			memory[operand] = accumulator;
			accumulator = memory[operand];
			printfmycomputer(memory, accumulator, instructionCounter,
				instructionRegister, operationCode, operand);
			++instructionCounter;
			break;
		case 30:
			accumulator += memory[operand];
			if (errAccumulator(accumulator)== true)
			{
				operationCode = 43;
				printf("\n***累加器满了***\n");
				break;
			}
			else
			{
				printfmycomputer(memory, accumulator, instructionCounter,
					instructionRegister, operationCode, operand);
				++instructionCounter;
				break;
			}
		case  31:
			accumulator -= memory[operand];
			if (errAccumulator(accumulator) == true)
			{
				operationCode = 43;
				printf("\n***累加器满了***\n");
				break;
			}
			else
			{
				printfmycomputer(memory, accumulator, instructionCounter,
					instructionRegister, operationCode, operand);
				++instructionCounter;
				break;
			}
		case 32:
			if (errZero(operand) == true)
			{
				operationCode = 43;
				printf("\n***除数为0***\n");
				break;
			}
			accumulator /= memory[operand];
			if (errAccumulator(accumulator) == 0)
			{
				operationCode = 43;
				printf("\n***累加器溢出错误***\n***程序将终止执行!\n");
				break;
			}
			else
			{
				printfmycomputer(memory, accumulator, instructionCounter,
					instructionRegister, operationCode, operand);
				++instructionCounter;
				break;
			}
		case 33:
			accumulator *= memory[operand];
			if (errAccumulator(accumulator) == 0)
			{
				operationCode = 43;
				printf("\n***累加器溢出错误***\n***程序将终止执行!\n");
				break;
			}
			else
			{
				printfmycomputer(memory, accumulator, instructionCounter,
					instructionRegister, operationCode, operand);
				++instructionCounter;
				break;
			}
		case 40:
			instructionCounter = operand;
			printfmycomputer(memory, accumulator, instructionCounter,
				instructionRegister, operationCode, operand);
			break;
		case 41:
			if (accumulator < 0)
			{
				instructionCounter = operand;
			}
			else {
				++instructionCounter;
				printfmycomputer(memory, accumulator, instructionCounter,
					instructionRegister, operationCode, operand); 
				break;
			
			}
		case 42:
			if (accumulator == 0)
			{
				instructionCounter = operand;
			}
			else
			{
				++instructionCounter;
				printfmycomputer(memory, accumulator, instructionCounter,
					instructionRegister, operationCode, operand);
				break;

			}
		case 43:
			instructionCounter = operand;
			printf("done\n");
			printfmycomputer(memory, accumulator, instructionCounter,
				instructionRegister, operationCode, operand);
			break;
		default:
			printf("输入错误,请重新输入!\n");
			printfmycomputer(memory, accumulator, instructionCounter,
				instructionRegister, operationCode, operand); 
			break;
		}


	} while (operationCode != 43);

	return 0;
}

void printfmycomputer(int memory[], int accumulator, int instructionCounter,
	int instructionRegister, int operationCode, int operand) {

	printf("REGISTERS:\n");
	printf("accumulator          %+05d\n", accumulator);
	printf("instructionCounter      %02d\n", instructionCounter);
	printf("instructionRegister  %+05d\n", instructionRegister);
	printf("operationCode           %02d\n", operationCode);
	printf("operand                 %02d\n\n", operand);
	printf("MEMORY:\n");

	printf("\t0     1     2     3     4     5     6     7     8     9\t");
	for (int i = 0; i < 100; i++)
	{
		if (i % 10 == 0)
		{
			printf("\n");
			printf("%2d  ", i);
		}
		printf("%+05d ", memory[i]);
	}
	printf("\n\n");





}

//判断操作码
bool errorOperandCode(int operationCode)
{

	if (operationCode == 10 || operationCode == 11 || operationCode == 20
		|| operationCode == 21 || operationCode == 30 || operationCode == 31
		|| operationCode == 32 || operationCode == 33 || operationCode == 40
		|| operationCode == 41 || operationCode == 42 || operationCode == 43)
	{
		return true;

	}
		
	return false;

}

bool errAccumulator(int accumulator)
{
	if (accumulator > 9999 || accumulator < -9999)
	{
		return true;
	}
	return false;
}

//判断0
bool errZero(int operand) {

	if (operand == 0)
	{
		return true;
	}
	return false;

}

在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值