POJ1049 Microprocessor Simulation
题目来源: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的更多相关文章
- POJ题目排序的Java程序
POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- Gate level Simulation(门级仿真)
1 什么是后仿真? 后仿真也成为时序仿真,门级仿真,在芯片布局布线后将时序文件SDF反标到网标文件上,针对带有时序信息的网标仿真称为后仿真. 2 后仿真是用来干嘛的? 检查电路中的timing vio ...
- fdtd simulation, plotting with gnuplot, writting in perl
# 9月13日 于成都黄龙溪 1 #!/usr/bin/perl # Author : Leon Email: yangli0534@gmail.com # fdtd simulation , plo ...
- 【转载】PMC/PEC Boundary Conditions and Plane Wave Simulation
原文链接 PMC/PEC Boundary Conditions and Plane Wave Simulation (FDTD) OptiFDTD now has options to use Pe ...
- dipole antenna simulation by CST
CST偶极子天线仿真,半波振子天线 一.本文使用CST仿真频率为1GHz的偶极子天线,使用2013版本.仿真的步骤为 1.选择一个CST的天线工程模板 2.设置好默认的单位 3.设置背景的材料(空气腔 ...
随机推荐
- pipeline(管道的连续应用)
# -*- coding: utf-8 -*- """ Created on Tue Aug 09 22:55:06 2016 @author: Administrato ...
- leetcode589
树的先序遍历,使用递归实现. class Solution { public: vector<Node> Tree; void preTree(Node node) { Tree.push ...
- URL与HTTP介绍
一.URL 1.基本介绍 URL的全称是Uniform Resource Locator(统一资源定位符) 通过1个URL,能找到互联网上唯一的1个资源 URL就是资源的地址.位置,互联网上的每个资源 ...
- pl/sql对excel数据的导入和导出
本来这部分是在上篇pl/sql的,但笔者介于此篇的内容,就独立出来了, 1.导出查询结果到excel文件,在查询结果上右键,然后弹出选择框如下: 2.从excel向数据库中导入数据: a.创建要导入的 ...
- java执行linux命令的工具类
package com.starfast.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ja ...
- 探究QA职能
测试人员一般是被外界普遍认为是QC,即对产品的质量进行检测,找出质量问题并配合相关人员解决问题,从而管控产品质量,说通俗点就是帮开发找漏洞,给开发擦屁股:如果线上出现bug,就是你没有测试完整,最累的 ...
- C++知识点总结(四)——面向对象的编程细节总结
1.空类的默认函数 一般情况下,对于任意一个类A,如果程序员不显示的声明和定义上述函数,C++编译器将会自动的为A产生4个public inline(公有.内联)的默认函数,这4个函数最常见的形式为: ...
- 利用JavaScriptCore实现以下简单的功能(平方和)
#import "RootViewController.h" #import <JavaScriptCore/JavaScriptCore.h> @interface ...
- Camera 3D概念
1. integration time即积分时间是以行为单位表示曝光时间(exposure time)的,比如说INT TIM为159,就是指sensor曝光时间为159行,两者所代表的意思是相同的, ...
- iOS应用推荐
RSS阅读器 Inoreader ***客户端 SuperWingy OpenWingy(已下架) 书签 Pocket 语音备忘录 Voice-Memos 编程语言学习 SoloLearn 社交 Tw ...