# Data Structures - Part I - PowerPoint PPT Presentation

1 / 20

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.

## Related searches for Data Structures - Part I

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Data Structures - Part I

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## 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

• 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, one-dimensional 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

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.

### Example 15.1

ar: .byte 0:7

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#get the base address

add i,i,5#distance to target is 5 bytes

move tmp,m[i]#access the target element

target element

can be viewed as offset

The formula for finding the address of an element in an array can be generalized to

i = b + s*n

s = size of each element

n = the number of elements between

the target element and the base

char = 1 byte

integer = 4 bytes

### 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

14 elements of type

integer

7 x 2 x (4 bytes)

### Example 15.4

To declare a 2 x 7 array of integers, we can do one of the following:

ar: .word 0:14

ar: .space 56

(0,0)

4

5

(0,1)

0

1

2

3

6

(0,2)

0

1

### Storage Order

Row-major order: the array is stored as a sequence of arrays consisting of rows

(0,0)

4

5

(1,0)

0

1

2

3

6

(0,1)

0

1

### Storage Order

Column-major order: The array is stored as a sequence of arrays consisting of columns instead of rows

### 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

0

1

2

3

0

1

2

### Example 15.5

e*k + n’ = 6

skip 6 elements

Row-major order: e = 4, k = 1, n’ = 2

Column-major order: e = 3, k =2, n’ = 1

e*k + n’ = 7

skip 7 elements

target

element