File tree 2 files changed +110
-0
lines changed
2 files changed +110
-0
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,64 @@ main:
147
147
148
148
=== Macros
149
149
150
+ MARS supports function style macros that can shorten your code and improve
151
+ readability in some cases (though I feel it can also make it worse or be a wash).
152
+
153
+ The syntax looks like this:
154
+
155
+ [source,mips,linenums]
156
+ ----
157
+ .macro macroname
158
+ instr1 a, b, c
159
+ instr2, b, d
160
+ # etc.
161
+ .end_macro
162
+
163
+ # or with parameters
164
+ .macro macroname(%arg1)
165
+ instr1 a, %arg1
166
+ instr2 c, d, e
167
+ # etc.
168
+ .end_macro
169
+ ----
170
+
171
+ Some common examples are using them to print strings:
172
+
173
+ [source,mips,linenums]
174
+ ----
175
+ .macro print_str_label(%x)
176
+ li $v0, 4
177
+ la $a0, %x
178
+ syscall
179
+ .end_macro
180
+
181
+ .macro print_str(%str)
182
+ .data
183
+ str: .asciiz %str
184
+ .text
185
+ li $v0, 4
186
+ la $a0, str
187
+ syscall
188
+ .end_macro
189
+
190
+ .data
191
+
192
+ str1: .asciiz "Hello 1\n"
193
+
194
+ .text
195
+ # in use:
196
+ print_str_label(str1)
197
+
198
+ print_str("Hello World\n")
199
+
200
+ ...
201
+ ----
202
+
203
+ You can see an example program in macros.s
204
+
205
+ Unfortunately, as far as I can tell SPIM does not support function style macros
206
+ despite what MARS's documentation implies about using a $ instead of a % for arguments.
207
+
150
208
// TODO MARS macros does spim have macros?
151
209
152
210
Original file line number Diff line number Diff line change
1
+
2
+ .data
3
+
4
+ .macro sys_exit
5
+ li $v0, 10
6
+ syscall
7
+ .end_macro
8
+
9
+ .macro print_int_reg(%x)
10
+ li $v0, 1
11
+ move $a0, %x
12
+ syscall
13
+ .end_macro
14
+
15
+ .macro print_str_label(%x)
16
+ li $v0, 4
17
+ la $a0, %x
18
+ syscall
19
+ .end_macro
20
+
21
+ .macro print_str(%str )
22
+ .data
23
+ str: .asciiz %str
24
+ .text
25
+ li $v0, 4
26
+ la $a0, str
27
+ syscall
28
+ .end_macro
29
+
30
+
31
+
32
+ str1: .asciiz "Hello 1\n"
33
+
34
+
35
+
36
+ .text
37
+
38
+ main:
39
+
40
+ print_str_label(str1)
41
+
42
+ print_str("Hello World!\n" )
43
+
44
+ li $t0, 42
45
+ print_int_reg($t0)
46
+
47
+
48
+
49
+
50
+ #sys_exit
51
+ li $v0, 10
52
+ syscall
You can’t perform that action at this time.
0 commit comments