题目来源: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. LAMP 2.9 php扩展模块如何安装

    php 和 apache 类似,核心文件为/usr/local/php/bin/php,针对 apache 的是/usr/local/apache2/modules/libphp5.so 模块.这两个 ...

  2. DAY2-python数据类型、字符编码、文件处理

    阅读目录 一.引子 二.数字 三.字符串 四.列表 五.元祖 六.字典 七.集合 八.数据类型总结 九.运算符 十.字符编码 十一.文件处理 一.引子 1 什么是数据? x=10,10是我们要存储的数 ...

  3. dubbo错误排查之No provider available for the service

    今天搞的一个dubbo服务,暴漏出来了,但是consumer端启动就报这个错,排查过程记录一下 一.启动zkCli 利用命令查看 ls / ls /dubbo 继续查看 ls /dubbo/com.w ...

  4. 登陆Oracle出现错误java.lang.exception

    出现错误时登录企业管理器时出现的界面 出现这种错误一般是因为没有设置时区,一般默认的是agentTZRegion=GMT,也就是GMT.所以大家只要设置了这个东西,然后重新启动dbconsole就可以 ...

  5. 使用自定义线程池优化EchoServer

    在上一篇文章中http://www.cnblogs.com/gosaint/p/8494423.html 我自定义了线程池ThreadPool.现在在我的EchoServer中使用自定义线程池去负责和 ...

  6. 30-盐水(分段dfs)

    链接:https://www.nowcoder.com/acm/contest/94/K来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 131072K,其他语言2621 ...

  7. Python如何打包EXE可执行文件

    一.安装Pyinstaller 使用Python打包EXE可执行文件时,需要使用Pyinstaller,使用命令pip install PyInstaller进行安装,如下图: 安装成功后的效果如下图 ...

  8. Service Fabric 注意事项

    1. ActorTimer和ActorReminder会阻塞一个Actor的其他外部方法调用,即ActorTimer和ActorReminder内部就去未执行完毕之前,该Actor其他方法只能等待. ...

  9. linux定制的补充

    上一篇博文:http://www.cnblogs.com/hjc4025/p/6918323.html 这篇文章是对之前博文的一点扩展和补充: 这里主要是在之前的基础上添加了一些自己制作好的程序,还有 ...

  10. 静态-static

    静态 1. 静态初始化块 (1)可初始化类的静态字段 (2)静态初始化块只调用一次 (3)创建子类时会调用父类的静态初始化块 源代码: class Root { static{ System.out. ...