Lab_CubeStats1_Class_pres
Introduction to Lab #3:
Lab_CubeStats1
Karim Ali
CMPUT 229
University of Alberta
Requirements
• Follow all subroutine calling conventions
• Must use $fp to access anything that is stored
in the stack
– Only can use $sp in this assignment to change the
size of the stack.
CubeStats
• Receives the following parameters:
– corner: the address of the first element of a
cube in an n-dimensional array.
– edge: the size of the edge of the cube.
– dimensions: the number of dimensions of the
cube (and base array).
– size: the size of the base array
• Assume that the size of the base array is the same in all
dimensions, i.e. the base array is itself a cube
A two-dimensional example
0 1 2 3 4 5 6 7 8 9
0
1
2
3
4
5
6
7
8
9
corner
edge
size
dimension = 2
CubeStats Return Values
• $v0: a signed integer representing the range
of all elements in the specified cube.
• $v1: a signed integer representing the floor
of the average of all elements in the specified
cube.
CubeStats Return Values — more
formally
𝐶 = 𝑆𝑒𝑡 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝑡ℎ𝑎𝑡 𝑎𝑟𝑒 𝑖𝑛 𝑡ℎ𝑒
𝑠𝑝𝑒𝑐𝑖𝑓𝑖𝑒𝑑 𝑐𝑢𝑏𝑒
$𝑣0 = max 𝐶 −min (𝐶)
$𝑣1 =
∑ 𝑥D�FG ∈ I
|𝐶|
CubeStats (cont.)
• Assume that the parameters are correct:
– Parameters are positive
– The Cube is contained within the base array
… 7 3 -1 1 4 -5 15 …
0x10001024
0x10001020
0x1000101C
0x10001018
0x10001014
0x10001010
0x1000100C
0x10001008 -1
Address Value
⋅⋅⋅
15
-5
4
1
0x10001004
0x10001000
0x10000FFC
3
7
⋅⋅⋅
Organization of B in memory
in row-major style.
A[i]
One-dimensional matrix A.
1-d Array Storage
What is the address of element -1 (i=2) ?
�
A + i × 4
A
↑
0x10001024
0x10001020
0x1000101C
0x10001018
0x10001014
0x10001010
0x1000100C
0x10001008
Address Value
z
y
x
w
v
u
t
0x10001004
0x10001000
0x10000FFC
s
r
B[i][j]
r s t
u v w
x y z
i
j
Two-dimensional 3×3 matrix B.
Organization of B in memory
in row-major style.
2-d Array Storage
What is the address of element w (i=1, j =2) ?
�
B + (i × 3 + j) × 4
0x10001024
0x10001020
0x1000101C
0x10001018
0x10001014
0x10001010
0x1000100C
0x10001008
Address Value
z
y
x
w
v
u
t
0x10001004
0x10001000
0x10000FFC
s
r
B[i][j]
r s t
u v w
x y z
i
j
Two-dimensional 3×3 matrix B.
Organization of B in memory
in row-major style.
2-d Array Storage
Which elements belong to a Cube at position (1,1) with
an edge = 2?
0x10001024
0x10001020
0x1000101C
0x10001018
0x10001014
0x10001010
0x1000100C
0x10001008
Address Value
z
y
x
w
v
u
t
0x10001004
0x10001000
0x10000FFC
s
r
B[i][j]
r s t
u v w
x y z
i
j
Two-dimensional 3×3 matrix B.
Organization of B in memory
in row-major style.
2-d Array Storage
Which elements belong to a Cube at position (1,1) with
an edge = 2?
g h
e f
c d
a b
0x10001024
0x10001020
0x1000101C
0x10001018
0x10001014
0x10001010
0x1000100C
0x10001008
h
g
f
c
Address Value
e
d
0x10001004
0x10001000
0x10000FFC
b
a
C[i][j][k]
c d
a b
j
k
i
Three-dimensional 2×2×2 matrix C.
Organization of C in memory
in row-major style.
3-d Array Storage
What is the address of element h (i=1, j =1, k=1) ?
�
C + (((i × 2) + j) × 2 + k) × 4
C + (i× 2 × 2 + j× 2 + k) × 4
g h
e f
c d
a b
0x10001024
0x10001020
0x1000101C
0x10001018
0x10001014
0x10001010
0x1000100C
0x10001008
h
g
f
c
Address Value
e
d
0x10001004
0x10001000
0x10000FFC
b
a
C[i][j][k]
c d
a b
j
k
i
Three-dimensional 2×2×2 matrix C.
Organization of C in memory
in row-major style.
3-d Array Storage
What is the address of element h (i=1, j =1, k=1) ?
�
C + (((i × 2) + j) × 2 + k) × 4
C + (i× 2 × 2 + j× 2 + k) × 4
main program
• reads a k-dimensional Cube from a file
• places the values in the memory in row-major
format
• for each specification of a cube in the file:
– initializes three global variables to zero:
• min, max, and total
– calls your CubeStats subroutine
– prints the value returned by CubeStats
File format
3 2
a b
c d
e f
g h
1 0 1 1
0 0 0 2
-1
Cube a position (0,0,0)
with edge 2
Cube at position (1,0,1) with edge 1
size of array
dimension of array
g h
e f
C[i][j][k]
c d
a b
j
k
i
C[1][0][1]
g h
e f
C[1][0][1]
e f
C[1][0][1]
f
C[1][0][1] ↔ f
c d
a b
main
• Reading and understanding the main routine
is part of the assignment.
Test Generator
• A test generator, written in Python, is
provided to you as a convenience.
– Have fun modifying/playing with it.
• Caution:
– Large test cases overflow the arena provided
– Increasing the arena is ok but will eventually run
into the static space limit of SPIM.
What to hand in
• A single file named lab3.s containing your
subroutine CubeStats written in MIPS
assembly.
• Your subroutine must return to the caller
using the instruction:
jr $ra
• Your file must not contain a main function.