Memory and Data Structures

Post on 13-Jan-2016

28 views 0 download

description

Memory and Data Structures. Arrays Stacks Queues. Memory. This is the “RAM” in a system Labels and addresses point to pieces of memory holding: Words Bytes Strings Floats Memory is just a collection of bits - PowerPoint PPT Presentation

transcript

Memory and Data Structures

ArraysStacksQueues

2CMPE12c Gabriel Hugh Elkaim

Memory

• This is the “RAM” in a system• Labels and addresses point to pieces of

memory holding:– Words– Bytes– Strings– Floats

• Memory is just a collection of bits• Can be used to represent integers,

characters, or some arbitrary representation.

3CMPE12c Gabriel Hugh Elkaim

• Instead: treat memory as a giant flat array of bytes– Compiler or Progammer decides what

to make of it– Element numbering starts at 0– The element number is an address

• In “C” memory is allocated by:

char m[size of array];

Memory

4CMPE12c Gabriel Hugh Elkaim

Storage of bytes

•MIPS architecture is “byte addressable” meaning that all addresses are “byte” addresses.

•This means the smallest unit of memory we can allocate is a byte.

•Use ‘lb’ (load byte) to access this unit.

5CMPE12c Gabriel Hugh Elkaim

Example

.datamychar: .bytenewline: .byte ‘\n’

….text

…lb $t0, newlineli $v0, 12 # getcsyscallsb $v0, mycharbeq $t0,$v0, found_newline

…found_newline: …

Storage of bytes

6CMPE12c Gabriel Hugh Elkaim

Storage of bytes

The data is placed in memory like this at start up (assuming .data section starts at address 4). The mychar variable will change to the value of the character entered by the user once stored.

3 0

7 0A 00 4

9 8

15 12

Memory

newline variable

mychar variable

addresses

7CMPE12c Gabriel Hugh Elkaim

Storage of Integers

•Use 4 bytes beginning at an address that is multiple of 4.

•So if stored at address 20, uses bytes 20, 21, 22, and 23.

•Use ‘lw’ (load word) instead of ‘lb’ (load byte).

8CMPE12c Gabriel Hugh Elkaim

.datan: .word 1n2: .word -1newline: .byte ‘\n’str1: .asciiz “Enter a

number ”.text

main: la $a0, str1li $v0, 4 # print

stringsyscallli $v0, 5 # get

integersyscalllw $t1, nadd $t2, $v0, $t1sw $t2, n2

Storage of Integers

9CMPE12c Gabriel Hugh Elkaim

40 00 00 00 01 43

44 FF FF FF FF 47

48 0A 45 6E 74 4B

4C 65 72 20 61 4F

50 20 6E 75 6D 53

54 62 65 72 20 57

58 00 5B

5C 5F

Memory 1 in 2SC

-1 in 2SC

New line “Enter a

number “

addresses

Storage of Integers

10CMPE12c Gabriel Hugh Elkaim

Memory

And the program would become this eventually:

.textmain: li $a0, 49 # expects an address for a

stringli $v0, 4 # code for put stringsyscalllw $t1, (40) # expects an addressadd $t2, $t1, $t1 # expects “values”sw $t2, (44) # expects an address

• Real machine code has addresses for variables, not labels.

• Later, we will show how to store instructions and get rid of branch labels. Then the .text segment will look similar to the .data segment.

11CMPE12c Gabriel Hugh Elkaim

Endian IssuesConsider this code:

.datan: .word 0x61626364 #ASCII for ‘a’, ‘b’, ’c’, ’d’

.textmain:

lb $a0, nli $v0, 11 # putc codesyscall

Do you think an ‘a’ or a ‘d’ is printed?

12CMPE12c Gabriel Hugh Elkaim

Endian Issues

Answer: ‘a’ since MIPS is a big Big Endian architecture.

40 61 62 63 64 43

addresses

memory

lw $s0, n

$s0 61 62 63 64

But if we did:

We would get this:

031

As we would expect.

13CMPE12c Gabriel Hugh Elkaim

Big Endian:

smallest address is most significant (biggest)

Little Endian:

smallest address is least significant (littlest)

So answer would be ‘d’

If we did a lw $s1, n$s1 ← 0x61626364

