-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain-row-first.s
More file actions
162 lines (145 loc) · 4.01 KB
/
main-row-first.s
File metadata and controls
162 lines (145 loc) · 4.01 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
#
# CMPUT 229: Cube Statistics Laboratory
# Author: Jose Nelson Amaral
# Date: December 2009
#
# Main program to read base array into memory,
# read a several cube specifications
# and print statistics for each cube.
#
.data
arena:
.space 32768
Pedge:
.asciiz "edge = "
PnegAvg:
.asciiz ", Negative Average = "
PposAvg:
.asciiz ", Positive Average = "
Pnewline:
.asciiz "\n"
# These data items will be used by the CubeStats method.
.globl countNeg
.globl countPos
.globl totalNeg
.globl totalPos
totalNeg: .word 0
totalPos: .word 0
countNeg: .word 0
countPos: .word 0
######################################################################
# Register usage: #
# $s0: dimension #
# $s1: size #
# $s2: edge #
# $s3: first #
######################################################################
.text
.globl power
power:
li $v0, 1
ploop:
beqz $a1, pdone
mul $v0, $v0, $a0
subu $a1, $a1, 1
j ploop
pdone:
jr $ra
.globl main
main:
subu $sp, $sp, 4 # Adjust the stack to save $fp
sw $fp, 0($sp) # Save $fp
move $fp, $sp # $fp <-- $fp
subu $sp, $sp, 4 # Adjust stack to save $ra
sw $ra, -4($fp) # Save the return address ($ra)
# Get the dimension
li $v0, 5
syscall
move $s0, $v0 # $s0 <-- dimension
# Get the size
li $v0, 5
syscall
move $s1, $v0 # $s1 <-- size
# Calculate numelems
move $a0, $s1 # $a0 <-- size
move $a1, $s0 # $a1 <-- dimension
jal power # numelems <-- power(size,dimension)
# Read array
sll $v0,$v0,2 # $v0 <-- 4*numelems
la $t5, arena # cursor <-- start of arena
add $t6, $t5, $v0 # $t6 <-- end of array
ReadArray:
li $v0, 5
syscall # $v0 <-- element
sw $v0, 0($t5) # *cursor <-- element
addi $t5, $t5, 4 # *cursor++
blt $t5, $t6, ReadArray # if(cursor<end of array)
forever:
# Read a Cube
la $s3, arena # first <-- start of arena
add $t2, $0, $0 # d <-- 0
ReadCube:
# Get the corner, calculating its absolute location along the way
li $v0, 5
syscall # $v0 <-- cubed
move $t4, $v0 # $t4 <-- cubed
blt $t4, $0, ExitMain # if(cubed<0) ExitMain
move $a0, $s1 # $a0 <-- size
sub $a1, $s0, $t2 # $a1 <-- dimension - d
addi $a1, $a1, -1 # $a1 <-- dimension - d - 1
jal power # $v0 <-- power(size,d)
mul $t3, $t4, $v0 # $t3 <-- cubed*power(size,dimension - d - 1)
sll $t3, $t3, 2 # $t3 <-- 4*$t3 (offset)
add $s3, $s3, $t3 # first = first + cubed*power(size,dimension - d - 1)
add $t2, $t2, 1 # d <-- d + 1
blt $t2, $s0, ReadCube # if(d<dimension) ReadCube
# Get the edge length
li $v0, 5
syscall # $v0 <-- edge
move $s2, $v0 # $s2 <-- edge
# Initialize totals and counts to be used by CubeStats
sw $0, countNeg
sw $0, countPos
sw $0, totalNeg
sw $0, totalPos
# Set up the arguments and call CubeStats
move $a0, $s0 # $a0 <-- dimension
move $a1, $s1 # $a1 <-- size
move $a2, $s3 # $a2 <-- first
move $a3, $s2 # $a3 <-- edge
jal CubeStats
# Get the averages into $t0, $t1
move $t0, $v0
move $t1, $v1
# Print the value of the edge
li $v0, 4
la $a0, Pedge
syscall
move $a0, $s2
li $v0, 1
syscall
# Print the value of the positive average
li $v0, 4
la $a0, PposAvg
syscall
move $a0, $t1
li $v0, 1
syscall
# Print the value of the negative average
li $v0, 4
la $a0, PnegAvg
syscall
move $a0, $t0
li $v0, 1
syscall
li $v0, 4
la $a0, Pnewline
syscall
j forever
ExitMain:
# Usual stuff at the end of the main
lw $ra, -4($fp)
addu $sp, $sp, 4
lw $fp, 0($sp)
addu $sp, $sp, 4
jr $ra