690 - I2P2015_homework Scoreboard

Time

2015/01/01 00:00:00 2015/01/01 00:00:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
10432 hw2 - instruction simulator
10445 hw3 - code translation 1
10446 hw3 - code translation 2
10447 hw3 - code translation 3

10432 - hw2 - instruction simulator   

Description

 The x97 CPU has 4 32 bits registers, r0, r1, r2, and r3, whose initial values are all zeros. The CPU has 2 kinds of instructions, MOV and ADD, 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 constant C, and save the result to register 1.

4.      ADD , : add the value of register 1 and the value of register 2, and save the result to register 1.

 

  

Hint:

You can use the following code to get the input and to print the output.

#include

#include

#include

enum code {MOV, ADD};

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;

} INST;

/* read input from stdin. */

INST* readInput(){

    char opc[10], op1[10], op2[10];

    int ns=0, c;

    INST *head=0, *curr=0;

    ns = scanf("%s %s %d ", opc, op1, &c);

    while(ns > 0) {

        /* allocate a storage for an instruction */

        if (head == 0){

            head = (INST*) malloc(sizeof(INST));

            curr = head;

            curr->next = 0;

        } else {

            curr->next = (INST*) malloc(sizeof(INST));

            curr = curr->next;

            curr->next = 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;

        /* Since op1 is always a register, read its id.*/

        curr->op1 = REG;

        curr->operand1_value = op1[1]-'0';

        /* read operand 2.*/

        if (ns == 2){  /* operand 2 is a register*/

            scanf("%s ", op2);

            curr->op2 = REG;

            curr->operand2_value = op2[1] -'0';

        } else if (ns == 3) { /* operand 2 is a constant.*/

            curr->op2 = CONST;

            curr->operand2_value = c;

        }

        /* get next one*/

        ns = scanf("%s %s %d ", opc, op1, &c);

    }

    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




10445 - hw3 - code translation 1   

Description

 We have 4 normal registers, r0, r1, r2, r3, whose initial values are all zeros, and one spe- cial register, sp, which stores stack pointer. Be careful, we cannot change the value in sp register. This CPU has 5 kinds of instructions, MOV, ADD, CMP, JMP, JLE, whose formats are specified as follows.

 
: Any register, r0, r1, r2, r3
: A memory address. For example, [sp+8]. Be careful, there is no space between ‘[’ and ‘]’.
: A constant value
 
MOV , will copy the value stored in the latter to the former
 
MOV ,
 
MOV ,
 
MOV ,
 
MOV ,
 
ADD , will add the value stored in the latter to the former
 
ADD ,
 
ADD ,
 
ADD ,
 
ADD ,
 
CMP ,
 
CMP ,
 
CMP ,
 
CMP ,
 
JMP
 
JLE  
 
Please fill in the following assembly code which is a translation of the following c code using these introduced instructions and store variable “a” in r0, variable “b” in r1.
 
c code :
 
int main() {
int a, b;
a=1;
b=2;
b = a+b+1; 
}
 
assembly code :
 
MOV r0, 1
MOV r1, 2
/* something missing */
ADD r1, 1
 
 

Input

There is no input.

Output

Please print the complete assembly code and use minimal instructions.

Sample Input  Download

Sample Output  Download

Tags




Discuss




10446 - hw3 - code translation 2   

Description

We have 4 normal registers, r0, r1, r2, r3, whose initial values are all zeros, and one spe- cial register, sp, which stores stack pointer. Be careful, we cannot change the value in sp register. This CPU has 5 kinds of instructions, MOV, ADD, CMP, JMP, JLE, whose formats are specified as follows.

 
: Any register, r0, r1, r2, r3
: A memory address. For example, [sp+8]. Be careful, there is no space between ‘[’ and ‘]’.
: A constant value
 
MOV , will copy the value stored in the latter to the former
 
MOV ,
 
MOV ,
 
MOV ,
 
MOV ,
 
ADD , will add the value stored in the latter to the former
 
ADD ,
 
ADD ,
 
ADD ,
 
ADD ,
 
CMP ,
 
CMP ,
 
CMP ,
 
CMP ,
 
JMP
 
JLE  
 
Please fill in the following assembly code which is a translation of the following c code using these introduced instructions and store variable “a” in r0, variable “b” in r1, variable "c" in r2, variable "d" in r3.

c code : 
int main(){
int a=10, b=20, c=30, d=40;
while(a<=d){
if(b<=c){
  a++;
b++;
}
else if(a<=b) a++;
else a+=2;
}
}

assembly code : 

MOV r0, 10
MOV r1, 20
MOV r2, 30
MOV r3, 40
JMP L2
L3 :
CMP r1, r2
/* something missing */ 
CMP r0, r1
JLE L5
ADD r0, 2
JMP L2
L5 :
/* something missing */
JMP L2
L4 :
ADD r0, 1
ADD r1, 1
L2 :
/* something missing */

Input

 There is no input.

Output

 Please print the complete assembly code and use minimal instructions.

Sample Input  Download

Sample Output  Download

Tags




Discuss




10447 - hw3 - code translation 3   

Description

We have 4 normal registers, r0, r1, r2, r3, whose initial values are all zeros, and one spe- cial register, sp, which stores stack pointer. Be careful, we cannot change the value in sp register. This CPU has 5 kinds of instructions, MOV, ADD, CMP, JMP, JLE, whose formats are specified as follows.

 
: Any register, r0, r1, r2, r3
: A memory address. For example, [sp+8]. Be careful, there is no space between ‘[’ and ‘]’.
: A constant value
 
MOV , will copy the value stored in the latter to the former
 
MOV ,
 
MOV ,
 
MOV ,
 
MOV ,
 
ADD , will add the value stored in the latter to the former
 
ADD ,
 
ADD ,
 
ADD ,
 
ADD ,
 
CMP ,
 
CMP ,
 
CMP ,
 
CMP ,
 
JMP
 
JLE    

Please fill in the following assembly code which is a translation of the following c code using these introduced instructions and store variable “a” in r0, variable “b” in r1, variable "c" in r2, variable "d" in r3.
 
c code :
 
int main(){
int i,j,a,b,c,d;
a = 5;
b = 10;
c = 1;
d = 2;
for(i=0;i<=a;i++){
for(j=0;j<=b;j++){
c++;
}
d++;
}
}
 
assembly code :
 
MOV [sp], 5
MOV [sp+4], 10
MOV [sp+8], 1
MOV [sp+12], 2
MOV r0, 0
JMP L2
L3 :
MOV r1, 0
JMP L4
L5 :
/* something missing */
ADD r1, 1
L4 :
CMP r1, [sp+4]
/* something missing */
ADD r0, 1
L2 :
/* something missing */
JLE L3
MOV r0, [sp]
MOV r1, [sp+4]
MOV r2, [sp+8]
MOV r3, [sp+12]

Input

 There is no input.

Output

  Please print the complete assembly code and use minimal instructions.

Sample Input  Download

Sample Output  Download

Tags




Discuss