1 Memory and Data Structures Patt and Patel Ch. 10 & 16.

Slides:



Advertisements
Similar presentations
Chapter 16 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Advertisements

Stack & Queues COP 3502.
Chapter 10 And, Finally.... Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display A Final Collection of ISA-related.
CPU Review and Programming Models CT101 – Computing Systems.
Programming and Data Structure
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
S. Barua – CPSC 240 CHAPTER 10 THE STACK Stack - A LIFO (last-in first-out) storage structure. The.
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
Chapter 6 Programming in Machine Language The LC-3 Simulator
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
Chapters 9 & 10 Midterm next Wednesday (11/19) Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET The Stack SSP & USP Interrupts RTI.
Chapter 9 Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Chapter 9 Trap Routines TRAP number (go to service routine) & RET (return from service routine) Subroutines (or Functions) JSR offset or JSRR rn (go to.
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Memory and Data Structures  Arrays  Stacks  Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Chapter 10 And, Finally... The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Stacks And Queues Chapter 18.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Chapter 7: Memory and Data Structures Arrays Stacks Queues.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Interrupt I/O (again!) Arithmetic using a stack.
Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 10 And, Finally... The Stack
Chapter 14 Functions.
Review Array Array Elements Accessing array elements
Chapter 12 Variables and Operators
The Stack.
Chapter 12 Variables and Operators
Chapter 10 And, Finally... The Stack
Chapter 14 Functions.
Chapter 10 The Stack.
Arithmetic using a stack
Chapter 16 Pointers and Arrays
More C Stack Frames / Pointer variables
Chapter 16 Pointers and Arrays
Pointers and Arrays.
Chapter 16 Pointers and Arrays
Chapter 10 And, Finally... The Stack
A Closer Look at Instruction Set Architectures Chapter 5
Chapter 16 Pointers and Arrays
Computer Organization and Assembly Language
Chapter 14 Functions.
Chapter 16 Pointers and Arrays
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

1 Memory and Data Structures Patt and Patel Ch. 10 & 16

2 Memory This is the “RAM” in a systemThis is the “RAM” in a system We have seen labels and addresses point to pieces of memory storing:We have seen labels and addresses point to pieces of memory storing: words words bytes bytes strings strings numbers numbers 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 Memory Treat memory as a giant array Compiler or programmer decides what use to make of it.Compiler or programmer decides what 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];

4 Storage of Data LC-3 architecture is “word addressable” meaning that all addresses are “word” addresses.LC-3 architecture is “word addressable” meaning that all addresses are “word” addresses. This means the smallest unit of memory we can allocate is 16-bits, a word.This means the smallest unit of memory we can allocate is 16-bits, a word. Use ‘LD’ (load) and ‘ST’ (store) to access this unit (or LDR & STR).Use ‘LD’ (load) and ‘ST’ (store) to access this unit (or LDR & STR).

5 Example mychar.BLKW1 newline.FILLxA … … LDR1, newline GETC STR0, mychar JSRSub; R2=R1-R0 BRzfound_newline … found_newline… Storage of bytes

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

7 Pointers and Arrays We've seen examples of both of these in our LC-3 programs, lets see how these work in “C” Pointer –Address of a variable in memory –Allows us to indirectly access variables in other words, we can talk about its address rather than its valueArray –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 Arrays Array 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 might want can be builtFrom an array, any other data structure we might want can be built

9 Properties of arrays: –Each element is the same size –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

10 Arrays LC-3 declarations of arrays within memory To allocate a portion of memory (more than a single variable’s worth) variablename.BLKWnumelements variablename.BLKWnumelements numelements is just that, numbering starts at 0 (as in C)

11 Array of Integers Calculating the address of an array element int myarray[7]/* C */ myarray.BLKW7; LC-3 If base address of myarray is 25If base address of myarray is 25 Address of myarray[4] = = 29Address of myarray[4] = = 29 Base address + distance from the first elementBase address + distance from the first element A62B Element index address myarray

12 Use the “load effective address” instruction, “LEA”Use the “load effective address” instruction, “LEA” Keep clear the difference between an address and the contents of an address.Keep clear the difference between an address and the contents of an address. Addressing Byte Arrays How do you get the address of myarray?

13 To get address of myarray[4] in LC-3, write the code… LEAR0, myarray ADDR1, R0, #4 If we wanted to increment element number 5 by 1… LDRR4, R1, #0 ADDR4, R4, #1 STRR4, R1, #0 Addressing Byte Arrays

14 Address vs. Value Sometimes we want to deal with the address of a memory location, rather than the value it contains. Recall example from Chapter 6: adding a column of numbers. –R2 contains address of first location. –Read value, add to sum, and increment R2 until all numbers have been processed. R2 is a pointer -- it contains the address of data we’re interested in. x3107 x2819 x0110 x0310 x0100 x1110 x11B1 x0019 x3100 x3101 x3102 x3103 x3104 x3105 x3106 x3107 x3100 R2 address value

