题目来源:http://poj.org/problem?id=1049

题目大意:

  一种小型的微处理器有以下特性:

  1. 每个字长4bit.

  2. 地址用2个字进行编码。先高位字后低位字,即高位字大的地址占据内存中靠前的字。

  3. 内存大小为256个字。

  4. 有两个微处理器A和B,每个存储一个字。

  5. 有9个指令编码。每条指令需要至少一个字来存储编码,其中有4条指令含参数,并需要额外的2个字。

  每4个bit组成的字可以取值0-15(10进制),下文中我们将用16进制来表示这些数。

  9条指令说明如下:

Code Words Description
0 3 LD: Load accumulator A with the contents of memory at the specified argument.
1 3 ST: Write the contents of accumulator A to the memory location specified by the argumen
2 1 SWP: Swap the contents of accumulators A and B.
3 1 ADD: Add the contents of accumulators A and B. The low word of the sum is stored in A, and the high word in B
4 1 INC: Increment accumulator A. Overflow is allowed; that is, incrementing F yields 0.
5 1 DEC: Decrement accumulator A. Underflow is allowed; that is, decrementing 0 yields F.
6 3 BZ: If accumulator A is zero, the next command to be executed is at the location specified by the argument. If A is not zero, the argument is ignored and nothing happens.
7 3 BR: The next command to be executed is at the location specified by the argument.
8 1 STP: Stop execution of the program.

程序总是最先执行地址00处的指令,然后依次执行后面的指令直到遇到Stop指令。

下面的例子展示了一些片段程序并描述了它的作用。

Program Description
01A8 Load accumulator A with the contents of memory location 1A (26 in decimal) and stop.
01A512F8 Load accumulator A with the contents of memory location 1A (26 in decimal), decrement it, store the result to memory location 2F, then stop.

输入:输入由若干行组成,每行256个16进制数字(1-9, A-F).每行表示的是内存的内容,地址编码从00到FF。00地址的内存单元为Stop指令时标志着输入的结束。输入的程序保证了程序的指令不会位于地址F0到FF的内存单元中。

输出:对每个输入的内存块,模拟微处理器的执行,输出程序执行后的每个内存字,格式与输入一致。


Sample Input

0102011311321128FF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Sample Output

0102011311321128FF1E00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

标题已经告诉我们方法:模拟。关键是细心,比如数字的INC和DEC要注意从数字到字母的跳变,还有分清楚值和址。勾起了当年做组成原理课程设计的"美好回忆"。=。=

 //////////////////////////////////////////////////////////////////////////
// POJ1049 Microprocessor Simulation
// Memory: 164K Time: 0MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <cstdio> using namespace std; char mem[];
char A, B;
int pc; int Hex2Dec(char c) {
if (c >= '' && c <= '') return c - '';
else return c - 'A' + ;
} int DHex2Dec(char a, char b) {
return Hex2Dec(a) * + Hex2Dec(b);
} char Dec2Hex(int i) {
if (i >= ) return 'A' + i - ;
else return i + '';
} int main(void) {
while (scanf("%s", mem) && mem[] != '') {
pc = ;
char command;
char buf;
int buf1, buf2;
A = B = '';
while ((command = mem[pc]) != '') {
switch (command) {
case ''://LD
A = mem[DHex2Dec(mem[pc + ], mem[pc + ])];
pc += ;
break;
case ''://ST
mem[DHex2Dec(mem[pc + ], mem[pc + ])] = A;
pc += ;
break;
case ''://SWP
buf = A;
A = B;
B = buf;
++pc;
break;
case ''://ADD
buf1 = Hex2Dec(A);
buf2 = Hex2Dec(B);
B = Dec2Hex((buf1 + buf2) / );
A = Dec2Hex((buf1 + buf2) % );
++pc;
break;
case ''://INC
if (A == 'F') A = '';
else if (A == '') A = 'A';
else ++A;
++pc;
break;
case ''://DEC
if (A == '') A = 'F';
else if (A == 'A') A = '';
else --A;
++pc;
break;
case ''://BZ
if (A == '') pc = DHex2Dec(mem[pc + ], mem[pc + ]);
else pc += ;
break;
case ''://BR
pc = DHex2Dec(mem[pc + ], mem[pc + ]);
break;
}
}
printf("%s\n", mem);
}
return ;
}

