Memory and Data Structures

Preview:

DESCRIPTION

Memory and Data Structures. Patt and Patel Ch. 10 & 16. Memory. This is the “RAM” in a system We have seen labels and addresses point to pieces of memory storing: words bytes strings numbers Memory is just a collection of bits We could use it to represent integers - PowerPoint PPT Presentation

Citation preview

1

Memory and Data Memory and Data Structures Structures

Patt and Patel Ch. 10 & 16Patt and Patel Ch. 10 & 16

2

MemoryMemory•This is the “RAM” in a systemThis is the “RAM” in a system•We have seen labels and addresses We have seen labels and addresses

point to pieces of memory storing:point to pieces of memory storing:• wordswords• bytesbytes• stringsstrings• numbersnumbers

•Memory is just a collection of bitsMemory is just a collection of bits•We could use it to represent integersWe could use it to represent integers•Or as an arbitrary set of bitsOr as an arbitrary set of bits

3

MemoryMemory

Treat memory as a giant arrayTreat memory as a giant array

•Compiler or programmer decides what Compiler or programmer decides what use to make of it.use to make of it.

•The element numbering starts at 0The element numbering starts at 0•The element number is an addressThe element number is an address•In “C” to allocate some memory:In “C” to allocate some memory:

char m[size_of_array];char m[size_of_array];

4

Storage of DataStorage of Data

•LC-3 architecture is “word LC-3 architecture is “word addressable” meaning that all addressable” meaning that all addresses are “word” addresses.addresses are “word” addresses.

•This means the smallest unit of This means the smallest unit of memory we can allocate is 16-bits, a memory we can allocate is 16-bits, a word.word.

•Use ‘LD’ (load) and ‘ST’ (store) to Use ‘LD’ (load) and ‘ST’ (store) to access this unit (or LDR & STR).access this unit (or LDR & STR).

5

ExampleExample

mychar mychar .BLKW.BLKW 11newlinenewline .FILL.FILL xAxA

……

……LDLD R1, newlineR1, newlineGETCGETCSTST R0, mycharR0, mycharJSRJSR SubSub ; R2=R1-R0; R2=R1-R0BRzBRz found_newlinefound_newline

……found_newlinefound_newline ……

Storage of bytesStorage of bytes

6

Storage of bytesStorage of bytes

The data is placed in The data is placed in memory like this at memory like this at start up (assuming start up (assuming data section starts at data section starts at address 1). address 1).

The mychar variable The mychar variable will change to the will change to the value of the value of the character entered by character entered by the user once stored.the user once stored.

00

11 0000 0000

22 0000 0A0A

33

MemoryMemory

newline newline variablevariable

mychar mychar variablevariable

addresseaddressess

7

Pointers and ArraysPointers and ArraysWe've seen examples of both of these in our We've seen examples of both of these in our LC-3 programs, lets see how these work in “C”LC-3 programs, lets see how these work in “C”

PointerPointer– Address of a variable in memory– Allows us to indirectly access variables

• in other words, we can talk about its address rather than its value

ArrayArray– A list of values arranged sequentially in memory– Example: a list of telephone numbers– Expression a[4] refers to the 5th element of the array a

8

ArraysArrays

Array implementation is very importantArray implementation is very important

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

want can be builtwant can be built

9

Properties of arrays:Properties of arrays:– Each element is the same size– Elements are stored contiguously– First element at the smallest memory address

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

ArraysArrays

10

ArraysArrays

LC-3 declarations of arrays within memoryLC-3 declarations of arrays within memory

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

variablenamevariablename .BLKW.BLKW numelementsnumelements

numelements is just that, numbering starts at 0 (as numelements is just that, numbering starts at 0 (as in C)in C)

11

Array of IntegersArray of IntegersCalculating the address of an array elementCalculating the address of an array element

int int myarray[7]myarray[7] /* C *//* C */myarraymyarray .BLKW.BLKW 77 ; LC-3; LC-3

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

•Base address + distance from the first elementBase address + distance from the first element

00

2525

11 22 33 44 55

2626 2727 2828 2929 2A2A

66

2B2B

Element indexElement index

addressaddressmyarraymyarray

12

•Use the “load effective address” Use the “load effective address” instruction, “LEA”instruction, “LEA”

•Keep clear the difference between an Keep clear the difference between an address and the contents of an address.address and the contents of an address.

Addressing Byte ArraysAddressing Byte Arrays

How do you get the How do you get the address of address of myarraymyarray??

13

