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 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; } }