699 - I2P(II)2015_Lee_Lab2 Scoreboard

Time

2015/03/11 08:20:00 2015/03/11 09:40:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
10443 instruction simulator 3

10443 - instruction simulator 3   

Description

 The x97 CPU has 4 32 bits registers, r0, r1, r2, and r3, whose initial values are all zeros. 

The CPU has four kinds of instructions, MOV, ADD, CMP, and JMP, whose formats are specified as follows.
1.  MOV , : assign constant C to register 1.
2.  MOV , : copy the content of register 2 to register 1.
3.  ADD , : add the value of register 1 and the value of register 2, and save the result to register 1.
4.  CMP , : compare the value of register 1 and the value of register 2. 
                                     If register1 is equal to register 2, setting register 1 to 0, otherwise, setting to -1.
5.  JMP                : if the value in register 1 is equal to 0, jump to the label. Otherwise do nothing.
 
  
Hint:
You can use the following code to get the input and to print the output.
It will use a struct, called INST, to record the opcode and its operands of an instruction.
Most of the instruction has a pointer, "next", pointing to the next instruction.
If the intruction is "JMP", it has another pointer, called "jump", pointing the the instruction after the label.  
So when executing the JMP instruction, you need to follow the "jump" pointer if the value in the register is 0;
and following the "next" pointer if the value in the register is not 0. 
There is at most one label in input.
===============================================================================
#include
#include
#include
 
enum code {MOV, ADD, CMP, JMP};
enum op_type {REG, CONST};
 
typedef struct INST
{
enum code opcode;
enum op_type op1;
int operand1_value;
enum op_type op2;
int operand2_value;
struct INST* next;
struct INST* jump;
} INST;
 
 
/* read input from stdin. */
 
INST* readInput()
{
char opc[10], op1[10], op2[10];
int isLabel=0, c;
INST *head=NULL, *curr=NULL, *label=NULL;
 
while( scanf("%s", opc) == 1)
{
/* allocate a storage for an instruction */
if (head == NULL)
{
head = (INST*) malloc(sizeof(INST));
curr = head;
curr->next = NULL;
curr->jump = NULL;
}
else
{
curr->next = (INST*) malloc(sizeof(INST));
curr = curr->next;
curr->next = NULL;
curr->jump = NULL;
}
 
/* if this INST is label, record it*/
if(isLabel==1){
label = curr;
isLabel = 0;
}
 
/* set the content of the instruction.*/
/* opcode */
if (strcmp(opc, "MOV")==0)
curr->opcode = MOV;
else if (strcmp(opc, "ADD")==0)
curr->opcode = ADD;
else if (strcmp(opc, "CMP")==0)
curr->opcode = CMP;
else if (strcmp(opc, "JMP")==0)
curr->opcode = JMP;
else
{ /*read a label*/
isLabel = 1;
continue;
}
 
/*According to opcode to read operands*/
if( curr->opcode == JMP)
{
/* Since op1 is always a register, read its id.*/
scanf("%s", op1);
curr->op1 = REG;
curr->operand1_value = op1[1]-'0';
 
curr->op2 = CONST;
curr->operand2_value = 0;
 
curr->jump = label;
}
else
{
/* Since op1 is always a register, read its id.*/
scanf("%s", op1);
curr->op1 = REG;
curr->operand1_value = op1[1]-'0';
 
/* read operand 2.*/
if(scanf("%d", &c)==1)
{
curr->op2 = CONST;
curr->operand2_value = c;
}
else
{
scanf("%s ", op2);
curr->op2 = REG;
curr->operand2_value = op2[1] -'0';
}
}
}
return head;
}
===============================================================================
 

Input

The input is a list of instructions.

Output

The output is the values of all register in the order of r0, r1, r2, and r3.

Use %4d for each resgister and print a newline in the end.

Sample Input  Download

Sample Output  Download

Tags




Discuss