概述
MicroprocessorSimulation
Time Limit: 2 Seconds MemoryLimit: 65536 KB
Problem
Consider a small microprocessor that has the following properties:
Each word is four bits.
Addresses are two words. The high word always comes first. That is, the highword of a two-word address will always occupy the lower word of memory.
Memory is 256 words.
There are two accumulators, A and B, each storing one word.
There are nine instruction codes. Each instruction requires at least one wordto store the code that specifies the instruction. Four instructions havearguments and require an additional two words.
Each 4 bit number can have the values from 0 to 15, inclusive, in base 10. Wewill write these using hexadecimal in the usual way, i.e. A means 10, B means11, etc.
These are the nine instructions:
Code | Words | Description |
0 | 3 | LD: Load accumulator A with the contents of memory at the specified argument. |
1 | 3 | ST: Write the contents of accumulator A to the memory location specified by the argument. |
2 | 1 | SWP: Swap the contents of accumulators A and B. |
3 | 1 | ADD: Add the contents of accumulators A and B. The low word of the sum is stored in A, and the high word in B. |
4 | 1 | INC: Increment accumulator A. Overflow is allowed; that is, incrementing F yields 0. |
5 | 1 | DEC: Decrement accumulator A. Underflow is allowed; that is, decrementing 0 yields F. |
6 | 3 | BZ: If accumulator A is zero, the next command to be executed is at the location specified by the argument. If A is not zero, the argument is ignored and nothing happens. |
7 | 3 | BR: The next command to be executed is at the location specified by the argument. |
8 | 1 | STP: Stop execution of the program. |
The microprocessoralways begins by executing the command at location 00. It executes the commandsin sequence until it reaches the Stop command.
The examples below show partial programs and describe their affect.
Program | Description |
01A8 | Load accumulator A with the contents of memory location 1A (26 in decimal) and stop. |
01A512F8 | Load accumulator A with the contents of memory location 1A (26 in decimal), decrement it, store the result to memory location 2F, then stop. |
The input willconsist of several lines of exactly 256 hex characters. Each line is thecontents of memory, beginning with address 00 and ending with address FF. Theend of the input is indicated by a memory state that has a stop instruction (an��8��) at address 00.The input programs will never ��fall of the end of memory��, that is, you will never execute aninstruction that is located between addresses F0 and FF, inclusive.
Output
For each memory state, you should simulate execution beginning with address 00.When the stop instruction is reached, you will dump the contents of memory tothe output as a single string of 256 hex characters followed by a newlinecharacter.
Examples
This program reads two one-word numbers from 10 and 11, and stores the two-wordsum at 12 and 13.
0102011311321128FF0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
The correct output is:
0102011311321128FF1E00000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
This program computes n2, where n is stored at 40, and stores the two-wordresult at FE and FF.
040563B14004220FF31FF041320FE31FE00C2042314200032041314170080000
F030000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000001
The correct output is:
040563B14004220FF31FF041320FE31FE00C2042314200032041314170080000
11F0000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000E
注意输入输出的格式问题
#include <stdio.h>
#include <string.h>
int a, b, t;
char mem[300];
int toDigits(char);
char toChar(int);
int process(char command);
int main()
{
int i, j;
char str[300];
while(1) {
t = 0;
while(t < 256){
gets(str);
for(j = 0; j < strlen(str); j++) {
mem[t++] = str[j];
}
}
if(mem[0] == '8') {
break;
}
for(t = 0; t < 256;) {
if(process(mem[t]) == -1) {
break;
}
}
for(i = 0; i < 256; i++) {
printf("%c", mem[i]);
}
printf("n");
}
return 0;
}
int process(char command)
{
int locate, temp, sum;
switch(command-'0') {
case 0:
locate = toDigits(mem[t+1]) * 16 + toDigits(mem[t+2]);
t += 3;
a = toDigits(mem[locate]);
break;
case 1:
locate = toDigits(mem[t+1]) * 16 + toDigits(mem[t+2]);
t += 3;
mem[locate] = toChar(a);
break;
case 2:
temp = a;
a = b;
b = temp;
t++;
break;
case 3:
sum = a + b;
b = sum / 16;
a = sum % 16;
t++;
break;
case 4:
a += 1;
if(a == 16) {
a = 0;
}
t++;
break;
case 5:
a -= 1;
if(a == -1) {
a = 15;
}
t++;
break;
case 6:
if(a == 0) {
locate = toDigits(mem[t+1]) * 16 + toDigits(mem[t+2]);
t = locate;
} else {
t += 3;
}
break;
case 7:
locate = toDigits(mem[t+1]) * 16 + toDigits(mem[t+2]);
t = locate;
break;
case 8:
return -1;
break;
}
return 0;
}
int toDigits(char c)
{
if(c >= '0' && c <= '9') {
return c - '0';
} else {
return c - 'A' + 10;
}
}
char toChar(int d)
{
if(d >= 0 && d <= 9) {
return d + '0';
} else {
return d - 10 + 'A';
}
}
最后
以上就是优美发夹为你收集整理的ZOJ-1072 MicroprocessorSimulation的全部内容,希望文章能够帮你解决ZOJ-1072 MicroprocessorSimulation所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复