ZOJ-1098 Simple Computer solution report

Source: Internet
Author: User
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 norann 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:

000 xxxxx STA xStore the value of the accu into memory byte x
001 xxxxx LDA xLoad the value of memory byte x into the accu
010 xxxxx BEQ xIf the value of the accu is 0 load the value x into the pc
011 ----- NOPNo operation
100 ----- DECSubtract 1 from the accu
101 ----- INCAdd 1 to the accu
110 xxxxx JMP xLoad the value x into the pc
111 ----- HLTTerminate 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

 

A simple 8-bit computer, 32-byte memory unit, 8-bit accumulators, and 5-bit PC (32-bytes addressing capability ). Each byte in the memory has a high level of instruction code and a low level of five is the memory address. The instruction meaning is described in the preceding topic. The initial status of the 32-byte memory is given. The initial values of pc (program counter) and accu (accumulators) are 0, the computer must output The accumulators at the end of the code segment (output in binary mode ).

The problem is very basic, so the basic solution step is: parse the input, that is, convert a string like "11110000" into a corresponding number, then simulate the operation, and then output The accumulators value, that is, the value is converted into a string. The Code is as follows:

 

Code_1098
/* ZOL-1098 Simple Computers */
# Include <stdio. h>
# Include <string. h>

/* 32 bytes memory of the Computer */
Unsigned char memory [32];
/* Pc: program counter, program counter, that is, the address pointer when the program is running */
Unsigned char pc;
/* Accu: accumulator, that is, the accumulators */
Unsigned char accu;

/* One row (byte) read each time )*/
Char line [9];

/* Parse the value of one row of bytes read */
Unsigned char ParseFromLine (char * s)
{
Unsigned char result = 0;
Int base = 128;
While (* s)
{
Result + = (* s-'0') * base;
Base = (base> 1 );
S ++;
}
Return result;
}

/* Parse the value into a binary string and prepare to print it */
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 );
}
}

/* Run and return the value of the accumulators */
Unsigned char Run ()
{
/* Indicates the current command, the instruction code (three digits), and the address (five digits below )*/
Unsigned char instruction, code, addr;
/* Initialize */
Pc = 0;
Accu = 0;

/* Running cycle */
While (1)
{
/* Specify */
Instruction = memory [pc ++];
/* Once the pc has crossed the memory limit, it will be rolled back to 0. If this statement does not exist, it will be WA. This statement causes AC! */
If (pc> = 32) pc = 0;
Code = (instruction> 5 );
Addr = (instruction & 0x1f );

Switch (code)
{
/* 000 xxxxx STA x store the value of the accu into memory byte x */
Case 0:
Memory [addr] = accu;
Break;
/* 001 xxxxx LDA x load the value of memory byte x into the accu */
Case 1:
Accu = memory [addr];
Break;
/* 010 xxxxx 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;
/* 110 xxxxx 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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.