2013
11-11

# Simple Computers

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.

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.

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

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


10000111


//* @author:
import java.util.*;
import java.math.*;

public class Main {
static String []s = new String [32];
static int []memory = new int [32];
static int []ans = new int [32];
static int MAX = 256;

static void deal()
{
Arrays.fill(memory, 0);
int accu = 0, pc = 0, i = 0, j, k, pos;
String str;
while (true)
{
str = s[i].substring(0, 3);
pc = (pc + 1) % 32;
pos = 0;
for (j = 3; j < 8; j++)
pos = (pos < < 1) + (s[i].charAt(j) - '0');
if (str.compareTo("000") == 0)
{
if (accu == 0)
s[pos] = "00000000";
else
{
j = accu;
k = 0;
while (j != 0)
{
j >>= 1;
k++;
}
s[pos] = "";
for (j = 0; j < 8 - k; j++)
s[pos] += "0";
s[pos] += Integer.toBinaryString(accu);
}
}
else if (str.compareTo("001") == 0)
{
accu = 0;
for (j = 0; j < 8; j++)
accu = (accu < < 1) + (s[pos].charAt(j) - '0');
accu %= MAX;
}
else if (str.compareTo("010") == 0 && accu == 0)
pc = pos;
else if (str.compareTo("100") == 0)
{
accu--;
if (accu < 0)
accu += MAX;
}
else if (str.compareTo("101") == 0)
accu = (accu + 1) % MAX;
else if (str.compareTo("110") == 0)
pc = pos;
else if (str.compareTo("111") == 0)
break;
i = pc;
}
if (accu == 0)
System.out.println("00000000");
else
{
i = j = 0;
while (accu != 0)
{
ans[i++] = accu % 2;
accu >>= 1;
}
for (j = 0; j < 8 - i; j++)
System.out.print("0");
i--;
while (i >= 0)
{
System.out.print(ans[i]);
i--;
}
System.out.println();
}
}

static void input()
{
Scanner cin = new Scanner (System.in);
int i;
while (cin.hasNext())
{
for (i = 0; i < 32; i++)
s[i] = cin.next();
deal();
}
}

public static void main (String []args)
{
input();
}
}

1. 因为是要把从字符串s的start位到当前位在hash中重置，修改提交后能accept，但是不修改居然也能accept

2. 这道题目虽然简单，但是小编做的很到位，应该会给很多人启发吧！对于面试当中不给开辟额外空间的问题不是绝对的，实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟，今天看到小编这篇文章相见恨晚。