-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUCIID_lab1-2.s
More file actions
225 lines (192 loc) · 5.41 KB
/
UCIID_lab1-2.s
File metadata and controls
225 lines (192 loc) · 5.41 KB
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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
# ICS 51, Lab #1
#
# IMPORTATNT NOTES:
#
# Write your assembly code only in the marked blocks.
#
# DO NOT change anything outside the marked blocks.
#
# Remember to fill in your name, student ID in the designated sections.
#
#
j main
###############################################################
# Data Section
.data
#
# Fill in your name, student ID in the designated sections.
#
student_name: .asciiz "First Last"
student_id: .asciiz "UCIID"
new_line: .asciiz "\n"
space: .asciiz " "
double_range_lbl: .asciiz "\nDouble range (Decimal Values) \nExpected output:\n1200 -690 104\nObtained output:\n"
swap_bits_lbl: .asciiz "\nSwap bits (Hexadecimal Values)\nExpected output:\n75757575 FD5775DF 064B9A83\nObtained output:\n"
count_bits_lbl: .asciiz "\nCount bits \nExpected output:\n20 24 13\nObtained output:\n"
swap_bits_test_data: .word 0xBABABABA, 0xFEABBAEF, 0x09876543
swap_bits_expected_data: .word 0x75757575, 0xFD5775DF, 0x064B9A83
double_range_test_data: .word 945, -345, 0, -3, 55
double_range_expected_data: .word 1200, -690, 104
hex_digits: .byte '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
###############################################################
# Text Section
.text
# Utility function to print hexadecimal numbers
print_hex:
move $t0, $a0
li $t1, 8 # digits
lui $t2, 0xf000 # mask
mask_and_print:
# print last hex digit
and $t4, $t0, $t2
srl $t4, $t4, 28
la $t3, hex_digits
add $t3, $t3, $t4
lb $a0, 0($t3)
li $v0, 11
syscall
# shift 4 times
sll $t0, $t0, 4
addi $t1, $t1, -1
bgtz $t1, mask_and_print
exit:
jr $ra
###############################################################
###############################################################
###############################################################
# PART 1 (Count Bits)
#
# You are given an 32-bits integer stored in $t0. Count the number of 1's
#in the given number. For example: 1111 0000 should return 4
count_bits:
move $t0, $a0
############################## Part 1: your code begins here ###
############################## Part 1: your code ends here ###
move $v0, $t0
jr $ra
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
# PART 2 (Swap Bits)
#
# You are given an 32-bits integer stored in $t0. You need swap the bits
# at odd and even positions. i.e. b31 <-> b30, b29 <-> b28, ... , b1 <-> b0
# The result must be stored inside $t0 as well.
swap_bits:
move $t0, $a0
############################## Part 2: your code begins here ###
############################## Part 2: your code ends here ###
move $v0, $t0
jr $ra
###############################################################
###############################################################
###############################################################
# PART 3
#
# You are given three integers. You need to find the smallest
# one and the largest one and multiply their sum by two and return it.
#
# Implementation details:
# The three integers are stored in registers $t0, $t1, and $t2. You
# need to store the answer into register $t0. It will be returned by the
# function to the caller.
double_range:
move $t0, $a0
move $t1, $a1
move $t2, $a2
############################### Part 3: your code begins here ##
############################### Part 3: your code ends here ##
move $v0, $t0
jr $ra
###############################################################
###############################################################
###############################################################
# Main Function
.globl main
main:
li $v0, 4
la $a0, student_name
syscall
la $a0, new_line
syscall
la $a0, student_id
syscall
la $a0, new_line
syscall
la $a0, count_bits_lbl
syscall
# Testing part 2
li $s0, 3 # num of test cases
li $s1, 0
la $s2, swap_bits_test_data
test_p1:
add $s4, $s2, $s1
# Pass input parameter
lw $a0, 0($s4)
jal count_bits
move $a0, $v0 # $integer to print
li $v0, 1
syscall
li $v0, 4
la $a0, space
syscall
addi $s1, $s1, 4
addi $s0, $s0, -1
bgtz $s0, test_p1
li $v0, 4
la $a0, new_line
syscall
la $a0, swap_bits_lbl
syscall
# Testing part 2
li $s0, 3 # num of test cases
li $s1, 0
la $s2, swap_bits_test_data
test_p2:
add $s4, $s2, $s1
# Pass input parameter
lw $a0, 0($s4)
jal swap_bits
move $a0, $v0
jal print_hex
li $v0, 4
la $a0, space
syscall
addi $s1, $s1, 4
addi $s0, $s0, -1
bgtz $s0, test_p2
li $v0, 4
la $a0, new_line
syscall
la $a0, double_range_lbl
syscall
# Testing part 3
li $s0, 3 # num of test cases
li $s1, 0
la $s2, double_range_test_data
test_p3:
add $s4, $s2, $s1
# Pass input parameter
lw $a0, 0($s4)
lw $a1, 4($s4)
lw $a2, 8($s4)
jal double_range
move $a0, $v0 # $integer to print
li $v0, 1
syscall
li $v0, 4
la $a0, space
syscall
addi $s1, $s1, 4
addi $s0, $s0, -1
bgtz $s0, test_p3
_end:
# end program
li $v0, 10
syscall