TOJ-1342 Simple Computer

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



Source: University of Ulm Local Contest 2000

看到这道题让我想起了之前学系计算机组成原理设计微操作码的实验。只要对cpu的工作原理清楚,这道题理解起来就十分轻松,是很简化的模拟cpu的运行。

说一些做这道题的编程上的问题:

  char与unsigned char的区别:两个都占1字节,也就是8位,但是char的最高位是符号位,所以char的取值范围是-128~127,显而易见的unsigned

char范围是0~255,所以我们用unsigned char表示BYTE。

  <<和>>是移位运算,学过汇编会很容易理解,也可以当作乘或除以2的幂。&是与运算。所以,n<<2等同于n*2;n&0x1F等同于n%32。

这道题的输入上并不是输入完一组就结束,可能会有多组数据。

#include <iostream>
#include <stdio.h>
#include <string>   
using namespace std;
  
unsigned char memory[32];
unsigned char ir;//指令寄存器
unsigned char accu,pc;

int main(){
    int i,j;
    char s[10];
    while(1){
        i = 0;
        while(1){//输入存入内存 
            if(gets(s) == NULL) return 0;
            memory[i] = 0;
            for(j=0;j<8;j++){
                memory[i] = (memory[i]<<1) + (s[j]-'0');
            }
            i++;
            if(i==32) break;
        }
        pc = 0;
        accu = 0;//初始化程序计数器和累加器 
        while(1){
            ir = memory[pc++];    //取指令,计数器增1 
            pc &= 0x1F;    //计数器范围 00000~11111
            switch(ir>>5){//指令高3位是操作码 
                case 0:        memory[ir&0x1F] = accu; break;//指令低5为操作数地址,我开始忘了switch里break是跳出switch。。。 
                case 1:     accu = memory[ir&0x1F]; break;
                case 2:        if(accu==0) pc = ir&0x1F; break;
                case 3:        break;
                case 4:        accu--; break;
                case 5:        accu++; break;
                case 6:        pc = ir&0x1F;
                case 7:        break;
            }
            if((ir>>5)== 7) break;
        } 
        for(i=7;i>=0;i--){
            cout<<((accu>>i)&1) ? 1 : 0;
        }
        cout<<endl;
    }
    
} 

 

转载于:https://www.cnblogs.com/shenchuguimo/p/6379361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值