题目来源: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. 问题:oracle floor;结果:Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

    Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 (2011-04-06 16:10:35) 转载▼ 标签: 谈 分类: 渐行渐远 FLOOR——对给定的数字取 ...

  2. [Python Study Notes]饼状图绘制

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  3. 一些API的用法

    //1.init初始化 NSString * str1 = [[NSString alloc] init]; NSLog(@"str1 = %@",str1); //2.initW ...

  4. 剑指offer 34_丑数

    丑数:只有2 3 5 这三个因子的数,求前(第)1500个.习惯上我们把1当作第一个丑数 例如 6, 8是丑数.14不是. #include <stdio.h> int Min(int x ...

  5. AudioTrack

    AudioTrack 在Java应用中,管理和播放一个单一的语音资源 The AudioTrack class manages and plays a single audio resource fo ...

  6. Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法

    Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法(2011-02-23 19:37:32) 转载▼   今天在惠普服务器上安装数据库2008时, ...

  7. 如何设置Win10文件资源管理器默认打开“这台电脑”

    摘录自:http://www.ithome.com/html/win10/126066.htm

  8. day69-oracle 22-DBCA

    只涉及到数据库的管理,不涉及到数据库的开发.不涉及到写SQL程序或者是写增删改查,不涉及到这些东西,也不涉及到事务. 你在安装oracle的时候它自动帮你创建一个数据库.

  9. 使用Java读取JSON数据

    ----------------siwuxie095                         JSON 官网:http://www.json.org/     在官网页面的下方,是 JSON ...

  10. C++中的友元

    友元函数 在类的声明中可以声明某一个函数作为该类的友元函数,然后该函数就可以访问类中的private数据成员了. demo: /* wirten by qianshou 2013/12/6 04:13 ...