-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlab5 demo.asm
167 lines (146 loc) · 6.57 KB
/
lab5 demo.asm
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
#############################################################################
# Row-major order traversal of 16 x 16 array of words.
# Pete Sanderson
# 31 March 2007
#
# To easily observe the row-oriented order, run the Memory Reference
# Visualization tool with its default settings over this program.
# You may, at the same time or separately, run the Data Cache Simulator
# over this program to observe caching performance. Compare the results
# with those of the column-major order traversal algorithm.
#
# The C/C++/Java-like equivalent of this MIPS program is:
# int size = 16;
# int[size][size] data;
# int value = 0;
# for (int row = 0; col < size; row++) {
# for (int col = 0; col < size; col++) }
# data[row][col] = value;
# value++;
# }
# }
#
# Note: Program is hard-wired for 16 x 16 matrix. If you want to change this,
# three statements need to be changed.
# 1. The array storage size declaration at "data:" needs to be changed from
# 256 (which is 16 * 16) to #columns * #rows.
# 2. The "li" to initialize $t0 needs to be changed to new #rows.
# 3. The "li" to initialize $t1 needs to be changed to new #columns.
#
.data
newLine: .asciiz "\n"
space: .ascii " "
data: .word 0 : 256 # storage for 16x16 matrix of words
data1: .word 0 : 256 # storage for 16x16 matrix of words
.text
li $t0, 16 # $t0 = number of rows
li $t1, 16 # $t1 = number of columns
move $s0, $zero # $s0 = row counter
move $s1, $zero # $s1 = column counter
move $t2, $zero # $t2 = the value to be stored
# Each loop iteration will store incremented $t1 value into next element of matrix.
# Offset is calculated at each iteration. offset = 4 * (row*#cols+col)
# Note: no attempt is made to optimize runtime performance!
#------Matrix A------Matrix A------Matrix A------Matrix A------Matrix A------Matrix A------Matrix A------
loop: mult $s0, $t1 # $s2 = row * #cols (two-instruction sequence)
mflo $s2 # move multiply result from lo register to $s2
add $s2, $s2, $s1 # $s2 += column counter
sll $s2, $s2, 2 # $s2 *= 4 (shift left 2 bits) for byte offset
sw $t2, data($s2) # store the value in matrix element
# print out A
ori $v0, $0, 1
add $a0, $t2, $0
syscall
# print out space
ori $v0, $0, 4
la $a0, space
syscall
addi $t2, $t2, 1 # increment value to be stored
# Loop control: If we increment past last column, reset column counter and increment row counter
# If we increment past last row, we're finished.
addi $s1, $s1, 1 # increment column counter
bne $s1, $t1, loop # not at end of row so loop back
# println
ori $v0, $0, 4
la $a0, newLine
syscall
move $s1, $zero # reset column counter
addi $s0, $s0, 1 # increment row counter
bne $s0, $t0, loop # not at end of matrix so loop back
# println
ori $v0, $0, 4
la $a0, newLine
syscall
move $s0, $zero # $s0 = row counter
move $s1, $zero # $s1 = column counter
li $t2, 255 # $t2 = the value to be stored
#------Matrix B------Matrix B------Matrix B------Matrix B------Matrix B------Matrix B------Matrix B------
loop1: mult $s0, $t1 # $s2 = row * #cols (two-instruction sequence)
mflo $s2 # move multiply result from lo register to $s2
add $s2, $s2, $s1 # $s2 += column counter
sll $s2, $s2, 2 # $s2 *= 4 (shift left 2 bits) for byte offset
sw $t2, data($s2) # store the value in matrix element
# print out B
ori $v0, $0, 1
add $a0, $t2, $0
syscall
# print out space
ori $v0, $0, 4
la $a0, space
syscall
subi $t2, $t2, 1 # increment value to be stored
# Loop control: If we increment past last column, reset column counter and increment row counter
# If we increment past last row, we're finished.
addi $s1, $s1, 1 # increment column counter
bne $s1, $t1, loop1 # not at end of row so loop back
# println
ori $v0, $0, 4
la $a0, newLine
syscall
move $s1, $zero # reset column counter
addi $s0, $s0, 1 # increment row counter
bne $s0, $t0, loop1 # not at end of matrix so loop back
# println
ori $v0, $0, 4
la $a0, newLine
syscall
move $s0, $zero # $s0 = row counter
move $s1, $zero # $s1 = column counter
move $t2, $zero # $t2 = looping up
li $t3, 255 # $t3 = looping down
move $t4, $zero # $t4 = sum of $t2, $t3
#------Matrix A+B------Matrix A+B------Matrix A+B------Matrix A+B------Matrix A+B------Matrix A+B------Matrix A+B------
loop2: mult $s0, $t1 # $s2 = row * #cols (two-instruction sequence)
mflo $s2 # move multiply result from lo register to $s2
add $s2, $s2, $s1 # $s2 += column counter
sll $s2, $s2, 2 # $s2 *= 4 (shift left 2 bits) for byte offset
sw $t2, data($s2) # store the value in matrix element
# print out A+B
ori $v0, $0, 1
add $t4, $t3, $t2 #sum of looping up and down
add $a0, $t4, $0
syscall
# print out space
ori $v0, $0, 4
la $a0, space
syscall
subi $t3, $t3, 1 # increment value down
addi $t2, $t2, 1 # increment value up
# Loop control: If we increment past last column, reset column counter and increment row counter
# If we increment past last row, we're finished.
addi $s1, $s1, 1 # increment column counter
bne $s1, $t1, loop2 # not at end of row so loop back
# println
ori $v0, $0, 4
la $a0, newLine
syscall
move $s1, $zero # reset column counter
addi $s0, $s0, 1 # increment row counter
bne $s0, $t0, loop2 # not at end of matrix so loop back
# println
ori $v0, $0, 4
la $a0, newLine
syscall
# We're finished traversing the matrix.
li $v0, 10 # system service 10 is exit
syscall # we are outta here.