-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.c
133 lines (131 loc) · 6.28 KB
/
part1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h> // for stderr
#include <stdlib.h> // for exit()
#include "types.h"
void decode_instruction(Instruction instruction) {
/* YOUR CODE HERE: COMPLETE THE SWITCH STATEMENTS */
switch(instruction.opcode) {
case 0x0: // opcode == 0x0 (SPECIAL)
switch(instruction.rtype.funct) {
case 0x00: //tested
printf("sll\t$%d,$%d,%u\n",instruction.rtype.rd,instruction.rtype.rt,instruction.rtype.shamt);
break;
case 0x02: //tested
printf("srl\t$%d,$%d,%u\n",instruction.rtype.rd,instruction.rtype.rt,instruction.rtype.shamt);
break;
case 0x03:
printf("sra\t$%d,$%d,%u\n",instruction.rtype.rd,instruction.rtype.rt,instruction.rtype.shamt);
break;
case 0x08:
printf("jr\t$%d\n",instruction.rtype.rs);
break;
case 0x09:
printf("jalr\t$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs);
break;
case 0xc: // funct == 0xc (SYSCALL) //tested
printf("syscall\n");
break;
case 0x10:
printf("mfhi\t$%d\n",instruction.rtype.rd);
break;
case 0x12:
printf("mflo\t$%d\n",instruction.rtype.rd);
break;
case 0x18:
printf("mult\t$%d,$%d\n",instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x19:
printf("multu\t$%d,$%d\n",instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x21:
printf("addu\t$%d,$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x23:
printf("subu\t$%d,$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x24: // funct == 0x24 (AND)
printf("and\t$%d,$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x25:
printf("or\t$%d,$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x26:
printf("xor\t$%d,$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x27:
printf("nor\t$%d,$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x2a: //tested
printf("slt\t$%d,$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs,instruction.rtype.rt);
break;
case 0x2b:
printf("sltu\t$%d,$%d,$%d\n",instruction.rtype.rd,instruction.rtype.rs,instruction.rtype.rt);
break;
default: // undefined funct
fprintf(stderr,"%s: illegal function: %08x\n",__FUNCTION__,instruction.bits);
exit(-1);
break;
}
break;
case 0x2: // opcode == 0x2 (J) tested
printf("j\t0x%x\n",instruction.jtype.addr*4);
break;
case 0x3: //tested
printf("jal\t0x%x\n",instruction.jtype.addr*4);
break;
case 0x4: //tested
printf("beq\t$%d,$%d,%d\n",instruction.itype.rs,instruction.itype.rt,(sHalf)instruction.itype.imm*4);
break;
case 0x5: //tested
printf("bne\t$%d,$%d,%d\n",instruction.itype.rs,instruction.itype.rt,(sHalf)instruction.itype.imm*4);
break;
case 0x9: //tested
printf("addiu\t$%d,$%d,%d\n",instruction.itype.rt,instruction.itype.rs,(sHalf)instruction.itype.imm);
break;
case 0xa:
printf("slti\t$%d,$%d,%d\n",instruction.itype.rt,instruction.itype.rs,(sHalf)instruction.itype.imm);
break;
case 0xb:
printf("sltiu\t$%d,$%d,%d\n",instruction.itype.rt,instruction.itype.rs,(sHalf)instruction.itype.imm);
break;
case 0xc:
printf("andi\t$%d,$%d,0x%x\n",instruction.itype.rt,instruction.itype.rs,instruction.itype.imm);
break;
case 0xd: // opcode == 0xd (ORI)
printf("ori\t$%d,$%d,0x%x\n",instruction.itype.rt,instruction.itype.rs,instruction.itype.imm);
break;
case 0xe:
printf("xori\t$%d,$%d,0x%x\n",instruction.itype.rt,instruction.itype.rs,instruction.itype.imm);
break;
case 0xf: //tested
printf("lui\t$%d,0x%x\n",instruction.itype.rt,instruction.itype.imm);
break;
case 0x20:
printf("lb\t$%d,%d($%d)\n",instruction.itype.rt,(sHalf)instruction.itype.imm,instruction.itype.rs);
break;
case 0x21:
printf("lh\t$%d,%d($%d)\n",instruction.itype.rt,(sHalf)instruction.itype.imm,instruction.itype.rs);
break;
case 0x23: //tested
printf("lw\t$%d,%d($%d)\n",instruction.itype.rt,(sHalf)instruction.itype.imm,instruction.itype.rs);
break;
case 0x24:
printf("lbu\t$%d,%d($%d)\n",instruction.itype.rt,(sHalf)instruction.itype.imm,instruction.itype.rs);
break;
case 0x25:
printf("lhu\t$%d,%d($%d)\n",instruction.itype.rt,(sHalf)instruction.itype.imm,instruction.itype.rs);
break;
case 0x28:
printf("sb\t$%d,%d($%d)\n",instruction.itype.rt,(sHalf)instruction.itype.imm,instruction.itype.rs);
break;
case 0x29:
printf("sh\t$%d,%d($%d)\n",instruction.itype.rt,(sHalf)instruction.itype.imm,instruction.itype.rs);
break;
case 0x2b: //tested
printf("sw\t$%d,%d($%d)\n",instruction.itype.rt,(sHalf)instruction.itype.imm,instruction.itype.rs);
break;
default: // undefined opcode
fprintf(stderr,"%s: illegal instruction: %08x\n",__FUNCTION__,instruction.bits);
exit(-1);
break;
}
}