zoukankan      html  css  js  c++  java
  • 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要注意从数字到字母的跳变,还有分清楚值和址。勾起了当年做组成原理课程设计的"美好回忆"。=。=

     1 //////////////////////////////////////////////////////////////////////////
     2 //        POJ1049 Microprocessor Simulation
     3 //        Memory: 164K        Time: 0MS
     4 //        Language: C++        Result: Accepted
     5 //////////////////////////////////////////////////////////////////////////
     6 
     7 #include <cstdio>
     8 
     9 using namespace std;
    10 
    11 char mem[257];
    12 char A, B;
    13 int pc;
    14 
    15 int Hex2Dec(char c) {
    16     if (c >= '0' && c <= '9') return c - '0';
    17     else return c - 'A' + 10;
    18 }
    19 
    20 int DHex2Dec(char a, char b) {
    21     return Hex2Dec(a) * 16 + Hex2Dec(b);
    22 }
    23 
    24 char Dec2Hex(int i) {
    25     if (i >= 10) return 'A' + i - 10;
    26     else return i + '0';
    27 }
    28 
    29 int main(void) {
    30     while (scanf("%s", mem) && mem[0] != '8') {
    31         pc = 0;
    32         char command;
    33         char buf;
    34         int buf1, buf2;
    35         A = B = '0';
    36         while ((command = mem[pc]) != '8') {
    37             switch (command) {
    38                 case '0'://LD
    39                     A = mem[DHex2Dec(mem[pc + 1], mem[pc + 2])];
    40                     pc += 3;
    41                     break;
    42                 case '1'://ST
    43                     mem[DHex2Dec(mem[pc + 1], mem[pc + 2])] = A;
    44                     pc += 3;
    45                     break;
    46                 case '2'://SWP
    47                     buf = A;
    48                     A = B;
    49                     B = buf;
    50                     ++pc;
    51                     break;
    52                 case '3'://ADD
    53                     buf1 = Hex2Dec(A);
    54                     buf2 = Hex2Dec(B);
    55                     B = Dec2Hex((buf1 + buf2) / 16);
    56                     A = Dec2Hex((buf1 + buf2) % 16);
    57                     ++pc;
    58                     break;
    59                 case '4'://INC
    60                     if (A == 'F') A = '0';
    61                     else if (A == '9') A = 'A';
    62                     else ++A;
    63                     ++pc;
    64                     break;
    65                 case '5'://DEC
    66                     if (A == '0') A = 'F';
    67                     else if (A == 'A') A = '9';
    68                     else --A;
    69                     ++pc;
    70                     break;
    71                 case '6'://BZ
    72                     if (A == '0') pc = DHex2Dec(mem[pc + 1], mem[pc + 2]);
    73                     else pc += 3;
    74                     break;
    75                 case '7'://BR
    76                     pc = DHex2Dec(mem[pc + 1], mem[pc + 2]);
    77                     break;
    78             }
    79         }
    80         printf("%s
    ", mem);
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    Windows批处理方式实现MySQL定期自动备份多个数据库
    MySQL数据库中库、表名、字段的大小写问题
    使用modbus4j通过串口解析modbus协议(java)
    mybatis —— 动态sql之if条件判断各种使用方式
    Mysql 获取表的comment 字段
    [转载]如何判断数据库,表或字段是否存在
    设计模式目录
    图片上传及访问
    实现第一个API
    ListView 源码解析
  • 原文地址:https://www.cnblogs.com/dengeven/p/3249503.html
Copyright © 2011-2022 走看看