-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_machine_code.c
172 lines (146 loc) · 3.76 KB
/
convert_to_machine_code.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "assembler.h"
/*
* printR: this function receive 5 parameters and return number
* the first parameter is the opcode
* the second parameter is the first par
* the third parameter is the second par
* the fourth parameter is the third par
* the fifth parameter is the number of funct
* this function convert given number to machinecode
*/
int printR(int opcode, int par1 ,int par2 ,int par3,int funct){
unsigned int flag;
unsigned int machine_code = 0;
/* flag = machine_code =32 bits */
flag = funct;
flag = flag << 6;
machine_code = machine_code |flag; /* funct */
flag= par3;
flag = flag << 11;
machine_code = machine_code |flag; /* rd */
flag= par2;
flag = flag << 16;
machine_code = machine_code |flag; /* rt */
flag= par1;
flag = flag << 21;
machine_code = machine_code |flag; /*rs */
flag= opcode;
flag = flag << 26;
machine_code = machine_code |flag; /*rs */
return machine_code ;
}
/*
* printI: this function receive 4 parameters and return number
* the first parameter is the opcode
* the second parameter is the first par
* the third parameter is the second par
* the fourth parameter is the number of immed
* this function convert given number to machine code
*/
int printI(int opcode, int par1 ,int par2 ,int immed){
unsigned int machine_code = 0;
unsigned int flag = immed;
/* immed */
machine_code =machine_code |flag;
machine_code = machine_code &0xFFFF;
/* rt */
flag= par2;
flag = flag << 16;
machine_code =machine_code |flag;
/*rs*/
flag= par1;
flag = flag << 21;
machine_code =machine_code |flag;
/*opcode*/
flag= opcode;
flag = flag << 26;
machine_code =machine_code |flag;
return machine_code ;
}
/*
* printJ: this function receive 3 parameters and return number
* the first parameter is the opcode
* the second parameter is the first par
* the third parameter is the second par
* this function convert given number to machine code
*/
int printJ(int opcode, int parOrAddress, int par1){
unsigned int machine_code = 0;
unsigned int flag = par1; /*address */
machine_code =machine_code |flag;
machine_code = machine_code &0x7FFF;
flag= parOrAddress; /* reg */
flag = flag << 25;
machine_code =machine_code |flag;
flag= opcode; /*opcode*/
flag = flag << 26;
machine_code =machine_code |flag;
return machine_code ;
}
/*
* iToOpcode: this function receive 1 parameters and return int
* the first parameter is the command number
* this function convert the command named i to opcode
*/
int iToOpcode(int i){
if(i>=0 && i<=4)
return 0;
if(i<=7)
return 1;
if(i==8)
return 10;
if(i==9)
return 11;
if(i==10)
return 12;
if(i==11)
return 13;
if(i==12)
return 14;
if(i==13)
return 15;
if(i==14)
return 16;
if(i==15)
return 17;
if(i==16)
return 18;
if(i==17)
return 19;
if(i==18)
return 20;
if(i==19)
return 21;
if(i==20)
return 22;
if(i==21)
return 23;
if(i==22)
return 24;
if(i==23)
return 30;
if(i==24)
return 31;
if(i==25)
return 32;
else
return 63;
}
/*
* iToFunct: this function receive 1 parameters and return int
* the first parameter is the command number
* this function convert the command named i to funct
*/
int iToFunct(int i){
if(i==0||i==5)
return 1;
if(i==1||i==6)
return 2;
if(i==2||i==7)
return 3;
if(i==3)
return 4;
if(i==4)
return 5;
return -1;
}