题目来源: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. Java enum(枚举)使用详解之四

    用法五:实现接口 所有的枚举都继承自java.lang.Enum类.由于Java 不支持多继承,所以枚举对象不能再继承其他类. public interface Behaviour { void pr ...

  2. oracle——存储过程分页

    1.包头: CREATE OR REPLACE PACKAGE BAWQ_PROC_PAGE IS -- BAWQ_PROC_PAGE 是包头名 TYPE T_CURSOR IS REF CURSOR ...

  3. 问题:HttpContext.Current.Session;结果:Session与HttpContext.Current.Session到底有什么区别呢?

    我在做练习的时候遇到了这样一个问题,在母版页页面中写入登录和密码修改的js代码,在登录的方法中写 入 HttpContext.Current.Session.Add("UserPwd&quo ...

  4. css代码结构

    整个文档结构如下: 一般性样式 主体样式 reset样式 链接 标题 其他元素 辅助样式 表单 通知和错误 一致的条目 页面结构 标题.页脚和导航 布局 其他页面结构元素 页面组件 各个页面 覆盖

  5. Alert---点击拍照弹出对话框

    /** * 照片对话框 *AlertDialog */ private void PhotoDialog() { AlertDialog.Builder builder = new Builder(m ...

  6. Node内存限制与垃圾回收

    对象分配 所有的JS对象都是通过堆来进行分配的.使用process.memoryUsage()查看使用情况Node.js 中文网文档 process.memoryUsage() { rss: , he ...

  7. IO流对文件的读取操作

    /*1. 在当前项目的根目录下有一个名为“info.txt”的文件,里面存放的内容如下(可手动创建录入,不需要使用IO流): 2. 利用IO流的知识读取info.txt文件的内容, 在控制台上打印大写 ...

  8. Spring 已看 没用

    注解 @Autwired 依赖注入  作用: 自动按照类型注入.当使用注解注入属性时,set方法可以省略.它只能注入其他bean类型.当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在 ...

  9. css选择器的一些说明

    标签选择器.ID选择器.类选择器 这三个很简单,没啥可说的. 子选择器得说一下. <ul class="food">    <li>水果        &l ...

  10. Luogu 3193 [HNOI2008]GT考试

    BZOJ1009 妙! 推荐这篇题解: https://www.luogu.org/blog/Edgration/solution-p3193 考虑设计dp,设$f_{i, j}$表示长串匹配到i,短 ...