To get address of To get address of myarray[4]myarray[4] in LC-3, write the code… in LC-3, write the code…

LEALEA R0, myarrayR0, myarrayADDADD R1, R0, #4R1, R0, #4

If we wanted to increment element number 5 by 1…If we wanted to increment element number 5 by 1…

LDRLDR R4, R1, #0R4, R1, #0ADDADD R4, R4, #1R4, R4, #1STRSTR R4, R1, #0R4, R1, #0

Addressing Byte ArraysAddressing Byte Arrays

14

Address vs. ValueAddress vs. ValueSometimes we want to deal with the Sometimes we want to deal with the addressaddress of a of a memory location, rather than the memory location, rather than the valuevalue it contains. it contains.

Recall example from Chapter 6:Recall example from Chapter 6:adding a column of numbers.adding a column of numbers.

– R2 contains address of first location.– Read value, add to sum, and

increment R2 until all numbershave been processed.

R2 is a pointer -- it contains theR2 is a pointer -- it contains theaddress of data we’re interested in.address of data we’re interested in.

x3107x2819x0110x0310x0100x1110x11B1x0019

x3100

x3101

x3102

x3103

x3104

x3105

x3106

x3107

x3100R2

address

value

15

Another Need for AddressesAnother Need for Addresses

Consider the following “C” function Consider the following “C” function that's supposed to swap the values of that's supposed to swap the values of its arguments.its arguments.

void Swap(int firstVal, int secondVal)void Swap(int firstVal, int secondVal){{ int tempVal = firstVal; int tempVal = firstVal; firstVal = secondVal; firstVal = secondVal; secondVal = tempVal; secondVal = tempVal;}}

16

Executing the Swap Executing the Swap FunctionFunction

firstValsecondVal valueB valueA

344 3

R6

before call

tempVal

firstValsecondVal valueB valueA

3

434 3

R6

after call

These valueschanged...

...but thesedid not.

Swap needs addresses of variables to swap, not values of the variables.

Swap

main

17

Pointers in CPointers in CC lets us talk about and manipulate pointers as C lets us talk about and manipulate pointers as variables and in expressions.variables and in expressions.

DeclarationDeclaration

int *p;int *p; /* p is a pointer to an int */ /* p is a pointer to an int */

A pointer in C is always a pointer to a particular data type:A pointer in C is always a pointer to a particular data type:int*int*, , double*double*, , char*char*, etc., etc.

OperatorsOperators

*p*p -- returns the value pointed to by p-- returns the value pointed to by p

&z&z -- returns the address of variable z-- returns the address of variable z

18

ExampleExample

int i;int i;

int *ptr;int *ptr;

i = 4;i = 4;

ptr = &i;ptr = &i;

*ptr = *ptr + 1;*ptr = *ptr + 1;

store the value 4 into the memory locationassociated with i

store the address of i into the memory location associated with ptr

read the contents of memoryat the address stored in ptr

store the result into memoryat the address stored in ptr

19

Example: LC-3 CodeExample: LC-3 Code; i is 1st local (offset 0), ptr is 2nd (offset -1); i is 1st local (offset 0), ptr is 2nd (offset -1)

; ; i = 4;i = 4;

AND R0, R0, #0 AND R0, R0, #0 ; clear R0; clear R0ADD R0, R0, #4 ADD R0, R0, #4 ; put 4 in R0; put 4 in R0STR R0, R5, #0 STR R0, R5, #0 ; store in i; store in i

; ; ptr = &i;ptr = &i;ADD R0, R5, #0 ADD R0, R5, #0 ; R0 = R5 + 0 (addr of i); R0 = R5 + 0 (addr of i)STR R0, R5, #-1 STR R0, R5, #-1 ; store in ptr; store in ptr

; ; *ptr = *ptr + 1;*ptr = *ptr + 1;LDR R0, R5, #-1 LDR R0, R5, #-1 ; R0 = ptr; R0 = ptrLDR R1, R0, #0 LDR R1, R0, #0 ; load contents (*ptr); load contents (*ptr)ADD R1, R1, #1 ADD R1, R1, #1 ; add one; add oneSTR R1, R0, #0 STR R1, R0, #0 ; store result where R0 points; store result where R0 points

20

2-Dimensional Arrays2-Dimensional Arrays

2-Dimensional arrays are more complicated in 2-Dimensional arrays are more complicated in assemblyassembly

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

• r x c arrayr x c array• r=rowsr=rows• c=columnsc=columns

