接前一篇文章,这里给出我实现的Simpletron模拟器的源代码。我觉得代码中很多地方看着不舒服,但是现在将就能用了。
首先是Simpletron.h:
#ifndef _SIMPLETRON_H_
#define _SIMPLETRON_H_

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#define READ        10
#define WRITE        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

/*global variables and function prototype*/
int        accumulator;
short    instructionCounter;
int        instructionRegister;
short    operationCode;
short    operand;
int        memory[100];

static void    init();
void    instruction()
{
    printf("*** Welcome to Simpletron ***/n");
    printf("*** Please enter your program one instruction ***/n");
    printf("*** ( or data word ) at a time, I will type the ***/n");
    printf("*** location number and a question mark (?). ***/n");
    printf("*** You then type the word for that location. ***/n");
    printf("*** Type the sentinel -99999 to stop entering ***/n");
    printf("*** your program. ***/n");
}
void    dump()
{
    char sign = '+';
    int start = 9;
    int i = 0;
    int j = 0;
    int skip = 7;
    printf("/n/n");
    printf("REGISTERS:/n");
    printf("accumulator/t/t/t%+.4d/n", accumulator);
    printf("instructionCounter/t/t   %.2d/n", instructionCounter);
    printf("instructionRegister/t/t%+.4d/n", instructionRegister);
    printf("operationCode/t/t/t   %.2d/n", operationCode);
    printf("operand/t/t/t/t   %.2d/n", operand);
    printf("/n");
    printf("MEMORY:/n");
    for (i=0 ;i<10 ;i++ )
    {
        if (i > 0)
            printf("%7d", i);
        else
            printf("%9d", i);
    }
    printf("/n");
    for (i=0 ;i<10 ;i++ )
    {
        printf("%.2d", i*10);
        for (j=0 ;j<10 ;j++ )
        {
            printf(" %+.4d", memory[i*10 + j]);
        }
        printf("/n");
    }
}
void    halt()
{
    printf("/n*** Simpletron execution terminated ***/n");
    dump();
    exit(1);
}
void    error(int errno)/*errno indicates error number*/
{
    switch (errno)
    {
    case 1:
        printf("[Instruction is negative.]/n");break;
    case 2:
        printf("[Attempt to devide by zero.]/n");break;
    case 3:
        printf("[Result beyond -9999 ~ +9999.]/n");break;
   
    }
    printf("*** Simpletron execution abnormally terminated. ***/n");
    dump();
    exit(-1);
}
int        read();
int        write();
int        load();
int        store();
int        add();
int        subtract();
int        divide();
int        multiiply();
int        branch();
int        branchneg();
int        branchzero();
void    start();
void    exec();
void    exechelper();

#ifdef __cplusplus
}
#endif

#endif /* _SIMPLETRON_H_ */

然后是Simpletron.c:
#include <stdio.h>
#include <math.h>
#include "Simpletron.h"

