acm.jlu.edu.cn-1099-Simple Computers

xiaoxiao2021-03-06  40

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 x001xxxxx LDA x load the value of Memory Byte X INTO THE ACCU010XXXX BEQ X if The value of the acid is 0 loading the value x oto the pc011 ----- NOP no Operation100 ----- Dec Subtract 1 from the accu101 ----- Inc Add 1 to the accu110xxxxx JMP x load the value x into the pc111 ----- HLT terminate programIn 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 SpecificationThe 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. INPU Test Case, Output ON A Line Test Case, Output ON A Line Test Case, Output ON A Line Test Case, Output ON A Line Test Case

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 Output10000111 # include

Using namespace std;

INT MEM [32];

Int INS;

Int OPE;

INT PC;

Int ac;

INT LoadMem (char * s)

{

INT D = 0;

For (int i = 0; i <8; i )

D = D * 2 S [I] - '0';

Return D;

}

Void loading

{

INS = (MEM [PC] & 224) >> 5;

}

void loading ()

{

OPE = (MEM [PC] & 31);

}

int main ()

{

Char s [9];

While (cin >> s)

{

MEM [0] = loadingMem (s);

For (int i = 1; i <32; i )

{

CIN >> S;

MEM [I] = loadingMem (s);

}

PC = 0; AC = 0;

Loadins ();

Loadope ();

PC ;

While (! (INS == 7)))))

{

Switch (INS)

{

Case 0:

MEM [OPE] = AC;

Break;

Case 1:

AC = MEM [OPE];

Break;

Case 2:

IF (AC == 0)

PC = OPE;

Break;

Case 3:

Break;

Case 4:

AC -

IF (ac == - 1)

AC = 255;

Break;

Case 5:

AC ;

IF (AC == 256)

AC = 0;

Break;

Case 6:

PC = OPE;

Break;

Case 7:

Goto L1;

DEFAULT:

;

}

Loadins ();

Loadope ();

PC ;

IF (PC == 32)

PC = 0;

}

L1: int R = 128;

For (int i = 0; i <8; i )

{

IF (AC> = R)

{

Cout << 1;

AC- = R;

}

Else

Cout << 0;

R / = 2;

}

Cout << Endl;

}

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-85893.html

New Post(0)