ZOJ - 1098 Simple Computer 解题报告

Simple Computers


Time Limit: 1 Second      Memory Limit: 32768 KB


You are to write an interpreter for a simple computer. This computer uses a processor with a small number of machine instructions. Furthermore, it is equipped with 32 byte of memory, one 8-bit accumulator (accu) and a 5-bit program counter (pc). The memory contains data as well as code, which is the usual von Neumann architecture.

The program counter holds the address of the instruction to be executed next. Each instruction has a length of 1 byte - the highest 3 bits define the type of instruction and the lowest 5 bits define an optional operand which is always a memory address (xxxxx). For instructions that don't need an operand the lowest 5 bits have no meaning (-----). Here is a list of the machine instructions and their semantics:

000xxxxx   STA x   store the value of the accu into memory byte x
001xxxxx   LDA x   load the value of memory byte x into the accu
010xxxxx   BEQ x   if the value of the accu is 0 load the value x into the pc
011-----   NOP     no operation
100-----   DEC     subtract 1 from the accu
101-----   INC     add 1 to the accu
110xxxxx   JMP x   load the value x into the pc
111-----   HLT     terminate program

In the beginning, program counter and accumulator are set to 0. After fetching an instruction but before its execution, the program counter is incremented. You can assume that programs will terminate.

Input Specification

The input file contains several test cases. Each test case specifies the contents of the memory prior to execution of the program. Byte 0 through 31 are given on separate lines in binary representation. A byte is denoted by its highest-to-lowest bits. Input is terminated by EOF.

Output Specification

For each test case, output on a line the value of the accumulator on termination in binary representation, again highest bits first.

 

Sample Input

00111110
10100000
01010000
11100000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00111111
10000000
00000010
11000010
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
11111111
10001001

Sample Output

10000111

 

          题意简要说明:一个简单的8位计算机,32个字节的内存单元,8位累加器,5位PC(即可寻址能力为32 bytes)。每一个存储器中的byte的高三位是指令码,低五位是存储器地址。其指令含义如上面题目中描述。现在给出存储器的32个字节的初始状态,pc(程序计数器)和accu(累加器)初始值为0,要求输出计算机对上面的代码段运行结束时的累加器值(按照二进制方式输出)。

          题目很基本,因此基本解题步骤是:解析输入,即把类似“11110000”这样的字符串换算成相应的数字,然后模拟运行,然后在输出累加器值,即再把值换算成字符串。代码如下:

 

Code_1098
/* ZOL - 1098 Simple Computers */
#include <stdio.h>
#include <string.h>

/*计算机的32bytes内存*/
unsigned char memory[32];
/*pc: program counter, 程序计数器,即程序运行时的地址指针 */
unsigned char pc;
/*accu: accumulator, 即累加器*/
unsigned char accu;

/*每次读取的一行(byte)*/
char line[9];

/*解析读入的一行字节的值*/
unsigned char ParseFromLine(char* s)
{
    unsigned char result = 0;
    int base = 128;
    while(*s)
    {
        result += (*s-'0')*base;
        base = (base>>1);
        s++;
    }
    return result;
}

/*把值解析成二进制表达的字符串,准备打印*/
void ParseToLine(unsigned char number, char* buf)
{
    int i=7;
    memset(buf, '0', 8);
    buf[8]=0;/* NULL terminated */
    while(number)
    {
        buf[i--] = (number % 2 ) + '0';
        number = (number>>1);
    }
}

/*运行,返回累加器的值*/
unsigned char Run()
{
    /*分别是当前指令,指令码(高三位),地址(低五位)*/
    unsigned char instruction, code, addr;
    /*初始化*/
    pc=0;
    accu=0;    
    
    /*运行循环*/
    while(1)
    {
        /*取指*/
        instruction = memory[pc++];
        /*一旦pc越过内存界限,就回退到0。没有这个语句会WA,这个语句导致AC!*/
        if(pc>=32) pc=0;
        code = (instruction >> 5);
        addr = (instruction & 0x1f);
        
        switch(code)
        {
            /* 000xxxxx   STA x   store the value of the accu into memory byte x */
            case 0:
                memory[addr] = accu;
                break;
            /* 001xxxxx   LDA x   load the value of memory byte x into the accu */
            case 1:
                accu = memory[addr];
                break;
            /* 010xxxxx   BEQ x   if the value of the accu is 0 load the value x into the pc */
            case 2:
                if(accu == 0) pc=addr;
                break;
            /* 011-----   NOP     no operation */
            case 3:
                break;
            /* 100-----   DEC     subtract 1 from the accu */
            case 4:
                accu--;
                break;
            /* 101-----   INC     add 1 to the accu */
            case 5:
                accu++;
                break;
            /* 110xxxxx   JMP x   load the value x into the pc */
            case 6:
                pc=addr;
                break;
            /* 111-----   HLT     terminate program */
            case 7:
                return accu;
                break;
        }
    }
    return accu;
}