Endian Issues

40 64 63 62 61 43

addresses

memory

Same as with big endian

14CMPE12c Gabriel Hugh Elkaim

Endian Issues

Little Endian – address LSB

(VAX, Alpha, x86, PDP-11)

Word Address Bytes MSBLSB 0 3 2 1 0 4 7 6 5 4

Word Address Bytes MSBLSB 0 0 1 2 3 4 4 5 6 7

Bi-Endian:

Selectable data format

Big Endian – address MSB

(IBM 360390, Motorola 680x0, Sparc, MIPS)

15CMPE12c Gabriel Hugh Elkaim

Programmable or bi-endian: on data only (why not instructions?)

i860 (Little-endian instructions)PowerPC (Big-endian instructions)

In general:

When accessing values smaller than a word must alsoconsider alignment, sign extension, and effect on high bytes of register.

Endian Issues

16CMPE12c Gabriel Hugh Elkaim

Arrays

Array implementation is very important

• Most assembly languages have no concept of arrays• From an array, any other data structure we might

want can be built

17CMPE12c Gabriel Hugh Elkaim

Properties of arrays:– Each element is the same size– Char = 1 byte– Integer = 1 word– Elements are stored contiguously– First element at the smallest memory address

In assembly language we must– Allocate correct amount of space for an array– Map array addresses to memory addresses

Arrays

18CMPE12c Gabriel Hugh Elkaim

Arrays

MAL declarations of arrays within memory

•To allocate a portion of memory (more than a singlevariable’s worth)

variablename: type initvalue:numelements

•type is as before - .byte, .word, or .float•numelements is just that, numbering starts at 0 (as in C)•initvalue is a starting value given to each element of the array

19CMPE12c Gabriel Hugh Elkaim

• MAL declarations of arrays within memory

• To allocate a portion of memory (more than a single variable’s worth)variablenametype init_value : num_elements

• Type as before:– .byte .word or .float

• Num_element: numbering starts at 0 (just like in “C”)

• Init_value: the value of each element of the array

Arrays

20CMPE12c Gabriel Hugh Elkaim

Arrays

New directive:

name: .spacenumber_of_bytes

• .space allocates space (in bytes) within memory without giving an initial value.

• The type of the data within this space cannot be inferred.

21CMPE12c Gabriel Hugh Elkaim

Examples:

8 character elements, number 0 – 7, initialized to 0

arrayname: .byte 0:8

18 bytes of memoryname: .space 18

5 integers initialized to 3numbers: .word 3:5

Array Examples

22CMPE12c Gabriel Hugh Elkaim

•If base address of myarray is 25•Byte address of myarray[4] = 25 + 4 = 29

•Base address + distance from the first element

Array of BytesCalculating the address of an array element

char myarray[7] /* C */myarray: .byte 0:7 # MAL

0

25

1 2 3 4 5

26 27 28 29 2A

6

2B

Element index

address

23CMPE12c Gabriel Hugh Elkaim

Addressing Byte Arrays

If we want the 3rd element

•It is at index 2 since indexing starts at zero.•Thus myarray[2] is the at the address of array + 2.•If the 1st element is [0] and is at address 25, then the address of the 3rd element is myarray[2] = 25 + 20

25

1 2 3 4 5

26 27 28 29 30

6

31 address

index

3rd element

24CMPE12c Gabriel Hugh Elkaim

How do you get the address of myarray?

•Use the “load address” instruction, “la”•Keep clear the difference between an

address and the contents of an address.

Addressing Byte Arrays

25CMPE12c Gabriel Hugh Elkaim

To get address of myarray[4] in MAL, write the code…

la $t0, myarrayadd $t1, $t0, 4

If we wanted to decrement element number 5 by 1…

lb $t4, ($t1) # ALT: lb $t4, 4($t1)sub $t4, $t4, 1sb $t4, ($t1)

Addressing Byte Arrays

26CMPE12c Gabriel Hugh Elkaim

Array of Integers

An integer is 32-bits (word) and contains 4 bytes

C++:int myarray[6];

MAL:myarray: .word 0:6

Or myarray: .space 24

27CMPE12c Gabriel Hugh Elkaim

