data structures part i l.
Skip this Video
Download Presentation
Data Structures - Part I

Loading in 2 Seconds...

play fullscreen
1 / 20

Data Structures - Part I - PowerPoint PPT Presentation

  • Uploaded on

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.

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

PowerPoint Slideshow about 'Data Structures - Part I' - derron

Download Now An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
data structures part i

Data Structures - Part I

CS 215 Lecture 7

  • 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.
  • 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
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
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
  • 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
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

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

base address

address of target element

finding the element address

can be viewed as offset

from the base address

Finding the element address

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

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


char = 1 byte

integer = 4 bytes

two dimensional arrays
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

14 elements of type


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

storage order













Storage Order

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

storage order18













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

example 15 5








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