POJ1049 Microprocessor Simulation的更多相关文章

  1. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  2. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  3. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  4. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  5. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  6. Gate level Simulation(门级仿真)

    1 什么是后仿真? 后仿真也成为时序仿真,门级仿真,在芯片布局布线后将时序文件SDF反标到网标文件上,针对带有时序信息的网标仿真称为后仿真. 2 后仿真是用来干嘛的? 检查电路中的timing vio ...

  7. fdtd simulation, plotting with gnuplot, writting in perl

    # 9月13日 于成都黄龙溪 1 #!/usr/bin/perl # Author : Leon Email: yangli0534@gmail.com # fdtd simulation , plo ...

  8. 【转载】PMC/PEC Boundary Conditions and Plane Wave Simulation

    原文链接 PMC/PEC Boundary Conditions and Plane Wave Simulation (FDTD) OptiFDTD now has options to use Pe ...

  9. dipole antenna simulation by CST

    CST偶极子天线仿真,半波振子天线 一.本文使用CST仿真频率为1GHz的偶极子天线,使用2013版本.仿真的步骤为 1.选择一个CST的天线工程模板 2.设置好默认的单位 3.设置背景的材料(空气腔 ...

随机推荐

  1. Json-lib 进行java与json字符串转换之一

    这篇文章主要介绍了在java中,JSON字符串与java对象的相互转换实例详解,非常不错,具有参考借鉴价值,需要的朋友可以参考下. 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.J ...

  2. javascript 中的JSON.stringify - 将对象和数组转换为json格式(来源于网络)

          JSON.stringify 函数 (JavaScript) 将 JavaScript 值转换为 JavaScript 对象表示法 (Json) 字符串.     JSON.stringi ...

  3. C#一般处理程序 ashx.cs使用Session报错问题

    HttpContext.Current.Session["UserID"].ToString();//报错,报Session为Null, 此时需要添加引用和继承IRequiresS ...

  4. 关于c#里的集合的,结构体,枚举的定义,解释与应用

    那么先写一下 集合 . 集合和数组很相似,数组里的类型是必须同一类型,固定长度.然而集合里的可以是不同类型,不固定长度的.所以集合运用的灵活度要更高一些. 要使用集合,必须先引用命名空间:using ...

  5. 大O表示法总结

    大O符号用于计算机科学来描述算法的性能或复杂性.Big O特别描述了最坏的情况,可以用算法来描述所需的执行时间或使用的空间(例如在内存或磁盘上). 任何读过Programming Pearls(编程珠 ...

  6. 电脑安装unity3d有C盘逐渐爆满问题解决方案

    打开unity3d软件,Edit - Preference - GI Cache选中 Custom Cache Location,切换到别的盘,然后点击 Clean Cache清空一次,再查看C盘,存 ...

  7. 业务逻辑:五、完成认证用户的动态授权功能 六、完成Shiro整合Ehcache缓存权限数据

    一. 完成认证用户的动态授权功能 提示:根据当前认证用户查询数据库,获取其对应的权限,为其授权 操作步骤: 在realm的授权方法中通过使用principals对象获取到当前登录用户 创建一个授权信息 ...

  8. python3-打印一个进度条

    # Auther: Aaron Fan import sys,time for i in range(30): #打印一个#号,这种方法打印不会自动换行 sys.stdout.write('#') # ...

  9. getUserMedia API

    getUserMedia API 特别有趣的一个 API,能够调用电脑的摄像头,未来这个 API 将被广泛用来让浏览器和用户之间互动. 如果 <video> 标签和 Canvas 现结合 ...

  10. Git 之 配置文件与用户凭证

    配置文件 Git的配置文件有三个: 系统配置: /private/etc/gitconfig 用户配置: ~/.gitconfig 项目配置:.git/config 用户凭证 由于Git和Github ...