POJ1049 Microprocessor Simulation
2013-08-10 01:39
148 查看
题目来源: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条指令说明如下:
程序总是最先执行地址00处的指令,然后依次执行后面的指令直到遇到Stop指令。
下面的例子展示了一些片段程序并描述了它的作用。
输入:输入由若干行组成,每行256个16进制数字(1-9, A-F).每行表示的是内存的内容,地址编码从00到FF。00地址的内存单元为Stop指令时标志着输入的结束。输入的程序保证了程序的指令不会位于地址F0到FF的内存单元中。
输出:对每个输入的内存块,模拟微处理器的执行,输出程序执行后的每个内存字,格式与输入一致。
Sample Input
Sample Output
标题已经告诉我们方法:模拟。关键是细心,比如数字的INC和DEC要注意从数字到字母的跳变,还有分清楚值和址。勾起了当年做组成原理课程设计的"美好回忆"。=。=
View Code
题目大意:
一种小型的微处理器有以下特性:
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. |
下面的例子展示了一些片段程序并描述了它的作用。
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. |
输出:对每个输入的内存块,模拟微处理器的执行,输出程序执行后的每个内存字,格式与输入一致。
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[257]; char A, B; int pc; int Hex2Dec(char c) { if (c >= '0' && c <= '9') return c - '0'; else return c - 'A' + 10; } int DHex2Dec(char a, char b) { return Hex2Dec(a) * 16 + Hex2Dec(b); } char Dec2Hex(int i) { if (i >= 10) return 'A' + i - 10; else return i + '0'; } int main(void) { while (scanf("%s", mem) && mem[0] != '8') { pc = 0; char command; char buf; int buf1, buf2; A = B = '0'; while ((command = mem[pc]) != '8') { switch (command) { case '0'://LD A = mem[DHex2Dec(mem[pc + 1], mem[pc + 2])]; pc += 3; break; case '1'://ST mem[DHex2Dec(mem[pc + 1], mem[pc + 2])] = A; pc += 3; break; case '2'://SWP buf = A; A = B; B = buf; ++pc; break; case '3'://ADD buf1 = Hex2Dec(A); buf2 = Hex2Dec(B); B = Dec2Hex((buf1 + buf2) / 16); A = Dec2Hex((buf1 + buf2) % 16); ++pc; break; case '4'://INC if (A == 'F') A = '0'; else if (A == '9') A = 'A'; else ++A; ++pc; break; case '5'://DEC if (A == '0') A = 'F'; else if (A == 'A') A = '9'; else --A; ++pc; break; case '6'://BZ if (A == '0') pc = DHex2Dec(mem[pc + 1], mem[pc + 2]); else pc += 3; break; case '7'://BR pc = DHex2Dec(mem[pc + 1], mem[pc + 2]); break; } } printf("%s\n", mem); } return 0; }
View Code
相关文章推荐
- POJ 1049 Microprocessor Simulation 已被翻译
- ZOJ 1072 Microprocessor Simulation
- ZOJ-1072 MicroprocessorSimulation
- 详解STM32F10x在.Net Micro Framework的Processor Properties的取值
- Difference Between Microprocessor and Microcontroller
- 详解STM32F10x在.Net Micro Framework的Processor Properties的取值
- Further Optimizations Exploiting the Microarchitecture of the Processor
- 详解STM32F10x在.Net Micro Framework的Processor Properties的取值
- History of Computer 计算机发展史 7 Great Moments in Microprocessor History
- Method and apparatus for loading a segment register in a microprocessor capable of operating in multiple modes
- DDJ Microprocessor Center
- During Barry's "Intel Microprocessor"
- Why does microprocessor contain ROM chips?
- EMBEDDED MICROPROCESSORS-Blackfin Processor Architecture Overview
- VLIW Microprocessor Hardware Design
- CSE 325 Embedded Microprocessor System Design
- Microservice architecture
- BeanFactoryPostProcessor和BeanPostProcessor的区别
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor
- micro httpd问题