Load and Constant manipulating instructions in MIPS
LAB # 11
ARRAYS IN MIPS ASSEMBLY LANGUAGE
OBJECTIVE
To introduce how to implement array as an abstract data structure in MIPS assembly
language.
THEORY
Not like high level languages, Assembly language has no notion of an array at all.
Arrays like variables are treated as a block of memory that could be allocated
with a single directive, where the first element is given a label.
The array as the most important and most general data structure has the following
properties:
1. All elements must be the same size. The array is a homogeneous data
structure.
2. The size of an array is fixed. The number of elements is fixed.
3. A label (address) is tied to the first element of the array.
4. Traversing each element of an array needs an index or indices and the label
as the array's name.
ARRAY DECLARATION:
With reference to the above properties, in assembly language to declare an array it
requires:
1. A label name,
2. The number of elements,
3. The size of each element,
4. The initial value of each element.
EXAMPLE:
Computer Architecture & Organization - CAO 36
Load and Constant manipulating instructions in MIPS
.data
A01: .byte 'a', 'k', 'p', 5 # A01 is an array of 4 bytes: {'a', 'k', 'p', 5}
A02: .word 5, 6, -9, 7 # A02 is an array of 4 words: {5, 6, -9, 7}
B02: .space 40 #allocate 40 consecutive bytes, with storage uninitialized
# could be used as a 40-element character array, or a
# 10-element integer array;
# a comment should indicate which!
var1: .half 3 # create a single short integer variable with initial value 3
B03: .word -1:30 # allocate 40 consecutive words with each element
# initialized with -1.
TRAVERSING SINGLE-DIMENSIONAL ARRAY:
To access every element of an array, we have to know the address of that element.
Because all elements have the same size, the address of an element of the array can be
formulated as:
The address of ith-element (in byte) = starting address + size-of-element * i
1. The first element of the array is indexed 0.
2. The size-of-element is the number of bytes in a single array element.
3. The size-of-element either is one byte, 2 bytes, 4 bytes, or 8 bytes.
EXAMPLE:
The following code fragment is to access the sixth element of table1:
.data
table1: .word 4, 5, 6, 7, 8, 9, 10, 21
.text
la $t0, table1
lw $t1, 20($t0)
addiu $t2, $t0, 20
lw $t1, 0($t2)
Computer Architecture & Organization - CAO 37