15 Another Need for Addresses Consider the following “C” function that's supposed to swap the values of its arguments. void Swap(int firstVal, int secondVal) { int tempVal = firstVal; firstVal = secondVal; secondVal = tempVal; }

16 Executing the Swap Function firstVal secondVal valueB valueA R6 before call tempVal firstVal secondVal valueB valueA R6 after call These values changed......but these did not. Swap needs addresses of variables to swap, not values of the variables. Swap main

17 Pointers in C C lets us talk about and manipulate pointers as variables and in expressions. Declaration int *p; /* p is a pointer to an int */ A pointer in C is always a pointer to a particular data type: int*, double*, char*, etc. Operators *p -- returns the value pointed to by p &z -- returns the address of variable z

18 Example int i; int *ptr; i = 4; ptr = &i; *ptr = *ptr + 1; store the value 4 into the memory location associated with i store the address of i into the memory location associated with ptr read the contents of memory at the address stored in ptr store the result into memory at the address stored in ptr

19 Example: LC-3 Code ; i is 1st local (offset 0), ptr is 2nd (offset -1) ; i = 4; AND R0, R0, #0 ; clear R0 ADD R0, R0, #4 ; put 4 in R0 STR R0, R5, #0 ; store in i ; ptr = &i; ADD R0, R5, #0 ; R0 = R5 + 0 (addr of i) STR R0, R5, #-1 ; store in ptr ; *ptr = *ptr + 1; LDR R0, R5, #-1 ; R0 = ptr LDR R1, R0, #0 ; load contents (*ptr) ADD R1, R1, #1 ; add one STR R1, R0, #0 ; store result where R0 points

20 2-Dimensional Arrays 2-Dimensional arrays are more complicated in assembly Memory is a 1-D array Memory is a 1-D array Must map 2-D array to 1-D array Must map 2-D array to 1-D array Arrays have rows and columns Arrays have rows and columns r x c array r x c array r=rows r=rows c=columns c=columns

21 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,0 1,0 2,0 3,0 0,1 1,1 2,1 3,1 0,0 0,1 1,0 1,1 2,0 2,1 3,0 3,1 4x2 example