So to find the address of myarray[x] we must find:• Where the array starts (base address)• Size of an element in bytes• Thus byte address of myarray[x] = base + size(x)

So the byte address of myarray[3] = 80+4(3) = 8C

0

80

1 2 3 4 5

84 88 8C 90 94

index

address

Array of Integers

28CMPE12c Gabriel Hugh Elkaim

lw vs. lb

The type of lb is ‘byte’ and of lw is ‘word’

Use lb to manipulate bytesUse lw to manipulate words

29CMPE12c Gabriel Hugh Elkaim

The index of both lb and lw is a byte address.

lb a #address byte in memory location M[a]

#The following indexes base[$t1]base: .byte 0:n

.textla $t0, baseadd $t2, $t0, $t1lb $t3, 0($t2)

lw $t1, a # addresses the 4 bytes starting at M[a]

Both lw and lb should always be used with a base address and allocated space.

lw vs. lb

30CMPE12c Gabriel Hugh Elkaim

2-Dimensional Arrays

2-Dimensional arrays are more complicated in assembly

•Memory is a 1-D array•Must map 2-D array to 1-D array•Arrays have rows and columns

•r x c array•r=rows•c=columns

31CMPE12c Gabriel Hugh Elkaim

2-Dimensional Arrays

Two sensible ways to map 2-D to 1-D

Row major form:(rows are all together)

Column major form:(columns are all together)

0,01,02,03,00,11,12,13,1

0,00,11,01,12,02,13,03,1

4x2 exampl

e

32CMPE12c Gabriel Hugh Elkaim

More Array Examples: 3x5

33CMPE12c Gabriel Hugh Elkaim

2-Dimensional Arrays

How do you calculate addresses in a 2-D array?

•Row Major:

•Column Major:

