伪代码:
寄存器用变量表示 内存用数组表示
定义宏定义为操作码功能
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;}

C语言写个简单的虚拟机相关推荐

  1. c语言写一个简单的小游戏-推箱子

    在学习C语言之后,写了一个简单的小游戏来锻炼自己的代码以及C语言知识的掌握能力. 推箱子作为手机上最常见的简单游戏,其代码也相对简单,想法也比较简单,下面为其代码和运行图. /************ ...

  2. 一个简单的c 游戏编程语言,编程达人 c语言写一个简单的小游戏-推箱子

    在学习C语言之后,写了一个简单的小游戏来锻炼自己的代码以及C语言知识的掌握能力. 推箱子作为手机上最常见的简单游戏,其代码也相对简单,想法也比较简单,下面为其代码和运行图. /************ ...

  3. Java语言写一个简单的学生信息管理系统,通过JDBC连接数据库对学生信息进行增删改查,采用三层思想和DBUtils第三方框架。

    我把源代码和sql文件放GitHub上了,你们可以自行下载:https://github.com/fenglily1/student. 有问题可以留言或私信,我看到就会回. 进阶版加上页面的管理系统在 ...

  4. C语言写的简单病毒程序

    <script type="text/javascript"> </script><script type="text/javascript ...

  5. C语言写个简单的串口调试助手

    学习单片机或者嵌入式编程常常要用到超级终端–串口调试助手,但有时需要我们自己做一个终端,用来当上位机.最近课设要求我们自己写个终端来处理串口信息,于是乎,接触了一些windows的串口API,做出了一 ...

  6. 用C语言写一个简单的小游戏——猜数字

    我们该如何设计这个程序? 1.首先应该打印一个菜单,让玩家选择玩游戏或者退出游戏 2.当玩家选择玩游戏,我们让电脑生成一个1~100的随机数,让玩家去猜 3.如果玩家猜的数比电脑生成的数大,我们提示猜 ...

  7. c语言定时开关程序,C语言写的简单的定时关机程序

    写一个定时关机的小程序,可以立即关闭计算机,也可以一段时间后关闭计算机. 这里主要考察system()命令. 代码实现: #include #include #include int main() { ...

  8. 用C语言写一个简单的贪吃蛇游戏(用到easyx图形库)

    学习内容:1.游戏的存储与读写 2.制作游戏菜单 3.制作定时器 目录 一.需要用到的头文件.全局变量和一些函数 二.定时器 三.数据的初始化和绘制 四.蛇的运行和吃食物 五.游戏菜单界面 六.游戏存 ...

  9. php编写服务器端脚本程序,PHP脚本语言写的简单服务器程序

    $username = $_POST["username"];/*客户端请求方式为POST,请求参数封装成nsdata类型放在HTTPBody中传给服务器,服务器用PHP脚本语言接 ...

最新文章

  1. 写一个判断素数的函数,在主函数输入一个整数,输出是否是素数的消息。
  2. 2016最热门的PHP框架
  3. SAP CRM Fiori My task应用里roundtrip取舍的讨论
  4. Java程序员应该知道的10个Eclipse调试技巧
  5. 推动Windows的限制:虚拟内存
  6. 【iOS报错】“Internal error. Please file a bug at bugreport.apple.com and attach /var/folders/v5/......”
  7. 经济学原理曼昆第八版课后习题答案
  8. Linux安装VIM执行sudo apt-get install vim 现在没有可用的软件包但是它被其他软件包用了
  9. zyf的童年(异或运算的运用)
  10. 【BZOJ 3653】谈笑风生
  11. 无线蓝牙耳机什么牌子好?适合学生党的蓝牙耳机性价比排行榜
  12. 工具传送门(持续更新)
  13. flutter onPressed onTap等手势检测及触摸事件处理
  14. SC8701 120W DC TO DC 电源模块的设计
  15. 你用过的论文写作神工具有哪些?
  16. 用python爬取考研信息网_用Python爬取了考研吧1000条帖子,原来他们都在讨论这些!...
  17. 江苏统考计算机英语作文,2017年高考江苏卷英语作文题目
  18. c语言乞丐的图片,乞丐简笔画图片
  19. 经纬度坐标点和距离之间的转换
  20. ​stp文件转iges

热门文章

  1. CSDN 签到在哪里
  2. 简单的一些安全知识总结
  3. 功率运算放大器。求助
  4. python三维数据可视化的多种方法
  5. 安卓系统应用Java什么核心技术_《深入Android应用开发:核心技术解析与最佳实践...
  6. 科研知识:关于质谱中常见的质量概念
  7. 单片机定时器和计数器的概念
  8. 【C#】Winform监听USB串口设备拔插
  9. 会议室管理前端页面_会议室预定设计
  10. Android APK瘦身/减小包体