21

2-Dimensional Arrays2-Dimensional Arrays

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

Row major form:Row major form:(rows are all together)(rows are all together)

Column major form:Column major form:(columns are all together)(columns are all together)

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

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

4x2 4x2 examplexampl

ee

22

2-Dimensional Arrays2-Dimensional Arrays

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

• Row Major:Row Major:

• Column Major:Column Major:

Base Address + (#columns x RBase Address + (#columns x Rii + C + Cii))

Base Address + (#rows x CBase Address + (#rows x Cii + R + Rii))

23

2-Dimensional Arrays2-Dimensional Arrays

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

How about 3-D arrays?How about 3-D arrays?

Summary of 2D arraysSummary of 2D arrays

24

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

arrayarray .BLKW.BLKW 1515

LEALEA R1, arrayR1, arrayADD ADD R1, R1, 15R1, R1, 15LDRLDR R0, R1, 0R0, R1, 0

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

25

StacksStacksA LIFO (last-in first-out) storage structure.A LIFO (last-in first-out) storage structure.

– The first thing you put in is the last thing you take out.

– The last thing you put in is the first thing you take out.

This means of access is what defines a stack, not the This means of access is what defines a stack, not the specific implementation.specific implementation.

Two main operations:Two main operations:

PUSHPUSH:: add an item to the stack add an item to the stack

POPPOP:: remove an item from the stack remove an item from the stack

26

A Physical StackA Physical StackCoin rest in the arm of an automobileCoin rest in the arm of an automobile

19951995 19961996199819981982198219951995

199819981982198219951995

Initial StateInitial State AfterAfterOne PushOne Push

After Three After Three More PushesMore Pushes

AfterAfterOne PopOne Pop

First quarter out is the last quarter in.First quarter out is the last quarter in.

27

A Hardware A Hardware ImplementationImplementation

Data items move between registersData items move between registers

/ / / / / /

/ / / / / /

/ / / / / /

/ / / / / /

/ / / / / /

YesEmpty:

TOP #18

/ / / / / /

/ / / / / /

/ / / / / /

/ / / / / /

NoEmpty:

TOP #12

#5

#31

#18

/ / / / / /

NoEmpty:

TOP #31

#18

/ / / / / /

/ / / / / /

/ / / / / /

NoEmpty:

TOP

Initial State AfterOne Push

After Three More Pushes

AfterTwo Pops

28

A Software A Software ImplementationImplementation

Data items don't move in memory, just our idea Data items don't move in memory, just our idea about there the TOP of the stack is.about there the TOP of the stack is.

/ / / / / /

/ / / / / /

/ / / / / /

/ / / / / /

/ / / / / / TOP

/ / / / / /

/ / / / / /

/ / / / / /

#18

/ / / / / /

TOP

#12

#5

#31

#18

/ / / / / /

TOP #12

#5

#31

#18

/ / / / / /

TOP

Initial State AfterOne Push

After Three More Pushes

AfterTwo Pops

x4000 x3FFF x3FFC x3FFER6 R6 R6 R6

By convention, R6 holds the Top of Stack (TOS) pointer.

29

Basic Push and Pop CodeBasic Push and Pop CodeFor our implementation, stack grows For our implementation, stack grows downward (when item added, TOS downward (when item added, TOS moves closer to 0)moves closer to 0)

PushPush

ADD R6, R6, #-1 ADD R6, R6, #-1 ; decrement stack ptr; decrement stack ptr STR R0, R6, #0 STR R0, R6, #0 ; store data (R0); store data (R0)

PopPop

LDR R0, R6, #0 LDR R0, R6, #0 ; load data from TOS; load data from TOS ADD R6, R6, #1 ADD R6, R6, #1 ; decrement stack ptr; decrement stack ptr

30

Pop with Underflow Pop with Underflow DetectionDetection

If we try to pop too many items off the stack, an If we try to pop too many items off the stack, an underflowunderflow condition occurs. condition occurs.

– Check for underflow by checking TOS before removing data.– Return status code in R5 (0 for success, 1 for underflow)

POP LD R1, EMPTY POP LD R1, EMPTY ; EMPTY = -x4000; EMPTY = -x4000 ADD R2, R6, R1 ADD R2, R6, R1 ; Compare stack pointer; Compare stack pointer

BRz FAIL BRz FAIL ; with x3FFF; with x3FFF LDR R0, R6, #0 LDR R0, R6, #0 ADD R6, R6, #1 ADD R6, R6, #1 AND R5, R5, #0 AND R5, R5, #0 ; SUCCESS: R5 = 0; SUCCESS: R5 = 0 RET RETFAIL AND R5, R5, #0 FAIL AND R5, R5, #0 ; FAIL: R5 = 1; FAIL: R5 = 1

ADD R5, R5, #1ADD R5, R5, #1 RET RETEMPTY .FILL xC000EMPTY .FILL xC000

31

Push with Overflow DetectionPush with Overflow DetectionIf we try to push too many items onto the stack, an If we try to push too many items onto the stack, an overflowoverflow condition occurs. condition occurs.

– Check for underflow by checking TOS before adding data.– Return status code in R5 (0 for success, 1 for overflow)

PUSH LD R1, MAX PUSH LD R1, MAX ; MAX = -x3FFB; MAX = -x3FFB ADD R2, R6, R1 ADD R2, R6, R1 ; Compare stack pointer; Compare stack pointer BRz FAIL BRz FAIL ; with x3FFF; with x3FFF ADD R6, R6, #-1 ADD R6, R6, #-1 STR R0, R6, #0 STR R0, R6, #0 AND R5, R5, #0 AND R5, R5, #0 ; SUCCESS: R5 = 0; SUCCESS: R5 = 0 RET RETFAIL AND R5, R5, #0 FAIL AND R5, R5, #0 ; FAIL: R5 = 1; FAIL: R5 = 1 ADD R5, R5, #1 ADD R5, R5, #1 RET RETMAX .FILL xC005MAX .FILL xC005

32

StacksStacks

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

integer = 1024integer = 1024

if integer == 0 thenif integer == 0 thenpush ‘0’push ‘0’

elseelsewhile integer != 0while integer != 0

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

while stack is not emptywhile stack is not emptypop charpop charprint charprint char

Stack ExampleStack Example

33

Arithmetic Using a StackArithmetic Using a StackInstead of registers, some ISA's use a stack for source and Instead of registers, some ISA's use a stack for source and destination operations: a destination operations: a zero-addresszero-address machine. machine.

– Example:ADD instruction pops two numbers from the stack, adds them, and pushes the result to the stack.

Evaluating (A+B)·(C+D) using a stack:Evaluating (A+B)·(C+D) using a stack:

(1) push A(1) push A(2) push B(2) push B(3) ADD(3) ADD(4) push C(4) push C(5) push D(5) push D(6) ADD(6) ADD(7) MULTIPLY(7) MULTIPLY(8) pop result(8) pop result

Why use a stack?•Limited registers.•Convenient calling

convention for subroutines.•Algorithm naturally

expressed using FIFO data structure.

34

Example: OpAddExample: OpAdd

POP two values, ADD, then PUSH result.POP two values, ADD, then PUSH result.

START

POP POP

OK? OK?

ADD

RangeOK?

PUSH

RETURN

Put back bothPut back first

Yes

No No No

Yes Yes

35

Example: OpAddExample: OpAddOpAddOpAdd JSR POPJSR POP ; Get first operand.; Get first operand.

ADD R5,R5,#0ADD R5,R5,#0 ; Check for POP success.; Check for POP success.BRp ExitBRp Exit ; If error, bail.; If error, bail.ADD R1,R0,#0ADD R1,R0,#0 ; Make room for second.; Make room for second.JSR POPJSR POP ; Get second operand.; Get second operand.ADD R5,R5,#0ADD R5,R5,#0 ; Check for POP success.; Check for POP success.BRp Restore1BRp Restore1 ; If err, restore & bail.; If err, restore & bail.ADD R0,R0,R1ADD R0,R0,R1 ; Compute sum.; Compute sum.JSR RangeCheckJSR RangeCheck ; Check size.; Check size.BRp Restore2BRp Restore2 ; If err, restore & bail.; If err, restore & bail.JSR PUSHJSR PUSH ; Push sum onto stack.; Push sum onto stack.RETRET

Restore2Restore2 ADD R6,R6,#-1ADD R6,R6,#-1 ; Decr stack ptr (undo POP); Decr stack ptr (undo POP)Restore1Restore1 ADD R6,R6,#-1ADD R6,R6,#-1 ; Decr stack ptr; Decr stack ptrExitExit RETRET

36

QueuesQueues

• A queue is a FIFO (First In, First Out).A queue is a FIFO (First In, First Out).– The classic analogy of a queue is a line.– Person gets on the end of the line (the Tail),– Waits,– Gets off at the front of the line (the Head).

• Getting into the queue is a operation called Getting into the queue is a operation called enqueueenqueue

• Taking something off the queue is an operation calledTaking something off the queue is an operation calleddequeuedequeue..

• It takes 2 pointers to keep track of the data structure,It takes 2 pointers to keep track of the data structure,

• Head (let’s use R5)Head (let’s use R5)

• Tail always points to empty element (R6)Tail always points to empty element (R6)

37

QueuesQueues

Initial state:Initial state:

Head (R5), and Tail (R6)Head (R5), and Tail (R6)

After 1 enqueue operation:After 1 enqueue operation:

XX

Head (R5)Head (R5) Tail (R6)Tail (R6)

After another enqueue operation:After another enqueue operation:

XX YY

Head (R5)Head (R5) Tail (R6)Tail (R6)

38

QueuesQueues

After a dequeue operation:After a dequeue operation:

XX YY

Head (R5)Head (R5) Tail (R6)Tail (R6)

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

39

QueuesQueues

Implementation of a queueImplementation of a queue

Storage:Storage:queuequeue .BLKW.BLKW infinity infinity ; assume infinite for now; assume infinite for nowLEALEA R5, queueR5, queue ; head; headLEALEA R6, queueR6, queue ; tail; tail

Enqueue (item):Enqueue (item):STRSTR R0, R6, #0R0, R6, #0 ; R0 has data to store; R0 has data to storeADDADD R6, R6, #1R6, R6, #1

Dequeue (item):Dequeue (item):JSRJSR SUBSUB ; R0 = R5-R6; R0 = R5-R6BRzBRz queue_emptyqueue_emptyLDRLDR R1, R5, #0R1, R5, #0 ; put data in R1; put data in R1ADDADD R5, R5, #1R5, R5, #1

How to add overflow, underflow detection?How to add overflow, underflow detection?

40

Circular QueuesCircular Queues

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

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

Q[7]Q[7] Q[0]Q[0]

Q[1]Q[1]

Q[2]Q[2]

Q[3]Q[3]Q[4]Q[4]

Q[5]Q[5]

Q[6]Q[6]Example of an Example of an 8 element 8 element circular queuecircular queue

Head & Tail Head & Tail pointerspointers

41

XXTailTail

HeadHead

Circular QueuesCircular Queues

After After “enqueue’ing” “enqueue’ing” one elementone element

After After “enqueue’ing” “enqueue’ing” another elementanother element

XX

TailTail

HeadHead

YY

42

Circular QueuesCircular Queues

After After “dequeue’ing” an “dequeue’ing” an elementelement

XX

TailTail

HeadHead

YY

43

Circular QueuesCircular Queues

Storage and initialization:Storage and initialization:

queuequeue .BLKW.BLKW queue_size queue_sizequeue_endqueue_end .BLKW.BLKW 1 1LEA LEA R5, queueR5, queue ; head; headLEALEA R6, queueR6, queue ; tail; tail

Enqueue (item)Enqueue (item)

STRSTR R0, R6, #0R0, R6, #0 ; data to enqueue is in R0; data to enqueue is in R0ADDADD R6, R6, #1R6, R6, #1LEALEA R1, queue_endR1, queue_endJSRJSR SUBSUB ; R1 = R1 – R6; R1 = R1 – R6BRnBRn zz continue1continue1LEALEA R6, queueR6, queue ; wrap around; wrap around

continue1continue1

44

Circular QueuesCircular Queues

Dequeue (item):Dequeue (item):JSRJSR SUBSUB ; R1 = R5 – R6; R1 = R5 – R6BRzBRz queue_emptyqueue_emptyLDRLDR R0, R5, #0R0, R5, #0ADDADD R5, R5, #1R5, R5, #1LEALEA R1, queue_endR1, queue_endJSRJSR SUBSUB ; R1 = R5 – R1; R1 = R5 – R1BRnBRn continue2continue2LEALEA R5, queueR5, queue ; wrap around; wrap around

continue2continue2

• How to add overflow, underflow detection?How to add overflow, underflow detection?

45

Summary of Data Summary of Data StructuresStructures

• All data structures are based on the All data structures are based on the simple array.simple array.

• 2D Arrays, Stacks, Queues.2D Arrays, Stacks, Queues.

• It is all about the implementation.It is all about the implementation.

• Bounds checking is important.Bounds checking is important.

• If not documented can become If not documented can become confusing.confusing.

46

Questions?Questions?

47

Recommended