MIPS架构下的MCU,指令集包含R-Type、I-Type、J-Type三种,在数电课程设计时为了给MCU编写指令集,需要将汇编语言转化成机器代码,这里分享一下自己写的Matlab 的 GUI。
主函数 C2M
1 function varargout = C2M(varargin) 2 val = zeros(1,1); 3 4 % C2M MATLAB code for C2M.fig 5 % C2M, by itself, creates a new C2M or raises the existing 6 % singleton*. 7 % 8 % H = C2M returns the handle to a new C2M or the handle to 9 % the existing singleton*. 10 % 11 % C2M('CALLBACK',hObject,eventData,handles,...) calls the local 12 % function named CALLBACK in C2M.M with the given input arguments. 13 % 14 % C2M('Property','Value',...) creates a new C2M or raises the 15 % existing singleton*. Starting from the left, property value pairs are 16 % applied to the GUI before C2M_OpeningFcn gets called. An 17 % unrecognized property name or invalid value makes property application 18 % stop. All inputs are passed to C2M_OpeningFcn via varargin. 19 % 20 % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 21 % instance to run (singleton)". 22 % 23 % See also: GUIDE, GUIDATA, GUIHANDLES 24 25 % Edit the above text to modify the response to help C2M 26 27 % Last Modified by GUIDE v2.5 08-Jun-2018 13:07:05 28 29 % Begin initialization code - DO NOT EDIT 30 gui_Singleton = 1; 31 gui_State = struct('gui_Name', mfilename, ... 32 'gui_Singleton', gui_Singleton, ... 33 'gui_OpeningFcn', @C2M_OpeningFcn, ... 34 'gui_OutputFcn', @C2M_OutputFcn, ... 35 'gui_LayoutFcn', [] , ... 36 'gui_Callback', []); 37 if nargin && ischar(varargin{1}) 38 gui_State.gui_Callback = str2func(varargin{1}); 39 end 40 41 if nargout 42 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 43 else 44 gui_mainfcn(gui_State, varargin{:}); 45 end 46 % End initialization code - DO NOT EDIT 47 48 49 % --- Executes just before C2M is made visible. 50 function C2M_OpeningFcn(hObject, eventdata, handles, varargin) 51 % This function has no output args, see OutputFcn. 52 % hObject handle to figure 53 % eventdata reserved - to be defined in a future version of MATLAB 54 % handles structure with handles and user data (see GUIDATA) 55 % varargin command line arguments to C2M (see VARARGIN) 56 57 % Choose default command line output for C2M 58 handles.output = hObject; 59 60 % Update handles structure 61 guidata(hObject, handles); 62 63 % UIWAIT makes C2M wait for user response (see UIRESUME) 64 % uiwait(handles.figure1); 65 66 67 % --- Outputs from this function are returned to the command line. 68 function varargout = C2M_OutputFcn(hObject, eventdata, handles) 69 % varargout cell array for returning output args (see VARARGOUT); 70 % hObject handle to figure 71 % eventdata reserved - to be defined in a future version of MATLAB 72 % handles structure with handles and user data (see GUIDATA) 73 74 % Get default command line output from handles structure 75 varargout{1} = handles.output; 76 77 78 79 function edit1_Callback(hObject, eventdata, handles) 80 % hObject handle to edit1 (see GCBO) 81 % eventdata reserved - to be defined in a future version of MATLAB 82 % handles structure with handles and user data (see GUIDATA) 83 84 % Hints: get(hObject,'String') returns contents of edit1 as text 85 % str2double(get(hObject,'String')) returns contents of edit1 as a double 86 87 88 % --- Executes during object creation, after setting all properties. 89 function edit1_CreateFcn(hObject, eventdata, handles) 90 % hObject handle to edit1 (see GCBO) 91 % eventdata reserved - to be defined in a future version of MATLAB 92 % handles empty - handles not created until after all CreateFcns called 93 94 % Hint: edit controls usually have a white background on Windows. 95 % See ISPC and COMPUTER. 96 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 97 set(hObject,'BackgroundColor','white'); 98 end 99 100 101 102 function edit2_Callback(hObject, eventdata, handles) 103 % hObject handle to edit2 (see GCBO) 104 % eventdata reserved - to be defined in a future version of MATLAB 105 % handles structure with handles and user data (see GUIDATA) 106 107 % Hints: get(hObject,'String') returns contents of edit2 as text 108 % str2double(get(hObject,'String')) returns contents of edit2 as a double 109 110 111 % --- Executes during object creation, after setting all properties. 112 function edit2_CreateFcn(hObject, eventdata, handles) 113 % hObject handle to edit2 (see GCBO) 114 % eventdata reserved - to be defined in a future version of MATLAB 115 % handles empty - handles not created until after all CreateFcns called 116 117 % Hint: edit controls usually have a white background on Windows. 118 % See ISPC and COMPUTER. 119 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 120 set(hObject,'BackgroundColor','white'); 121 end 122 123 124 % --- Executes on button press in pushbutton1. 125 function pushbutton1_Callback(hObject, eventdata, handles) 126 global val 127 % hObject handle to pushbutton1 (see GCBO) 128 % eventdata reserved - to be defined in a future version of MATLAB 129 % handles structure with handles and user data (see GUIDATA) 130 131 str1 = get(handles.edit2, 'String'); %?????? 132 133 x = size(str1, 2); %???????? 134 y = size(str1, 1); %???????? 135 %b = zeros(y, x); %b?????????????????? 136 137 for i = 1 : y 138 139 opcode1 = str1(i,1); 140 opcode2 = strcat(str1(i,1), str1(i,2)); 141 opcode3 = strcat(str1(i,1), str1(i,2), str1(i,3)); 142 opcode4 = strcat(str1(i,1), str1(i,2), str1(i,3), str1(i,4)); 143 rd = strcat(str1(i, 6), str1(i, 7)); 144 rs = strcat(str1(i,11), str1(i,12)); 145 rt = strcat(str1(i,16), str1(i,17)); 146 147 if (x >= 19) 148 imm_sh = strcat(str1(i,15), str1(i,16), str1(i,17), str1(i, 18), str1(i, 19)); 149 end 150 rt4 = strcat(str1(i,7), str1(i,8)); 151 rs4 = strcat(str1(i,12), str1(i,13)); 152 if (x >= 20) imm4 = strcat(str1(i,16), str1(i,17), str1(i, 18), str1(i, 19), str1(i, 20)); 153 end 154 155 rd2 = strcat(str1(i, 5), str1(i, 6)); 156 rs2 = strcat(str1(i,10), str1(i,11)); 157 rt2 = strcat(str1(i,15), str1(i,16)); 158 rt_w = strcat(str1(i, 5), str1(i, 6)); 159 imm_w = strcat(str1(i, 9), str1(i, 10), str1(i, 11), str1(i, 12), str1(i, 13)); 160 rs_w = strcat(str1(i, 16), str1(i, 17)); 161 162 switch opcode4 163 case 'addi' 164 b(i,1)=0;b(i,2)=0;b(i,3)=1;b(i,4)=0;b(i,5)=0;b(i,6)=0; 165 b(i, 7:11) = rig_f(rs4); b(i, 12:16) = rig_f(rt4); 166 b(i, 17:32) = rig_n(imm4); 167 bq(i,:) = 'this is adi'; 168 %bqq(i,:) = strcat('this is addi ', rs4 , '-', rt4 , '-',imm4); 169 otherwise 170 switch opcode3 171 case 'add' 172 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 173 b(i,27)=1;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=0;b(i,32)=0; 174 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 175 bq(i,:) = 'this is add'; 176 %bqq(i,:) = strcat('this is add, ', rs , '-', rt , '-',rd); 177 case 'sub' 178 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 179 b(i,27)=1;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=1;b(i,32)=0; 180 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 181 bq(i,:) = 'this is sub';bqq(i,:) = strcat('this is sub, ', rs , '-', rt , '-',rd); 182 case 'and' 183 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 184 b(i,27)=1;b(i,28)=0;b(i,29)=0;b(i,30)=1;b(i,31)=0;b(i,32)=0; 185 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 186 bq(i,:) = 'this is and';bqq(i,:) = strcat('this is and, ', rs , '-', rt , '-',rd); 187 case 'slt' 188 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 189 b(i,27)=1;b(i,28)=0;b(i,29)=1;b(i,30)=0;b(i,31)=1;b(i,32)=0; 190 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 191 bq(i,:) = 'this is slt';bqq(i,:) = strcat('this is slt, ', rs , '-', rt , '-',rd); 192 case 'sll' 193 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 194 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=0;b(i,32)=0; 195 b(i, 7:11) = 0; b(i, 12:16) = rig_f(rs); b(i, 17:21) = rig_f(rd); b(i, 22:26) = rig_sh(imm_sh); 196 bq(i,:) = 'this is sll'; 197 % bqq(i,:) = strcat('this is sll, ', rs , '-', rd , '-',imm_sh); 198 case 'srl' 199 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 200 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=1;b(i,32)=0; 201 b(i, 7:11) = 0; b(i, 12:16) = rig_f(rs); b(i, 17:21) = rig_f(rd); b(i, 22:26) = rig_sh(imm_sh); 202 bq(i,:) = 'this is srl'; 203 % bqq(i,:) = strcat('this is srl, ', rs , '-', rd , '-',imm_sh); 204 case 'sra' 205 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 206 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=1;b(i,32)=1; 207 b(i, 7:11) = 0; b(i, 12:16) = rig_f(rs); b(i, 17:21) = rig_f(rd); b(i, 22:26) = rig_sh(imm_sh); 208 bq(i,:) = 'this is sra'; 209 case 'beq' 210 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=1;b(i,5)=0;b(i,6)=0; 211 b(i, 7:11) = rig_f(rd); b(i, 12:16) = rig_f(rs); b(i, 17:32) = rig_n(imm_sh); 212 bq(i,:) = 'this is beq'; 213 %bqq(i,:) = strcat('this is beq, ', rd , '-', rs , '-',imm_sh); 214 case 'bne' 215 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=1;b(i,5)=0;b(i,6)=1; 216 b(i, 7:11) = rig_f(rd); b(i, 12:16) = rig_f(rs); b(i, 17:32) = rig_n(imm_sh); 217 bq(i,:) = 'this is bne'; 218 %bqq(i,:) = strcat('this is bne, ', rd , '-', rs , '-',imm_sh); 219 case 'mul' 220 b(i,1)=0;b(i,2)=1;b(i,3)=1;b(i,4)=1;b(i,5)=0;b(i,6)=0; 221 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=0;b(i,32)=1; 222 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 223 bq(i,:) = 'this is mul';bqq(i,:) = strcat('this is mul, ', rs , '-', rt , '-',rd); 224 case 'muf' 225 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 226 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=1;b(i,31)=0;b(i,32)=1; 227 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 228 bq(i,:) = 'this is mul';bqq(i,:) = strcat('this is mul, ', rs , '-', rt , '-',rd); 229 otherwise 230 switch opcode1 231 case 'j' 232 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=1;b(i,6)=0; 233 b(i, 7:32) = 9; 234 bq(i,:) = 'this is jrr';bqq(i,:) = strcat('this is j ', '66666666666666666666'); 235 otherwise 236 end 237 end 238 end 239 240 switch opcode2 241 case 'or' 242 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 243 b(i,27)=1;b(i,28)=0;b(i,29)=0;b(i,30)=1;b(i,31)=0;b(i,32)=1; 244 b(i, 7:11) = rig_f(rs2); b(i, 12:16) = rig_f(rt2); b(i, 17:21) = rig_f(rd2); b(i, 22:26) = 0; 245 bq(i,:) = 'this is orr'; 246 247 case 'lw' 248 b(i,1)=1;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=1;b(i,6)=1; 249 b(i, 7:11) = rig_f(rs_w);b(i, 12:16) = rig_f(rt_w); b(i, 17:32) = rig_n(imm_w); 250 251 % bqq(i,:) = strcat('this is lwr, ', rs_w, '-', rt_w, '-',imm_w); 252 bq(i,:) = 'this is lwr'; 253 254 case 'sw' 255 b(i,1)=1;b(i,2)=0;b(i,3)=1;b(i,4)=0;b(i,5)=1;b(i,6)=1; 256 b(i, 7:11) = rig_f(rs_w);b(i, 12:16) = rig_f(rt_w); b(i, 17:32) = rig_n(imm_w); 257 bq(i,:) = 'this is swr'; 258 otherwise 259 end 260 261 262 end 263 264 bb = num2str(b); 265 set(handles.edit1, 'String', bb); 266 set(handles.edit3, 'String', bq); 267 val = b; 268 269 % --- Executes on button press in pushbutton2. 270 function pushbutton2_Callback(hObject, eventdata, handles) 271 global val 272 % hObject handle to pushbutton2 (see GCBO) 273 % eventdata reserved - to be defined in a future version of MATLAB 274 % handles structure with handles and user data (see GUIDATA) 275 dlmwrite('C:\Users\ALIENWARE\Desktop\fft_test.txt', val, 'delimiter', '', 'precision', 6, 'newline','pc'); 276 277 278 279 function edit3_Callback(hObject, eventdata, handles) 280 % hObject handle to edit3 (see GCBO) 281 % eventdata reserved - to be defined in a future version of MATLAB 282 % handles structure with handles and user data (see GUIDATA) 283 284 % Hints: get(hObject,'String') returns contents of edit3 as text 285 % str2double(get(hObject,'String')) returns contents of edit3 as a double 286 287 288 % --- Executes during object creation, after setting all properties. 289 function edit3_CreateFcn(hObject, eventdata, handles) 290 % hObject handle to edit3 (see GCBO) 291 % eventdata reserved - to be defined in a future version of MATLAB 292 % handles empty - handles not created until after all CreateFcns called 293 294 % Hint: edit controls usually have a white background on Windows. 295 % See ISPC and COMPUTER. 296 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 297 set(hObject,'BackgroundColor','white'); 298 end 299 300 301 % --- Executes on button press in pushbutton3. 302 function pushbutton3_Callback(hObject, eventdata, handles) 303 % hObject handle to pushbutton3 (see GCBO) 304 % eventdata reserved - to be defined in a future version of MATLAB 305 % handles structure with handles and user data (see GUIDATA) 306 close(gcf);
函数rig_f 用来寻找名称对应的寄存器地址
1 function [q] = rig_f(a) 2 switch a 3 case '0_' 4 q(1)=0;q(2)=0;q(3)=0;q(4)=0;q(5)=0; 5 case 'at' 6 q(1)=0;q(2)=0;q(3)=0;q(4)=0;q(5)=1; 7 case 'v0' 8 q(1)=0;q(2)=0;q(3)=0;q(4)=1;q(5)=0; 9 case 'v1' 10 q(1)=0;q(2)=0;q(3)=0;q(4)=1;q(5)=1; 11 case 'a0' 12 q(1)=0;q(2)=0;q(3)=1;q(4)=0;q(5)=0; 13 case 'a1' 14 q(1)=0;q(2)=0;q(3)=1;q(4)=0;q(5)=1; 15 case 'a2' 16 q(1)=0;q(2)=0;q(3)=1;q(4)=1;q(5)=0; 17 case 'a3' 18 q(1)=0;q(2)=0;q(3)=1;q(4)=1;q(5)=1; 19 case 't0' 20 q(1)=0;q(2)=1;q(3)=0;q(4)=0;q(5)=0; 21 case 't1' 22 q(1)=0;q(2)=1;q(3)=0;q(4)=0;q(5)=1; 23 case 't2' 24 q(1)=0;q(2)=1;q(3)=0;q(4)=1;q(5)=0; 25 case 't3' 26 q(1)=0;q(2)=1;q(3)=0;q(4)=1;q(5)=1; 27 case 't4' 28 q(1)=0;q(2)=1;q(3)=1;q(4)=0;q(5)=0; 29 case 't5' 30 q(1)=0;q(2)=1;q(3)=1;q(4)=0;q(5)=1; 31 case 't6' 32 q(1)=0;q(2)=1;q(3)=1;q(4)=1;q(5)=0; 33 case 't7' 34 q(1)=0;q(2)=1;q(3)=1;q(4)=1;q(5)=1; 35 case 's0' 36 q(1)=1;q(2)=0;q(3)=0;q(4)=0;q(5)=0; 37 case 's1' 38 q(1)=1;q(2)=0;q(3)=0;q(4)=0;q(5)=1; 39 case 's2' 40 q(1)=1;q(2)=0;q(3)=0;q(4)=1;q(5)=0; 41 case 's3' 42 q(1)=1;q(2)=0;q(3)=0;q(4)=1;q(5)=1; 43 case 's4' 44 q(1)=1;q(2)=0;q(3)=1;q(4)=0;q(5)=0; 45 case 's5' 46 q(1)=1;q(2)=0;q(3)=1;q(4)=0;q(5)=1; 47 case 's6' 48 q(1)=1;q(2)=0;q(3)=1;q(4)=1;q(5)=0; 49 case 's7' 50 q(1)=1;q(2)=0;q(3)=1;q(4)=1;q(5)=1; 51 case 't8' 52 q(1)=1;q(2)=1;q(3)=0;q(4)=0;q(5)=0; 53 case 't9' 54 q(1)=1;q(2)=1;q(3)=0;q(4)=0;q(5)=1; 55 case 'k0' 56 q(1)=1;q(2)=1;q(3)=0;q(4)=1;q(5)=0; 57 case 'k1' 58 q(1)=1;q(2)=1;q(3)=0;q(4)=1;q(5)=1; 59 case 'gp' 60 q(1)=1;q(2)=1;q(3)=1;q(4)=0;q(5)=0; 61 case 'sp' 62 q(1)=1;q(2)=1;q(3)=1;q(4)=0;q(5)=1; 63 case 'fp' 64 q(1)=1;q(2)=1;q(3)=1;q(4)=1;q(5)=0; 65 case 'ra' 66 q(1)=1;q(2)=1;q(3)=1;q(4)=1;q(5)=1; 67 otherwise 68 q(1)=9;q(2)=9;q(3)=9;q(4)=9;q(5)=9; 69 end
函数rig_n 用来将5位十进制数转换成16位2进制小数输出
1 function [u] = rig_n(a) 2 3 if (a(1) == '-') 4 nu = str2num(a(2:5)); 5 num = 2^16 - nu; 6 q = dec2bin(num, 16); 7 else 8 if (a(1) == '0' | a(1) == '1' | a(1) == '2' | a(1) == '3' | a(1) == '4' | a(1) == '5') 9 nu = str2num(a); 10 q = dec2bin(nu,16); 11 end 12 end 13 14 for i = 1 : 16 15 u(i) = str2num(q(i)); 16 end
函数rig_sh 用来将3位十进制数转换成5位2进制小数输出
1 function [u] = rig_sh(a) 2 num = str2num(a); 3 q = dec2bin(num, 5); 4 5 for i = 1 : 5 6 u(i) = str2num(q(i)) 7 end
最后欣赏一下炫酷的界面
以及附带的read me 和最终打包的Matlab程序~~
1 *** 修正了lw和sw指令 2 *** 打开C2M.m文件运行程序 3 *** 立即数变成5位,lw,sw,addi等指令的数字都变成了5位 4 5 6 支持指令名称: 7 add\sub\and\slt\sll\srl\beq\bne\mul\addi\or\lw\sw 8 9 标准形式: 10 add $s1, $s2, $s0 11 sub $s1, $s2, $s0 12 and $s1, $s2, $s0 13 slt $s1, $s2, $s0 14 sll $s1, $s2, 00005 15 srl $s1, $s2, 00005 16 beq $s1, $s2, 00004 17 bne $s1, $s2, 00004 18 mul $s1, $s2, $s3 19 or $s1, $s2, $s3 20 lw $s1, 00005($s2) 21 sw $s1, 00005($s2) 22 addi $s1, $s2, -12 23 24 每一个命令之后加一个tab,地址前加$,逗号之后有空格,立即数的输入为3位,如5写成005,负数-5写成-05 25 寄存器文件按照书上标准,其中$0写法为$0_ 26 27 创建txt文件: 28 地址为:line266 29 C:\Users\ALIENWARE\Desktop\test_.txt (需要改成你自己的地址) 30 31 *不包含 j 指令