int main(int argc, char *argv[])
{
    init();
    instruction();
    start();
    exec();
   
    return 0;
}
static void init()
{
    accumulator = 0;
    instructionCounter = 0;
    instructionRegister = 0;
    operationCode = 0;
    operand = 0;
}
void start()
{
    int resultOfScanf = 0;/*indicate whether scanf() succeed. 0 for false*/
    int scanfed = -1;
    while (scanfed != -99999)
    {
        printf("%.2d ? ", instructionCounter);
        resultOfScanf = scanf("%d", &scanfed);
        if (0 == resultOfScanf || (scanfed!=-99999 && abs(scanfed)>9999))
        {
            printf("[A wrong instruction or data word.(-9999 ~ +9999)]/n");
            continue;
        }
        if (scanfed == -99999)
        {
            instructionCounter--;
            break;
        }
        memory[ instructionCounter++ ] = scanfed;
    }
    printf("*** Program. loading completed ***/n");
}
void exec()
{
    init();
    printf("*** Program. execution begins ***/n");
    while (instructionCounter<100)
    {
        instructionRegister = memory[ instructionCounter++ ];
        if (instructionRegister <= 0)
        {
            instructionCounter--;
            error(1);
        }
        operationCode = instructionRegister/100;
        operand = instructionRegister%100;
        exechelper();
    }
}
void exechelper()
{
    switch (operationCode)
    {
    case READ:
        read();break;
    case WRITE:
        write();break;
    case LOAD:
        load();break;
    case STORE:
        store();break;
    case ADD:
        add();break;
    case SUBTRACT:
        subtract();break;
    case DIVIDE:
        divide();break;
    case MULTIPLY:
        multiply();break;
    case BRANCH:
        branch();break;
    case BRANCHNEG:
        branchneg();break;
    case BRANCHZERO:
        branchzero();break;
    case HALT:
        halt();
    }
}
int read()
{
    int resultOfScanf = 0;/*indicate whether scanf() succeed. 0 for false*/
    int scanfed = -1;
    while (1)
    {
        printf("? ");
        resultOfScanf = scanf("%d", &scanfed);
        if (0 == resultOfScanf || abs(scanfed)>9999)
            printf("[Please input a integer( -9999 ~ +9999 ).]/n");
        memory[ operand ] = scanfed;
        break;
    }
    return scanfed;
}
int write()
{
    printf("%d", memory[ operand ]);
    return 1;
}
int load()
{
    accumulator = memory[ operand ];
    return 1;
}
int store()
{
    memory[ operand ] = accumulator;
    return 1;
}
int add()
{
    accumulator += memory[ operand ];
    return 1;
}
int subtract()
{
    accumulator -= memory[operand ];
    return 1;
}
int divide()
{
    int div = memory[ operand ];
    if (div == 0)
    {
        error(2);
    }
    else
    {
        accumulator /= div;
    }
    return 1;
}
int multiply()
{
    accumulator *= memory[ operand ];
    if (abs(accumulator) > 9999)
        error(3);
    return 1;
}
int branch()
{
    instructionCounter = operand;
    return 1;
}
int branchneg()
{
    if (accumulator < 0)
        instructionCounter = operand;
    return 1;
}
int branchzero()
{
    if (accumulator == 0)
        instructionCounter = operand;
    return 1;
}

代码中没有什么注释,一来简单,二来懒惰。不过区区不到300行的代码相信不会难倒大家的。欢迎指正!!

PS:突然发现代码中内存100字,但是却没有判断输入的程序是否超过了100行,这是一个很大的bug,不过假定现在没有那么长的程序了。这点我现在也不想改了(很好改的),各位有兴趣的自己动手吧。