Base Address + element size(#columns x Ri + Ci)

Base Address + element size(#rows x Ci + Ri)

34CMPE12c Gabriel Hugh Elkaim

2-Dimensional Arrays

Summary of 2D arrays

•Row/Column major (storage order)•Base address•Size of elements•Dimensions of the array

How about 3-D arrays?

35CMPE12c Gabriel Hugh Elkaim

Bounds Checking•Many HLL’s have bounds checking (not C!!!)•Assembly languages have no implied bounds checking•Your program is in total control of memory•With a 5 x 3 array, what does the following address?

array: .word 0:100

.textla $t1, arrayadd $t1, $t1, 15lw $t0, ($t1)

•Bounds checking is often a good idea!!•Most C development environments include optional bounds checking.

36CMPE12c Gabriel Hugh Elkaim

Stacks

A data structure that stores data in the reverse orderthat it is used.•The data is not known until run time.•The classic analogy is a stack of dishes

• You can put single plates on the stack• You can take plates off the stack• Last in, First out (LIFO)

37CMPE12c Gabriel Hugh Elkaim

1st on2nd on3rd on

Data is added or “pushed” onto the stack.

Data is removed or “popped” off the stack.

Stacks

38CMPE12c Gabriel Hugh Elkaim

Stacks

•Printing out a positive integer, character by character•Push LSB to MSB•Pop MSB to LSB (LIFO)

integer = 1024

if integer == 0 thenpush ‘0’

elsewhile integer != 0

digit integer mod basechar digit + 48push char onto stackinteger integer div base

while stack is not emptypop charprint char

39CMPE12c Gabriel Hugh Elkaim

Implementation of a Stack

One implementation of a stack out of an array.

•Index the top of stack (tos), with a stack pointer (sp).

•SP is a variable that contains the address of the emptylocation at the top of the stack (when empty).

initial state, sp points at top of stack

40CMPE12c Gabriel Hugh Elkaim

Stack Implementation

In MAL:

stack: .word 0:50stackend: .word stack+4x50

Or

stack: .space 200

• Labels can be used as initial values• The address (label) of the stack gets put into the

variable

41CMPE12c Gabriel Hugh Elkaim

Pushing and popping code

la $s0, stack #initialization of stack ptr

PUSHsw $s1, 0($s0) # $s1 has data to pushadd $s0, $s0, 4

POPsub $s0, $s0, 4lw $s1, 0($s0)

Stack Implementation

42CMPE12c Gabriel Hugh Elkaim

Stack Implementation

A stack could instead be implemented such that the stack pointer points to a FULL location at the top of the stack.

sp (initial state) sp (1 item on stack)

43CMPE12c Gabriel Hugh Elkaim

PUSH operation:add $s0, $s0, 4sw $s1, 0($s0)

POP operation:lw $s1, 0($s0)sub $s0, $s0, 4

The stack would “grow” from the end of the array towards the beginning.

Stack Implementation

44CMPE12c Gabriel Hugh Elkaim

System Stack

• Gives the programmer an “infinite” dynamic memory.

• OS and HW work together to provide this.

• MIPS uses the $sp ($29) to access.• Starts at the high address and

moves towards the smaller addresses. Why?

45CMPE12c Gabriel Hugh Elkaim

System Stack Examples

46CMPE12c Gabriel Hugh Elkaim

More System Stack Examples

47CMPE12c Gabriel Hugh Elkaim

Queues

• A Queue is a FIFO (First In, First Out)– classic analogy is a line

• Get in (at the TAIL)• wait, move up in line• Get out at front (at the HEAD)

• Putting thing into queue is called ENQUEUE• Getting thing out of the queue is called

DEQUEUE• It takes two pointers to keep track of the head

and tail of the queue– Let’s use $s5 for the HEAD– Let’s use $s7 for the TAIL

48CMPE12c Gabriel Hugh Elkaim

Queues

Initial state:

Head (s5), and Tail (s7)

After 1 enqueue operation:

X

Head (s5) Tail (s7)

After another enqueue operation:

X Y

Head (s5) Tail (s7)

49CMPE12c Gabriel Hugh Elkaim

Queues

After a dequeue operation:

X Y

Head (s5) Tail (s7)

Like stacks, when an item is removed from the datastructure, it is physically still present, but correct use of the structure cannot access it.

50CMPE12c Gabriel Hugh Elkaim

Queues

Implementation of a queue•Storage:

queue: .word 0:infinity # assume infinite for now.text

la $s5, queue # headla $s7, queue # tail

•Enqueue (item):sw $t0, 0($s7) # $t0 has data to storeadd $s7, $s7, 4

•Dequeue (item):beq $s5, $s7, queue_emptylw $t1, 0($s5)add $s5, $s5, 4

•How to add overflow, underflow detection?

51CMPE12c Gabriel Hugh Elkaim

Circular Queues

• To avoid infinite array, wrap around from end tobeginning.

• Head == Tail means empty• Head points to first item (for next dequeue)• Tail point to empty location (for next enqueue)

Q[7] Q[0]

Q[1]

Q[2]

Q[3]Q[4]

Q[5]

Q[6]Example of an 8 element circular queue

Head & Tail pointers

52CMPE12c Gabriel Hugh Elkaim

X

Tail

Head

Circular Queues

After pushing one element

After pushing another element

X

Tail

Head

Y

53CMPE12c Gabriel Hugh Elkaim

Circular Queues

After popping an element

X

Tail

Head

Y

54CMPE12c Gabriel Hugh Elkaim

Circular Queues

•Storage and initialization:

queue: .word 0:queue_sizequeue_end: .word queue+4*queue_sizela $s5, queue # headla $s7, queue # tail

•Enqueue (item)

sw $t0, 0($s7) # data to enqueue is in $t0add $s7, $s7, 4la $t1, queue_endblt $s7, $t1, continue1la $s7, queue # wrap around

continue1:

55CMPE12c Gabriel Hugh Elkaim

Circular Queues

•Dequeue (item):beq $s5, $s7, queue_emptylw $t0, 0($s5)add $s5, $s5, 4la $t1, queue_endblt $s5, $t1, continue2la $s5, queue # wrap around

continue2:

•How to add overflow, underflow detection?

56CMPE12c Gabriel Hugh Elkaim

Modulo for Queues

57CMPE12c Gabriel Hugh Elkaim

Queue Examples

58CMPE12c Gabriel Hugh Elkaim

Summary of data structures

• All data structures are based on the simple array.

• 2D Arrays, Stacks, Queues.• It is all about the implementation.• Bounds checking is important.• If not documented can become

confusing.

59CMPE12c Gabriel Hugh Elkaim

60CMPE12c Gabriel Hugh Elkaim