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