int main()
{
    int i=0;
    unsigned char result;
    while(scanf("%s",line)!=EOF)
    {
        memory[0]=ParseFromLine(line);
        for(i=1;i<32;i++)
        {
            scanf("%s",line);
            memory[i]=ParseFromLine(line);
        }
        result = Run();
        ParseToLine(result, line);
        printf("%s\n", line);
    }
    return 0;
}

时间: 2024-07-31 18:42:41

ZOJ - 1098 Simple Computer 解题报告的相关文章

ZOJ 1111 - Poker Hands 解题报告

          Poker Hands (比较两手牌的大小)           http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=111           题目描述:这道题要求比较两手牌的大小.每手牌都有5张牌组成,牌的大小的定义如下,首先确定这组牌的所属种类是下面哪一种(若同属同一种类,则根据该分类后面的比较规则继续比较,所属种类越靠后牌越大).           ● High Card:杂牌(不属于下面任何一种).

ZOJ 1205 - Martian Addition 解题报告

          http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1205           题目说明:(把题目从GOOGLE翻译的结果修改而来)           在22世纪,科学家们发现智能居民生活在火星.火星人非常喜欢数学.每一年,他们将举行一次火星算术大赛(计算机) ,竞赛内容是计算两个100位数的和,使用时间最少的人获得冠军.今年,他们还邀请地球上的人参加竞赛.            作为唯一代表地球,你发

ZOJ 1635 - Directory Listing 解题报告

       题目:1635 Directory Listing(列出目录)        http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=635         看描述好像是chenyue姐姐出的题目.这道题的大意是,给出一个UNIX文件系统的树,以及树节点的自身size,按要求列出它们,保持适当的缩进,并统计每个节点的总的size.输入的第一行代表根结点,每一个节点由name size或者*name size的形式组成(如

杭电ACM 2000-&amp;gt;2099 100道题 详细解题报告出炉

我去年暑假花了5天,把杭电ACM网站上2000到2099这100道题全AC了,又花了10来天精心写解题报告.里面包括题目.解题思路.编程技巧以及参考源码.所有代码都是使用C/C++写的. 最近整理资料时无意间发现,打包成chm文件和大家分享.我已经上传到CSDN上了.下载地址:http://download.csdn.net/source/492194 也可到我的Google Sites上下载. 题号 题名 题号 题名 2000 ASCII码排序 2001 计算两点间的距离 2002 计算球体积

ZOJ 3505. Yet Another Set of Numbers 解题报告

ZOJ 3505:Yet Another Set of Numbers 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3505   题意:有一个数字集合,集合中的数遵循以下规则:   ( 1 ). 每个数字的最高位不是 0 ; ( 2 ). 每个数字包含最多 N 位,且只有 0,1,2,3 这四个数字可能出现(0 < N < 20); ( 3 ). 每个数字的相邻位不同(例如:301是有效的,300不是); (

ZOJ 2529 - A+B in Hogwarts 解题报告

          题目2529:A+B in Hogwarts           链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1535           题目描述:哈利波特去的魔法学院使用一种特殊进制法表示数字:第i位用第i个素数为进制(radix),例如"个位"的进制为第一个素数2,"十位"的进制为第二个素数3,"百位"的进制为第三个素数5,...依此类推.例

ZOJ 1010. Area 解题报告

ZOJ 1010. Area. 题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 基本题意:此题目给出 n 个点的坐标 (xi,yi ) ,要求求出这些点按顺序围成的多边形的面积.同时要求检测出不合法的多边形(例如非相邻边彼此相交的情况).     此题求面积不难,样例很容易就过了,但是提交 10+ 次基本都是 WA ,几乎失去信心.做题最郁闷的就是你不知道问题到底出在什么地方,可能改了很多次都没有试对地

ZOJ 1090 - The Circumference of the Circle 解题报告

      题目的链接在这里:       http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1090       题目描述很简单,大意是,给出三个点的坐标,设为A(x1,y1),B (x2, y2),C (x3, y3),然后求出通过这三点的圆的周长(保留两位小数).但推导公式却比较麻烦,我是这样来做的.       首先根据同一个弦的圆心角角度相同,不难得出,圆周的直径d= BC/ sin a = AC/ sin b =

ZOJ 1009, 1115, 1476, 1733, 2405 解题报告

        星期天这天一口气AC了五道题,除了1009外基本都可算是简单题.        (1)1009 Enigma:        http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1009        题目是讲解二战期间德国使用的密码机Enigma.明文通过按键,通过转盘(rotor)转换为密文.如下图所示为有1个转盘,一共有6个字母的情况,每击键一次,转盘转动一格.如果含有多个转盘,则以类似数字进制方式转动,