Simpletron模拟器(二)相关推荐

  1. Appium+Python MAC安装Android夜神模拟器(二)

    ① 目的 实现Android APP自动化 ② 环境 Pthon+appium+MAC ③安装Android 夜神模拟器 夜神模拟器官网:https://www.yeshen.com/ 1.下载到本地 ...

  2. 如何用Excel做一个战斗模拟器(二)属性表

    如何用Excel做一个战斗模拟器(一)升级经验表 如何用Excel做一个战斗模拟器(三)战斗过程模拟 目录索引 属性表 属性表 首先确定人物的生命.攻击.防御.暴击值与闪避值属性.用公式将其设定为与等 ...

  3. Charles学习(三)之使用Map local代理本地静态资源以及配置网页代理在Mac模拟器调试iOS客户端...

    前言 问题一:我们在App内嵌H5开发的过程中,肯定会遇到一个问题就是我不想在chrome的控制台中调试,我想要在手机上调试,那么如何解决这个问题呢? 问题二:我们期待调试时达到的效果就是和Charl ...

  4. xcode5切换IOS7,IOS6,IOS5模拟器

    一,先下载各个版本的模拟器,打开XCODE5,偏好设置,下载各个模拟器 二,编译程序的时候设置目标版本 三,在编译应用的时候就可以选择对应的模拟器版本,以及IOS的版本 转载于:https://www ...

  5. 如何用Excel做一个战斗模拟器(一)升级经验表

    如何用Excel做一个战斗模拟器(二)属性表 如何用Excel做一个战斗模拟器(三)战斗过程模拟 目录索引 引言 确定战斗公式与怪物强度 确定人物升级经验 确定人物升级时间 确定升级所需经验 引言 作 ...

  6. android模拟点击系统打开apk按钮,Android开发系列: 电脑端Android模拟器安装使用教程...

    如今说到什么智能手机最火,那很多人肯定会说android系统的手机.如果你还没有Android手机,又想买一部试试,可是价格都不便宜,买来不适合自己怎么办?那么可以先在电脑上安装一个Android模拟 ...

  7. 在电脑上安装android,在电脑上安装Android模拟器

    <在电脑上安装Android模拟器>由会员分享,可在线阅读,更多相关<在电脑上安装Android模拟器(12页珍藏版)>请在人人文库网上搜索. 1.在电脑上安装Android模 ...

  8. 下载夜神模拟器-安装autojs-连接vscode

    本文包括 1.下载夜神模拟器 2.在模拟器里安装autojs 3.使模拟器里的autojs连接电脑的vscode的详细步骤 文章目录 前言 一.下载夜神模拟器 二.在模拟器里安装autojs 三.使模 ...

  9. 如何用Excel做一个战斗模拟器(三)战斗过程模拟

    如何用Excel做一个战斗模拟器(一)升级经验表 如何用Excel做一个战斗模拟器(二)属性表 目录索引 定义战斗子过程 定位战斗双方基础属性 利用基础属性与战斗公式进行计算 战斗过程的运算与输出 判 ...

最新文章

  1. Oracle 11.2.0.3.7 PSU补丁升级
  2. Dcloud课程1 APP的架构有哪些
  3. HuaWei设置镜像端口和观察端口
  4. python映射类型-python映射类型的相关介绍
  5. ubuntu上开启SSH服务
  6. 我们学习到底是为了什么,到底什么才是我们真正想要的
  7. Matlab编程学习笔记【待续】
  8. idea2020.01的git下的Local Changes消失的问题
  9. Android 系统(265)----Android进程保活全攻略(上)
  10. HDU 4381 Grid
  11. linux下安装oracle instant client,linux下安装Oracle instant client
  12. 网络安全基础(木马、概述、冰河木马实验)
  13. 技术美术 之 游戏开发流程
  14. openwrt1907使用mt7621+mt715 5G wifi吞吐量低问题解决方法
  15. 苹果计算机远程控制软件,向日葵远程控制软件iPhone手机远程控制电脑
  16. 全系统进程隐藏win7winn10win11器风铃进程隐藏器软件
  17. css文字向右对齐_如何使用CSS实现文本左对齐、右对齐和居中对齐
  18. springboot整合容联云发短信验证码
  19. word强调文字颜色在哪,强调文字颜色2 word2010如何将文字设置成红色,强调文
  20. 水漆哪个品牌好?十大品牌水漆排行榜

热门文章

  1. C语言HeapBottomUP算法,C语言堆的建立Percolate Up and Down
  2. 产学交流 | 重庆科技学院数理学院一行到访芝诺数据
  3. c语言计算利息答案是0.0,ACCP北大青鸟4.0 程序逻辑和C语言实现课本后的习题和上机题目,怎么做?...
  4. 计算机假期计划内容,2019寒假计划,超详细学习计划表
  5. 挑战52天背完小猪佩奇(第02天)
  6. 软件设计师证书重要吗?
  7. 转载一篇不错的文章:谈谈“野生”Java程序员学习的道路
  8. macw资讯:MacOS如何隐藏、加密文件或文件夹
  9. 股价跳水20%,市值缩水1230亿美元?Facebook财报会议告诉你原因
  10. 知己知彼,一起来了解中国在线音频市场现状!