UART
Arrays
Christopher Mar
Announcements
Monday is Fall Break (No Lecture)
Wednesday we will be doing another In-Class Activity using Arrays
Array
Data structure
Array name is a pointer
Memory address of first element
Other elements accessed using offset (number of elements from first element)
Array Data Access
Address Contents
0x10000000 12
0x10000004 256
0x10000008 9
0x1000000C 37
0x10000010 654
0x10000014 81
0x10000018 0
0x1000001C 5309
Lecture Question
What instruction would you use to read the value from an array element?
li
lw
sw
move
None of the above
Array Data Access
Address Contents
0x10000000 12
0x10000004 256
0x10000008 9
0x1000000C 37
0x10000010 654
0x10000014 81
0x10000018 0
0x1000001C 5309
register
lw
Lecture Question
What instruction would you use to write the value from an array element?
li
lw
sw
move
None of the above
Array Data Access
Address Contents
0x10000000 12
0x10000004 256
0x10000008 9
0x1000000C 37
0x10000010 654
0x10000014 81
0x10000018 0
0x1000001C 5309
register
lw
register
sw
Traversing an Array
Loop that increments offset
my_array[i]
Counter, i, is the offset from the base address
Traversing an Array
Problem: in PLP offset is a fixed value
Architectural choice limits lw/sw to 2 register accesses
Solution: save base address (if needed) and increment register pointing to array address
Lecture Question
If $s0 points to the base address of an array, which instruction would read from the fourth element (assume array is zero indexed and each element is 1 word in size)?
lw $t0, 0($s0)
lw $t0, 3($s0)
lw $t0, 4($s0)
lw $t0, 12($s0)
Array Data Access
Address Contents
0x10000000 13
0x10000004 256
0x10000008 9
0x1000000C 37
0x10000010 654
0x10000014 81
0x10000018 0
0x1000001C 5309
Array Space Allocation
Assembler Directive, .space
Example:
array_label:
.space 10
Leaves 10 words of memory empty
Copy address of space into register:
li $s0, array_label
More information can be found at:
http://progressive-learning-platform.github.io/instructions.html#space-allocation
Array Space Allocation
Assembler Directive, .word
Example:
array_label:
.word 9
.word 2
Places the value, 9, in a word in memory and the value, 2, in the next word
More information can be found at:
http://progressive-learning-platform.github.io/instructions.html#space-allocation
Array Space Allocation
Assembler Directive, .asciiw
Example:
string_label:
.asciiw “example string”
Places one character per 32-bit word
In this example, the base address (pointed to by string_label) contains an “e”
More information can be found at:
http://progressive-learning-platform.github.io/instructions.html#space-allocation
Bubble Sort
Array Examples
17