Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data Structures - Part I
CS 215 Lecture 7
Motivation




Real programs use abstractions like
lists, trees, stacks, and queues.
The data associated with these
abstractions must be ordered within
memory.
Assembly language does not provide
convenient ways to access data in
memory like high-level languages do.
It therefore necessary to calculate
explicitly the locations of data within
the structure.
Memory




A memory cell is a unit of
memory with a unique address.
The entire memory is a collection
of memory cells.
Each memory cell holds eight
bits, or 1 byte.
A word designates the amount of
memory used to store an integer,
e.g., 1 word = 4 bytes
Byte Addressing




A method of addressing where each
byte has a unique address
The ordering of the bytes within the
word can either be little-endian or
big-endian.
Little-endian numbers the bytes
from the least significant to the
most signifcant.
Big-endian numbers the bytes from
the most significant to the least
signifcant.
Storing an integer



Assume a 32-bit word. The number of
bits used to store an integer is 32 or 4
bytes. It can also be thought of as an
array of 4 bytes.
By convention, the smallest of the four
byte addresses is used to indicate the
address of the word.
The smallest of the byte addresses in
a word must be a multiple of four -words are aligned
Array




The array is the most important and
most general data structure.
All other data structures can be
implemented using an array.
The computer memory itself is
organized as a large, onedimensional array.
All uses of the memory are simply
allocations of part of this gigantic
array.
Allocating Space for Arrays


SAL does not have a facility for
declaring arrays.
To allocate space for an array of
char
{variable:} .byte {value:numelement}

An array of integers or real numbers
can be allocated space using
{variable:} .word {value:numelement}
{variable:} .float {value:numelement}

If we know how many bytes (given
by value) to allocate to a variable
we can directly allocate space by
using the .space directive
variable: .space value
Example 15.1
ar: .byte 0:7
7 bytes of memory space is allocated
to the variable ar. The memory cells
are initialized to 0. In the ASCII table
0 is the null character.
we can also allocate 7 bytes using
ar: .space 7
Example 15.2
Suppose we want to declare an array of
integers that can hold 10 elements. We
can declare the array in one the following
ways:
ar: .word 0:10
ar: .byte 0:40
ar: .space 40
Note: We only need to allocate the total amount of
memory space. Each memory cell has the same size
of 1 byte. Recall that an integer is 4 bytes. Also the label
ar is bound to the first byte of the allocated 40 bytes.
Accessing array elements



To access element i of the array, we
must add an offset to the array’s
starting address, also called the base
address. This is where the label of the
array declaration is bound.
We can access the ith element via
m[i]
We must make sure that the value of
i is the correct displacement between
the ith element and the base
address.



m[i] is used to access memory in a
byte fashion.
If we want to access .word spaces
from memory, we need to use M[i].
In the case of M[i], you must be
sure that i is a multiple of 4.
Example 15.3
. . .
ar: .byte 0:20 #20-element array
i: .word
#holds the array’s base address
tmp: .byte
. . .
la
i, ar
add i,i,5
move tmp,m[i]
base address
#get the base address
#distance to target is 5 bytes
#access the target element
target element
address of target element
Finding the element address
The formula for finding the address of
an element in an array can be
generalized to
char = 1 byte
integer = 4 bytes
i = b + s*n
where b = base address
s = size of each element
n = the number of elements between
the target element and the base
address
can be viewed as offset
from the base address
Two-dimensional Arrays

The number of bytes needed for the
array is found by multiplying the
size of each element times the
number of elements
2 x 7 array
Example 15.4
To declare a 2 x 7 array of integers,
we can do one of the following:
14 elements of type
integer
ar: .word 0:14
ar: .space 56
7 x 2 x (4 bytes)
Storage Order
Row-major order: the array is
stored as a sequence of arrays
consisting of rows
0
0
1
1
2
3
4
5
6
(0,0)
(0,1)
(0,2)
Storage Order
Column-major order: The array is
stored as a sequence of arrays
consisting of columns instead of
rows
0
0
1
1
2
3
4
5
6
(0,0)
(1,0)
(0,1)
Accessing elements in 2D arrays
We know that the formula for finding
the address of an element is
i = b + s*n
s*n = s*e*k + s*n’ = s*(e*k + n’)
e = number of elements in a row or column
k = number of rows or columns to be skipped
n’ = the number of elements between the target
element and the beginning of a row or column
e*k + n’ = 6
skip 6 elements
Example 15.5
Row-major order: e =
Column-major order:
e*k + n’ = 7
skip 7 elements
0
0
1
2
1
2
4, k = 1, n’ = 2
e = 3, k =2, n’ = 1
3
target
element