-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.s
274 lines (239 loc) · 7.27 KB
/
template.s
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#################################################
# #
# Data Segment #
# #
#################################################
.data
endl: .asciiz "\n"
msg: .asciiz "Hello, MIPS World!\n"
num: .word 10
#################################################
# #
# Text Segment #
# #
#################################################
.text
.globl __start
__start:
# Print a message
la $a0, msg # Load address of message
li $v0, 4 # System call to print string
syscall
# Example: Call a procedure that uses stack
jal leaf_proc_using_stack
# Example: Arithmetic operations
li $t0, 5
li $t1, 10
add $t2, $t0, $t1 # $t2 = $t0 + $t1
sub $t3, $t1, $t0 # $t3 = $t1 - $t0
mul $t4, $t0, $t1 # $t4 = $t0 * $t1
div $t1, $t0 # Divide $t1 by $t0
mflo $t5 # Move quotient to $t5
mfhi $t6 # Move remainder to $t6
# Print newline
jal print_endl
# Example: Logical operations
li $t7, 0xFF00FF00
li $t8, 0x00FF00FF
and $t9, $t7, $t8 # Bitwise AND
or $s0, $t7, $t8 # Bitwise OR
xor $s1, $t7, $t8 # Bitwise XOR
not $s2, $t7 # Bitwise NOT
# Example: Looping
li $t0, 0 # Initialize counter to 0
while_loop:
bge $t0, 5, Exit # Exit loop if counter >= 5
# Print counter
move $a0, $t0
li $v0, 1 # System call to print integer
syscall
jal print_endl
addi $t0, $t0, 1 # Increment counter
j while_loop
Exit:
li $v0, 10
syscall # Exit program
#################################################
# #
# Useful Procedures #
# #
#################################################
print_endl:
la $a0, endl # Load address of newline
li $v0, 4 # System call to print
syscall
jr $ra # Return
print_msg:
la $a0, msg # Load address of message
li $v0, 4 # System call to print
syscall
jr $ra # Return
#################################################
# #
# Leaf Procedures #
# #
#################################################
leaf_proc_using_stack:
# Prologue
addi $sp, $sp, -16 # Allocate stack space
sw $t0, 0($sp) # Save registers
sw $t1, 4($sp)
sw $t2, 8($sp)
sw $t3, 12($sp)
# Example computation
li $t0, 42
li $t1, 24
add $t2, $t0, $t1
# Epilogue
lw $t0, 0($sp) # Restore registers
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $t3, 12($sp)
addi $sp, $sp, 16 # Deallocate stack space
jr $ra # Return
#################################################
# #
# Node Procedures #
# #
#################################################
node_proc_using_stack:
# Prologue
addi $sp, $sp, -16 # Allocate stack space
sw $ra, 12($sp) # Save $ra
sw $t0, 8($sp) # Save other registers
sw $t1, 4($sp)
sw $t2, 0($sp)
# Example function call
jal leaf_proc_using_stack
# Epilogue
lw $t0, 0($sp) # Restore other registers
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $ra, 12($sp) # Restore $ra
addi $sp, $sp, 16 # Deallocate stack space
jr $ra # Return
#################################################
# #
# Control Structures #
# #
#################################################
switch_case_no_break:
li $t0, 3 # Example value
li $t1, 1
li $t2, 2
li $t3, 3
beq $t0, $t1, case_1
beq $t0, $t2, case_2
beq $t0, $t3, case_3
j end_switch
case_1:
# Code for case 1
j switch_case_no_break
case_2:
# Code for case 2
j switch_case_no_break
case_3:
# Code for case 3
end_switch:
# End of switch-case
switch_case_with_break:
li $t0, 2 # Example value
li $t1, 1
li $t2, 2
li $t3, 3
beq $t0, $t1, case_1_break
beq $t0, $t2, case_2_break
beq $t0, $t3, case_3_break
j end_switch_break
case_1_break:
# Code for case 1
j end_switch_break
case_2_break:
# Code for case 2
j end_switch_break
case_3_break:
# Code for case 3
end_switch_break:
# End of switch-case with breaks
while_do_1_condition:
li $t0, 0
while_cond:
bge $t0, 10, end_while
# Code inside while loop
addi $t0, $t0, 1
j while_cond
end_while:
# End of while loop
while_do_a_AND_b:
li $t0, 0
li $t1, 0
while_and:
bge $t0, 5, end_while_and
bge $t1, 5, end_while_and
# Code inside while (a AND b)
addi $t0, $t0, 1
addi $t1, $t1, 1
j while_and
end_while_and:
# End of while loop with AND condition
while_do_a_OR_b_1:
li $t0, 0
li $t1, 0
while_or_1:
bge $t0, 5, check_b
j continue_or_1
check_b:
bge $t1, 5, end_while_or_1
continue_or_1:
# Code inside while (a OR b), approach 1
addi $t0, $t0, 1
addi $t1, $t1, 1
j while_or_1
end_while_or_1:
# End of while loop with OR condition, approach 1
while_do_a_OR_b_2:
li $t0, 0
li $t1, 0
while_or_2:
blt $t0, 5, continue_or_2
blt $t1, 5, continue_or_2
j end_while_or_2
continue_or_2:
# Code inside while (a OR b), approach 2
addi $t0, $t0, 1
addi $t1, $t1, 1
j while_or_2
end_while_or_2:
# End of while loop with OR condition, approach 2
#################################################
# #
# Function Call Example #
# #
#################################################
function_call_example:
li $a0, 5 # Argument to function
jal factorial # Call factorial function
# Result is in $v0
j Exit
factorial:
# Factorial function using recursion
# Prologue
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $a0, 0($sp)
# Base case
li $t0, 1
beq $a0, $t0, factorial_base
# Recursive case
addi $a0, $a0, -1
jal factorial
lw $a0, 0($sp)
mul $v0, $a0, $v0
j factorial_end
factorial_base:
li $v0, 1
factorial_end:
# Epilogue
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra