Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1286234
  • 博文数量: 196
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2019年(31)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-12 15:48:26

Simple Computer

Time Limit:1sMemory limit:32M
Accepted Submit:120Total Submit:333

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




#include "stdio.h"

int str_to_digit(char *str)
{
    int i, val;

    val = 0;
    for (i = 0; i < 8; ++i)
        val = val * 2 + str[i] - '0';

    return val;
}

int output_accu(int accu)
{
    int i = 0;
    int a = 0x80;
    
    for (i = 0; i < 8; ++i)
    {
        if (a & accu)
            printf("1");
        else
            printf("0");
        a >>= 1;
    }
    printf("\n");

    return 0;
}

int main()
{
    int i, hlt;
    int binary[32], v_pc, accu;
    char str[10];

    while (scanf ("%s", str) != EOF)
    {
        binary[0] = str_to_digit(str);
        for (i = 1; i <= 31; ++i)
        {
            scanf ("%s", str);
            binary[i] = str_to_digit(str);
        }

        hlt = 1;
        v_pc = 0;
        accu = 0;
        while(hlt)
        {
            switch(binary[v_pc++] & 0xE0)
            {
            case 0x00: /* STA */
                binary[binary[v_pc - 1] & 0x1F] = accu;
                break;
            case 0x20: /* LDA */
                accu = binary[binary[v_pc - 1] & 0x1F];
                break;
            case 0x40: /* beq */
                if (accu == 0)
                    v_pc = binary[v_pc - 1] & 0x1F;
                break;
            case 0x60: /* nop */
                break;
            case 0x80: /* dec */
                if (accu == 0)
                    accu = 0xFF;
                else
                    --accu;
                break;
            case 0xA0: /* inc */
                accu++;
                accu &= 0xFF;
                break;
            case 0xC0: /* jmp */
                v_pc = binary[v_pc - 1] & 0x1F;
                break;
            case 0xE0: /* hlt */
                hlt = 0;
                break;
            }
            v_pc &= 0x1F;
        }

        output_accu(accu);
    }

    return 0;
}

阅读(1280) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~