22 2-Dimensional Arrays How do you calculate addresses in a 2-D array? Row Major: Row Major: Column Major: Column Major: Base Address + (#columns x R i + C i ) Base Address + (#rows x C i + R i )

23 2-Dimensional Arrays Row/Column major (storage order) Row/Column major (storage order) Base address Base address Size of elements Size of elements Dimensions of the array Dimensions of the array How about 3-D arrays? Summary of 2D arrays

24 Bounds Checking Many HLL’s have bounds checking (not C!!!) Many HLL’s have bounds checking (not C!!!) Assembly languages have no implied bounds checking Assembly languages have no implied bounds checking Your program is in total control of memory Your program is in total control of memory With a 5 x 3 array, what does the following address? With a 5 x 3 array, what does the following address? array.BLKW15 LEAR1, array ADD R1, R1, 15 LDRR0, R1, 0 Bounds checking is often a good idea!! Bounds checking is often a good idea!! Most C development environments include optional bounds checking. Most C development environments include optional bounds checking.

25 Stacks 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 specific implementation. Two main operations: PUSH: add an item to the stack POP: remove an item from the stack POP: remove an item from the stack

26 A Physical Stack Coin rest in the arm of an automobile Initial State After One Push After Three More Pushes After One Pop First quarter out is the last quarter in.

27 A Hardware Implementation Data items move between registers / / / Yes Empty: TOP #18 / / / No Empty: TOP #12 #5 #31 #18 / / / No Empty: TOP #31 #18 / / / No Empty: TOP Initial StateAfter One Push After Three More Pushes After Two Pops

28 A Software Implementation Data items don't move in memory, just our idea about there the TOP of the stack is. / / / TOP / / / #18 / / / TOP #12 #5 #31 #18 / / / TOP #12 #5 #31 #18 / / / TOP Initial StateAfter One Push After Three More Pushes After Two Pops x4000x3FFFx3FFCx3FFE R6 By convention, R6 holds the Top of Stack (TOS) pointer.

29 Basic Push and Pop Code For our implementation, stack grows downward (when item added, TOS moves closer to 0) Push ADD R6, R6, #-1 ; decrement stack ptr STR R0, R6, #0 ; store data (R0) ADD R6, R6, #-1 ; decrement stack ptr STR R0, R6, #0 ; store data (R0)Pop LDR R0, R6, #0 ; load data from TOS ADD R6, R6, #1 ; decrement stack ptr LDR R0, R6, #0 ; load data from TOS ADD R6, R6, #1 ; decrement stack ptr

30 Pop with Underflow Detection If we try to pop too many items off the stack, an underflow 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 ; EMPTY = -x4000 ADD R2, R6, R1 ; Compare stack pointer BRz FAIL ; with x3FFF LDR R0, R6, #0 ADD R6, R6, #1 AND R5, R5, #0 ; SUCCESS: R5 = 0 RET FAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RET EMPTY.FILL xC000

31 Push with Overflow Detection If we try to push too many items onto the stack, an overflow 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 ; MAX = -x3FFB ADD R2, R6, R1 ; Compare stack pointer BRz FAIL ; with x3FFF ADD R6, R6, #-1 STR R0, R6, #0 AND R5, R5, #0 ; SUCCESS: R5 = 0 RET FAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RET MAX.FILL xC005

32 Stacks Printing out a positive integer, character by character Printing out a positive integer, character by character Push LSB to MSB Push LSB to MSB Pop MSB to LSB (LIFO) Pop MSB to LSB (LIFO) integer = 1024 if integer == 0 then push ‘0’ else while integer != 0 digit  integer mod base char  digit + 48 push char onto stack integer  integer div base while stack is not empty pop char print char Stack Example

33 Arithmetic Using a Stack Instead of registers, some ISA's use a stack for source and destination operations: a zero-address 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: (1) push A (2) push B (3) ADD (4) push C (5) push D (6) ADD (7) MULTIPLY (8) pop result Why use a stack? Limited registers. Convenient calling convention for subroutines. Algorithm naturally expressed using FIFO data structure.

34 Example: OpAdd POP two values, ADD, then PUSH result.

35 Example: OpAdd OpAddJSR POP; Get first operand. ADD R5,R5,#0; Check for POP success. BRp Exit; If error, bail. ADD R1,R0,#0; Make room for second. JSR POP; Get second operand. ADD R5,R5,#0; Check for POP success. BRp Restore1; If err, restore & bail. ADD R0,R0,R1; Compute sum. JSR RangeCheck; Check size. BRp Restore2; If err, restore & bail. JSR PUSH; Push sum onto stack. RET Restore2ADD R6,R6,#-1; Decr stack ptr (undo POP) Restore1ADD R6,R6,#-1; Decr stack ptr ExitRET

36 Queues 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 enqueueGetting into the queue is a operation called enqueue Taking something off the queue is an operation called dequeue.Taking something off the queue is an operation called dequeue. 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 Queues Initial state: Head (R5), and Tail (R6) After 1 enqueue operation: X Head (R5) Tail (R6) After another enqueue operation: XY Head (R5) Tail (R6)

38 Queues After a dequeue operation: XY Head (R5) Tail (R6) Like stacks, when an item is removed from the data structure, it is physically still present, but correct use of the structure cannot access it.

39 Queues Implementation of a queue Storage: queue.BLKW infinity; assume infinite for now LEAR5, queue; head LEAR6, queue; tail Enqueue (item): STRR0, R6, #0; R0 has data to store ADDR6, R6, #1 Dequeue (item): JSRSUB; R0 = R5-R6 BRzqueue_empty LDRR1, R5, #0; put data in R1 ADDR5, R5, #1 How to add overflow, underflow detection?

40 Circular Queues To avoid infinite array, wrap around from end to beginning.To avoid infinite array, wrap around from end to 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[0] Q[1] Q[2] Q[3]Q[4] Q[5] Q[6] Example of an 8 element circular queue Head & Tail pointers

41 X Tail Head Circular Queues After “enqueue’ing” one element After “enqueue’ing” another element X Tail Head Y

42 Circular Queues After “dequeue’ing” an element X Tail Head Y

43 Circular Queues Storage and initialization: queue.BLKW queue_size queue_end.BLKW 1 LEA R5, queue; head LEAR6, queue; tail Enqueue (item) STRR0, R6, #0; data to enqueue is in R0 ADDR6, R6, #1 LEAR1, queue_end JSRSUB; R1 = R1 – R6 BRnzcontinue1 LEAR6, queue; wrap around continue1

44 Circular Queues Dequeue (item): JSRSUB; R1 = R5 – R6 BRzqueue_empty LDRR0, R5, #0 ADDR5, R5, #1 LEAR1, queue_end JSRSUB; R1 = R5 – R1 BRncontinue2 LEAR5, queue; wrap around continue2 How to add overflow, underflow detection? How to add overflow, underflow detection?

45 Summary of Data Structures All data structures are based on the simple array.All data structures are based on the 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 confusing.If not documented can become confusing.

46 Questions?

47